Compartilhar via


Creating a Client-Side Object and SALT

  Microsoft Speech Technologies Homepage

The ColorChooser control creates a client-side object containing generic information that is used when creating the question and confirm prompts. The following code sample creates the client object, and is stored in a separate JScript file labeled ColorChooser-code.js. ColorChooser builds the client-side object at run time as in the section Generating the Final Web Page.

function CC(clientActivationFunctionName, question, questionHelp, preConfirm, postConfirm, confirmHelp, silence, noReco, promptSelectFunctionName)
{
    this.ClientActivationFunctionName = clientActivationFunctionName;    
}

CC.prototype.ClientActivationFunction = function(lastQA, reason, count)
{
    if(this.ClientActivationFunctionName != "")
    {
        return eval(this.ClientActivationFunctionName + "(lastQA, reason, count)");
    }
    return true;
}

Linking the Client Object Script to the Control

The finished ColorChooser-code.js file must be linked to the ColorChooser control. The OnPreRender method of the ColorChooser control links the file, as illustrated in the following code sample.

protected override void OnPreRender(EventArgs e)
{
    Page.RegisterClientScriptBlock("CC_code", "<script language=\"JScript\" src=\"./ColorChooser-code.js\"></script>");
}

Generating the Final Web Page

The speech-enabled Web page that is requested by the user's browser contains standard HTML plus Speech Application Language Tags (SALT) markup and JScript. This page is created at run time by ColorChooser and its child controls. The following RenderSpeechScript method outputs part of the JScript. The method calls the JScript function that creates a new CC object, generates an array of color names, and then creates a new CCPrompt object using the CC object ID and the array of color names as two of the four required parameters.

public override void RenderSpeechScript(HtmlTextWriter writer, SaltModules modules)
{
    if(QuestionPrompt == string.Empty)
    {
        throw new ArgumentNullException(_questionPromptException);
    }
    if(SemanticItem== string.Empty)
    {
        throw new ArgumentNullException(_semanticItemException);
    }

    writer.WriteLine("var " + ClientObjectID + " = new CC(" + 
                            "\"" + ClientActivationFunction  + "\"" + ");");

    writer.Write("availableColors    = new Array();");
    writer.Write("availableColors[0] = \"" + _blue  + "\";");
    writer.Write("availableColors[1] = \"" + _red   + "\";");
    writer.Write("availableColors[2] = \"" + _green + "\";");
    writer.WriteLine("availableColors[3] = \"" + _yellow + "\";");

    writer.WriteLine("var " + ClientPromptObjectID + " = new CCPrompt(" + 
                        "\"" + ClientObjectID           + "\""  + "," +
                        "\"" + QuestionPrompt           + "\""  + "," +
                        "\"" + PromptSelectFunction     + "\""  + "," +
                            "availableColors"                + ");");

    base.RenderSpeechScript(writer, modules);
}

In the preceding sample code, the array of color names is hard-coded in the RenderSpeechScript method. However, for a production application, the developer would probably replace the code that writes the array of color names using code that obtains records from a server-side database of color names.

See Also

Creating ColorChooser: A Custom Application Speech Control | Creating the Class and SemanticItem Property | Creating the Grammars | Implementing the Dialogue Flow | Generating Prompts