Registering a script with the Page object

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.