The ClientScriptManager class usage in asp.net2.0

Source: Internet
Author: User
Tags define key string client
Asp.net|client

ClientScriptManager class usage in asp.net2.0-How to add a client event

In asp.net2.0, the ClientScriptManager class uniquely identifies the script by key String and type. Scripts that have the same keys and types are treated as duplicate scripts. Therefore, we can use script types to avoid confusing similar scripts that might be used in pages from different user controls. <title>clientscriptmanager example</title>
<body>
<form id= "Form1"
runat= "Server" >
<input type= "text" id= "message" > <input type= "button" value= "ClickMe" >
</form>
</body>

1 <%@ Page language= "C #"%>
2 <script runat= "Server" >
3 public void Page_Load (Object sender, EventArgs e)
4 {
5//Define client script type and name
6 String csname1 = "Popupscript";
7 String csname2 = "Buttonclickscript";
8 Type Cstype = this. GetType ();
9
10//Instantiate client script new class
One ClientScriptManager cs = page.clientscript;
12
13//Register client start script, display client alert message when page is loaded
An if (!CS). IsStartupScriptRegistered (Cstype, csname1))
15 {
String cstext1 = "alert (' Hello world ');";
CS. RegisterStartupScript (Cstype, csname1, Cstext1, true);
18}
19
20//Register client execution script, define the HTML button's OnClick event client Handler
An if (!cs. IsClientScriptBlockRegistered (Cstype, csname2))
22 {
StringBuilder cstext2 = new StringBuilder ();
Cstext2. Append ("<script type=text/javascript> function DoClick () {");
Cstext2. Append ("Form1.message.value= ' Text from client script. '} </");
Cstext2. Append ("script>");
CS. RegisterClientScriptBlock (Cstype, Csname2, Cstext2. ToString (), false);
28}
29}
</script>



Related Article

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.