|
Tuesday, 20 December 2011 00:12 |
There is one main class in the ASP.NET Framework included for working with Javascript: the ClientScriptManager class. This class is exposed by the Page.ClientScript property. The ClientScriptManager class has several useful methods for working with Javascript including:
RegisterClientScriptBlock():
Adds a script to a page right after the opening server-side tag
StringBuilder cstext2 = new StringBuilder();
cstext2.Append(" function DoClick() {");
cstext2.Append("Form1.Message.value='Text from client script.'} ");
cstext2.Append("script>");
RegisterClientScriptBlock(csname2, cstext2.ToString());
RegisterStartupScript():
Adds a script to a page right before the closing server-side tag
String cstext1 = "" +
"alert('Hello World');" + "script>";
RegisterStartupScript(csname1, cstext1);
RegisterClientScriptInclude():
Adds a reference to an external Javascript file
GetWebResourceUrl() - Returns the URL to a server-side resource
// Define the name, type and url of the client script on the page.
String csname = "ButtonClickScript";
String csurl = "~/script_include.js";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the include script exists already.
if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
{
cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));
}
 Read more: |