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>