An example of the ClientScriptManager class on MADN:
1<%@ Page language="C #"%>2<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en"3 "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">4<script runat="Server">5 Public voidPage_Load (Object sender, EventArgs e)6 {7 //Define the name and type of the client scripts on the page.8String csname1 ="Popupscript";9String csname2 ="Buttonclickscript";TenType Cstype = This. GetType ();//Gets the type of the current page One A //Get a ClientScriptManager reference from the Page class. -ClientScriptManager cs =page.clientscript;//Note Here ClientScriptManager object is obtained, ClientScriptManager is sealed class, no constructor, cannot instantiate - the //Check to see if the startup script is already registered. - if(!cs. IsStartupScriptRegistered (Cstype, csname1)) - { -String Cstext1 ="alert (' Hello world ');"; +Cs. RegisterStartupScript (Cstype, csname1, Cstext1,true); - } + A //Check to see if the client script is already registered. at if(!cs. IsClientScriptBlockRegistered (Cstype, csname2)) - { -StringBuilder cstext2 =NewStringBuilder (); -Cstext2. Append ("<script type=\ "text/javascript\" > Function DoClick () {"); -Cstext2. Append ("form1.message.value= ' Text from client script. '} </"); -Cstext2. Append ("script>"); inCs. RegisterClientScriptBlock (Cstype, Csname2, Cstext2. ToString (),false); - } to } +</script> -"http://www.w3.org/1999/xhtml"> the *<title>clientscriptmanager example</title> $Panax Notoginseng<body> -<form id="Form1" therunat="Server"> +<input type="text"Id="Message"/> <input type="Button"Value="ClickMe"onclick="DoClick ()"/> A</form> the</body> +Note above:
① using RegisterStartupScript, it is not necessary to add <script></script> to the script content to be registered, Using the RegisterClientScriptBlock to register a script block of code requires the use of <script></script> otherwise it will cause a script error to be loaded into the Page object and cannot be executed
② uses the Response.Write () method because it destroys the standard of the page (which appears on top of the page)
③ Note the page type gets this. GetType
④ note the acquisition of ClientScriptManager objects, ClientScriptManager is a sealed class, cannot be instantiated
Registering a script with the Page object