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 class of client script
11 clientscriptmanager cs = page. clientscript;
12
13 // register the client start script. The client alarm message is displayed when the page is loaded.
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 handler for The onclick event of the HTML button
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>

-

Reference: http://www.knowsky.com/340038.html

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.