To create a client object with server variables you can use this basic code:
// *** Specify page instance and optional name of the client object ScriptVariables scriptVars = new ScriptVariables(this,"serverVars"); // *** Add any values static or dynamic scriptVars.Add("name", "Rick Strahl"); scriptVars.Add("company", "Rick's \"East\\West\" Trading"); scriptVars.Add("entered", DateTime.Now.AddYears(-10)); scriptVars.Add("counter",12.22M); // *** Add a control's value dynamically at pre render time scriptVars.AddDynamicValue("txtNameValue", this.txtName, "Text");
Which creates a client object like this:
var serverVars = { name: "Rick Strahl", company: "Rick's \"East\\West\" Trading", entered: new Date(888053450808), counter: 12.22, txtNameValue: "Rick" };
Each value added to the collection translates into a property on the generated JavaScript object. Not that you can add fixed values via .Add() and dynamic values that are evaluated pre-render time using AddDynamicValue().
You can also embed client script ids into the page as follows:
// *** Add all Client Ids - note this may cause naming conflicts on duplicate names// *** in separate naming containers. First one wins!scriptVars.AddClientIds(this,true);
which results in the following generated client ids:
var serverVars = { name: "Rick Strahl", company: "Rick's \"East\\West\" Trading", entered: new Date(888054678218), counter: 12.22, txtNameValue: "Rick", headId: "ctl00_head", form1Id: "aspnetForm", ContentId: "ctl00_Content", txtNameId: "ctl00_Content_txtName", btnSubmitId: "ctl00_Content_btnSubmit", panel1Id: "ctl00_Content_panel1", txtPanelTextBoxId: "ctl00_Content_txtPanelTextBox", repListId: "ctl00_Content_repList", gdvTestId: "ctl00_Content_gdvTest" };
In this example recursive Ids are generated, but you can also specify a specific container and no-recursion to minimize naming conflicts and proliferation of variables.
ScriptVariables scriptVars = new ScriptVariables(this,"serverVars"); scriptVars.UpdateMode = AllowUpdateTypes.All; // *** Add any values static or dynamic scriptVars.Add("name", "Rick Strahl"); scriptVars.Add("company", "Rick's \"East\\West\" Trading"); scriptVars.Add("entered", DateTime.Now.AddYears(-10)); scriptVars.Add("counter",12.22M); // *** Read back values assigned to the client object if (this.IsPostBack) { Response.Write(scriptVars.GetValue<string>("name")); Response.Write(scriptVars.GetItemValue<string>("Custom")); Response.Write(scriptVars.GetItemValue<string>("CustomValue")); Response.Write(scriptVars.GetItemValue<DateTime>("curDate")); Response.Write(scriptVars.GetValue<DateTime>("entered")); }
Note that values are available only on Postback. Non-postback access causes an exception. On the client you can simply update the properties of the generated object or the _Items collection via .add():
// *** Update Properties to be sent back to server serverVars.entered = new Date(); serverVars.name = "Jimmy Johnston Jr."; // *** Update _Items 'collection' serverVars.add("Custom","Rick"); serverVars.add("CustomValue","West Wind"); serverVars.add("curDate",new Date(2001,10,1));
Property values can be read back on the server with scriptVars.GetValue<t>(property) and Items collection values with .GetItemValue<t>(key).