In ASP.NET 2.0, the ClientScriptManager class uniquely identifies a script through the keys String and Type. Scripts with the same key and type are considered duplicate scripts. Therefore, we can use script types to avoid confusing similar scripts from different user controls that may be used in the page. <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 client script type and name
6 String csname1 = "PopupScript";
7 String csname2 = "ButtonClickScript";
8 Type cstype = this.GetType();
9
10 // Instantiate new client script class
11 ClientScriptManager cs = Page.ClientScript;
12
13 //Register client startup script to display client alert message when loading 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 execution script and define the client handler for the onClick event of the HTML button
21 if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
twenty two {
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>