ASP.NET2.0中的ClientScriptManager 類用法—如何添加用戶端事件!

來源:互聯網
上載者:User
在ASP.NET2.0中,ClientScriptManager 類通過鍵 String 和 Type 唯一地標識指令碼。具有相同的鍵和類型的指令碼被視為重複指令碼。因此,我們可以使用指令碼類型來避免混淆可能用在頁中的來自不同使用者控制項的相似指令碼。

<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     // 定義用戶端指令碼類型和名稱
 6     String csname1 = "PopupScript";
 7     String csname2 = "ButtonClickScript";
 8     Type cstype = this.GetType();
 9         
10     // 執行個體化用戶端指令碼新類
11     ClientScriptManager cs = Page.ClientScript;
12 
13     // 註冊用戶端起始指令碼,在載入頁時顯示用戶端警報訊息
14     if (!cs.IsStartupScriptRegistered(cstype, csname1))
15     {
16       String cstext1 = "alert('Hello World');";
17       cs.RegisterStartupScript(cstype, csname1, cstext1, true);
18     }
19 
20     // 註冊用戶端執行指令碼,定義 HTML 按鈕的 onClick 事件的用戶端處理常式
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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.