ScriptVariables Class
ScriptVariables is a utility class that lets you safely render server side variables/values into JavaScript code so it can be used by client side code.
It works by creating a JavaScript object that is rendered into a script block based on server side values you add to this object. Each key and value pair is then rendered into the page as part of a client side Javascript object using the .ToString()
or .ToHtmlString()
methods.
Definitions automatically handle:
- Variable name declaration * Safe Serialization of values * Optional pre and post script code * Optional wrapping of script in script tags
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() ); });<>
Westwind.AspNetCore.Utilities.ScriptVariables
Class Members
Member | Description | |
---|---|---|
![]() |
Constructor | Constructor that optionally accepts the name of thevariable that is to be created |
![]() |
Add | Adds a property and value to the client side object to be rendered intoJavaScript code. VariableName becomes a property on the object and thevalue will be properly converted into JavaScript…
public void Add(string variableName, object value)
public void Add(IDictionary
|
![]() |
AddScriptAfter | Any custom JavaScript code that is to immediately follow theclient object declaration. This allows setting up of namespacesif necesary for scoping.
public void AddScriptAfter(string scriptCode)
|
![]() |
AddScriptBefore | Any custom JavaScript code that is to immediately preceed theclient object declaration. This allows setting up of namespacesif necesary for scoping.
public void AddScriptBefore(string scriptCode)
|
![]() ![]() |
Serialize | Serializes value or object to JSON using:* Indented formatting for objects* Dates formatted as `new Data(3123312312)`
public string Serialize(object value, bool useCamelCase)
|
![]() |
ToHtmlString | Returns the script as an MVC/Razor HTML string so it embedsproperly as raw text. Use this version in Razor views toavoid having to use `Html.Raw()`.@scriptVars.ToHtmlString()
public HtmlString ToHtmlString(bool addScriptTags)
|
![]() |
ToString | Returns the rendered JavaScript as a string
public string ToString()
public string ToString(bool addScriptTags)
|
![]() |
ClientObjectName | The name of the object generated in client script codeCan be either single object name like `serverVars` or a complexname like `window.global.authData`.If a single variable name is used a `var`… |
![]() |
NoVar | If true, no `var` prefix is added to the single variable nameused in ClientObjectName. This is useful when assigning topre-existing objects or for creating 'global' objects.Applies only… |
![]() |
Setting | |
![]() |
UseCamelCase | Determines whether script variables that objects or collections areautomatically serialized using camelCase names. |
Assembly: Westwind.AspNetCore.dll
© West Wind Technologies, 2025 • Updated: 2025-03-08
Comment or report problem with topic