如何實現Asp.Net控件的雙擊事件

        Asp.net控件(包括Web服務器控件和Html服務器控件)都沒有雙擊事件,那麼該如何將雙擊事件付給Asp.Net控件呢?我們以Lable控件爲例。
      
        一、首先加入控件,ID爲Lable1,然後加入一個Button控件,ID爲Button1,代碼如下
 
               <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
               <asp:Button ID="Button1" runat="server" Text="Button" />
  
        二、在aspx頁面加入javascript函數響應TextBox1雙擊事件:
 
               <script type="text/javascript">
                      function ondbl()
                      {
                            document.getElementById(“Button1”).click();  //獲取按鈕句柄並觸發提交動作
                      }
              </script>
               //
        三、將雙擊事件及相應函數賦予TextBox控件。在aspx.cs文件的Page_Load函數中加入如下代碼:
 
               protected void Page_Load(object sender, EventArgs e)
                {
                       if (!IsPostBack)
                       {
                               Label1.Attributes.Add("ondblclick", "ondbl");
                       }
                }
 
        四、爲Button1添加響應函數:
          
                protected void Button1_Click(object sender, EventArgs e)
                {
                }
 
        五、至此,工作已基本完成,可以在Button1_Click(object sender, EventArgs e)函數中添加雙擊Lable1後所作動作。需要注意的是示例是在web窗體頁面進行的,如果要在用戶控件中使用需要將『document.getElementById(“Button1”).click();  』中“Button 1”換爲客戶端源文件中的ID。如果不想顯示按鈕,可用css將按鈕隱藏,不能用visible屬性。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章