Provides an easy way for server code to publish strings into client script code. This object basically provides a mechanism for adding key value pairs and embedding those values into an object that is hosted on the client.

This component supports:

  • Creating individual client side variables
  • Dynamic values that are 'evaluated' in OnPreRender to pick up a value
  • Creating properties of ClientIDs for a given container
  • Changing the object values and POSTing them back on Postback

You create a script variables instance and add new keys to it:

ScriptVariables scriptVars = new ScriptVariables(this,"scriptVars"); // Simple value scriptVars.Add("userToken", UserToken); AmazonBook tbook = new AmazonBook(); tbook.Entered = DateTime.Now; // Complex value marshalled scriptVars.Add("emptyBook", tbook); scriptVars.AddDynamic("author", txtAuthor,"Text"); // Cause all client ids to be rendered as scriptVars.formFieldId vars (Id postfix) scriptVars.AddClientIds(Form,true);

In client code you can then access these variables:

$( function() { alert(scriptVars.book.Author); alert(scriptVars.author); alert( $("#" + scriptVars.txtAmazonUrlId).val() ); });