2011-04-10 95 views
0

我在動態添加按鈕單擊事件時遇到問題。 我正在使用網格。該網格的一列有一個按鈕。在該網格的Row_dataBound事件中,我找到該按鈕,並按照以下方式將該事件處理程序添加到該網格的單擊按鈕按鈕。動態添加Click事件到asp按鈕

protected void grdDisplayUserLeave_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       Button btnApprove = (Button)e.Row.FindControl("btnApprove"); 
       Button btnDisApprove = (Button)e.Row.FindControl("btnDisApprove"); 

       UserLeaveDTO objUserLeave = (UserLeaveDTO)e.Row.DataItem; 
       btnApprove.OnClientClick = "leaveApplication.HoldLeaveId(" + objUserLeave.LeaveId + ",'" + hdnLeaveId.ClientID + "')"; 
       btnDisApprove.OnClientClick = "leaveApplication.HoldLeaveId(" + objUserLeave.LeaveId + ",'" + hdnLeaveId.ClientID + "')"; 

       //btnApprove.Attributes.Add("onclick", "leaveApplication.HoldLeaveId("+objUserLeave.LeaveId+",'"+hdnLeaveId.ClientID+"')"); 
       //btnDisApprove.Attributes.Add("onclick", "leaveApplication.HoldLeaveId(" + objUserLeave.LeaveId + ",'" + hdnLeaveId.ClientID + "')"); 

       btnApprove.Click += new EventHandler(Handle_ApproveLeave); 
       btnDisApprove.Click += new EventHandler(Handle_ApproveLeave); 
      } 
     } 

,我宣佈我的事件處理程序通過以下方式

protected void Handle_ApproveLeave(object sender, EventArgs e) 
     { 
      //long cusomerId = Convert.ToInt64(deleteItemIdValue.Value); 
     } 

但問題是,我沒有得到這個按鈕被點擊時調用的事件處理程序。 任何人都可以告訴我我在做什麼錯?

在此先感謝。

回答

0

我能夠解決這個問題,只需在標記中使用onclick事件,然後讓事件處理程序在後面編寫代碼並允許網格執行回發。

0

我認爲這是一個回發問題。因爲無論何時分配事件處理程序,都應該在頁面加載完成之前完成。

+0

我能夠解決此問題,只需使用的onclick中標記事件了,然後有事件處理程序的代碼背後,並允許電網做回帖。無論如何感謝回答這個問題。我真的很感謝有人回答這個問題:) – Sumit 2012-02-28 09:17:58

0

只是爲解決方案添加更多的細節。您需要爲該按鈕使用CommandName屬性,然後爲整個gridview創建一個事件處理程序。您指定的命令名稱將傳遞給處理程序。你也可以動態地添加一個命令arguemnt,它也將被傳遞。這裏是我的解決方案的基礎:

 <asp:GridView ID="gvStudiesInProgress" DataSourceID="dsIncompleteStudies" AutoGenerateColumns="false" OnRowDataBound="gvStudiesInProgress_RowDataBound" OnRowCommand="gvStudiesInProgress_RowCommand"> 
     <Columns> 
      <asp:BoundField DataField="physician.id" HeaderText="ID" /> 
      <asp:BoundField DataField="physician.firstName" HeaderText="First Name" /> 
      <asp:BoundField DataField="physician.lastName" HeaderText="Last Name" /> 
      <asp:ButtonField HeaderText="Reopen Study?" ButtonType="Button" ControlStyle-CssClass="pure-button pure-button-success pure-button-small" Text="Reopen" CommandName="Reopen" /> 
     </Columns> 
     </asp:GridView> 

現在,在後面我的代碼,我的命令參數添加到我的按鈕作爲數據行被確定:

 protected void gvStudiesInProgress_RowDataBound(object sender, GridViewRowEventArgs e) 
     {   
      // Only perform these operations on datarows, skip the header 
      if (e.Row.RowType != DataControlRowType.DataRow) 
       return; 

      saveSet currSaveSet = (saveSet)e.Row.DataItem; 

      // Add the saveSetId attribute to the row's repoen button 
      ((Button)e.Row.Cells[5].Controls[0]).CommandArgument = currSaveSet.saveSetId.ToString(); 

     } 

然後終於,我創建活動處理整個gridview並基於按鈕的命令名和命令參數屬性處理回發。

 protected void gvStudiesInProgress_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 
      // Allow the Reopen button to trigger a study reset 
      if (e.CommandName == "Reopen") 
      { 
       bool reopened = DAL.reopenTimeStudy(int.Parse(e.CommandArgument.ToString())); 
      } 
     } 

上的CommandName屬性的例子對我幫助很大: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.commandname(v=vs.110).aspx