Use the clientscriptmanager class in ASP. net2.0-how to add client events!

Source: Internet
Author: User
In ASP. net2.0, The clientscriptmanager class uniquely identifies a script by the string and type keys. Scripts with the same key and type are considered as repeated scripts. Therefore, we can use the script type to avoid obfuscation of similar scripts that may be used on pages from different user controls.

< Html >
< Head >
< Title > Clientscriptmanager example </ Title >
</ Head >
< Body >
< Form ID = "Form1"
Runat = "Server" >
< Input Type = "Text" ID = "Message" >   < Input Type = "Button" Value = "Clickme" Onclick = "Doclick ()" >
</ Form >
</ Body >
</ Html > 1 <% @ Page Language = " C # " %>
2 < Script runat = " Server " >
3 Public   Void Page_load (Object sender, eventargs E)
4 {
5 // Define the client script type and name
6 String csname1 =   " Popupscript " ;
7 String csname2 =   " Buttonclickscript " ;
8 Type cstype =   This . GetType ();
9
10 // Instantiate a new client script class
11 Clientscriptmanager CS = Page. clientscript;
12
13 // Register the client start script and display the client alarm message when loading the page
14 If ( ! CS. isstartupscriptregistered (cstype, csname1 ))
15 {
16 String cstext1 =   " Alert ('Hello World '); " ;
17 CS. registerstartupscript (cstype, csname1, cstext1, True );
18 }
19
20 // Register the client to execute the script and define the client to handle the onclick event of the HTML buttonProgram
21 If ( ! CS. isclientscriptblockregistered (cstype, csname2 ))
22 {
23 Stringbuilder cstext2 =   New Stringbuilder ();
24 Cstext2.append ( " <SCRIPT type = text/JavaScript> function doclick (){ " );
25 Cstext2.append ( " Form1.message. value = 'text from client script. '} </ " );
26 Cstext2.append ( " SCRIPT> " );
27 CS. registerclientscriptblock (cstype, csname2, cstext2.tostring (), False );
28 }
29 }
30 </ Script >

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.