Revision: 44217
Updated Code
at April 8, 2011 02:14 by housecor
Updated Code
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "myuniquescriptkey", "<script type=\"text/javascript\" language=\"javascript\">var i = 4;</script>");
//A more involved example sending down a list of objects in json:
public class Employee
{
public string Name { get; set; }
public string ID { get; set; }
public string Age { get; set; }
}
Employee oEmployee1 = new Employee { Name = "Pini", ID = "111", Age = "30" };
Employee oEmployee2 = new Employee { Name = "Yaniv", ID = "Cohen", Age = "31" };
Employee oEmployee3 = new Employee { Name = "Yoni", ID = "Biton", Age = "20" };
List<Employee> oList = new List<Employee>() { oEmployee1, oEmployee2, oEmployee3 };
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = oSerializer.Serialize(oList);
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "myuniquescriptkey", "<script type=\"text/javascript\" language=\"javascript\">var employees = " + json + ";</script>");
//Then, to parse the list:
jQuery.each(employees, function() {
alert(this.Name);
});
Revision: 44216
Updated Code
at April 8, 2011 02:13 by housecor
Updated Code
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "myuniquescriptkey", "<script type=\"text/javascript\" language=\"javascript\">var i = 4;</script>");
//A more involved example sending down a list of objects in json:
Employee oEmployee1 = new Employee { Name = "Pini", ID = "111", Age = "30" };
Employee oEmployee2 = new Employee { Name = "Yaniv", ID = "Cohen", Age = "31" };
Employee oEmployee3 = new Employee { Name = "Yoni", ID = "Biton", Age = "20" };
List<Employee> oList = new List<Employee>() { oEmployee1, oEmployee2, oEmployee3 };
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = oSerializer.Serialize(oList);
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "myuniquescriptkey", "<script type=\"text/javascript\" language=\"javascript\">var employees = " + json + ";</script>");
//Then, to parse the list:
jQuery.each(employees, function() {
alert(this.Name);
});
Revision: 44215
Updated Code
at April 8, 2011 01:58 by housecor
Updated Code
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "myuniquescriptkey", "<script type=\"text/javascript\" language=\"javascript\">var i = 4;</script>");
Revision: 44214
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 8, 2011 01:51 by housecor
Initial Code
Page.ClientScript.RegisterClientScriptBlock("name", "script body goes here");
Initial URL
Initial Description
Options: 1. Write the desired values into a hidden field that contains json. Reference the hidden field by ID and parse the json to get the desired vars. 2. Call a webmethod that returns the values in json format 3. Write js via the code-behind that writes the variables to the page in a separate script tag. The js is placed inline in the page. Call: Page.ClientScript.RegisterClientScriptBlock. See example below. So why is putting the entire javasscript in a string within the code-behind a lousy idea? 1. No code coloring 2. No syntax checking 3. Have to escape quotes which makes it hard to read and adds work 4. JS isn't cached since it's a string sitting within the html, unlike an attached .js file. 5. No separation of concerns. Client script should ideally be in a .js file to keep server side business logic from being intermingled with other concerns. 6. Inability to reuse. A useful .js file can be called from multiple pages. js stored in a string of a code-behind is page specific. 7. No IDE assistance for formatting or intellisense support
Initial Title
Access server side C# variabes in javascript
Initial Tags
Initial Language
JavaScript