【问题标题】:Dynamically Add Click event to asp button将 Click 事件动态添加到 asp 按钮
【发布时间】:2011-04-10 15:49:47
【问题描述】:

我在动态添加按钮单击事件时遇到问题。 我正在使用网格。该网格的一列有一个按钮。在该网格的 Row_dataBound 事件上,我找到了该按钮并以下列方式将事件处理程序添加到该网格的单击按钮按钮。

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);
        }

但问题是单击按钮时我没有调用此事件处理程序。 谁能告诉我我做错了什么???

提前致谢。

【问题讨论】:

    标签: events button click add dynamic


    【解决方案1】:

    我认为这是一个回发问题。因为每当分配事件处理程序时,最好在页面加载之前完成。

    【讨论】:

    • 我能够解决这个问题,只需在标记中使用 onclick 事件,然后让事件处理程序在后面编码并允许网格回发。无论如何感谢您回答问题。我真的很感谢有人回答这个问题:)
    【解决方案2】:

    我能够解决这个问题,只需在标记中使用 onclick 事件,然后让事件处理程序在后面编码并允许网格回发。

    【讨论】:

      【解决方案3】:

      只是为解决方案添加更多细节。您需要为按钮使用 CommandName 属性,然后为整个 gridview 创建一个事件处理程序。您指定的命令名称将传递给处理程序。您还可以动态添加一个也将被传递的命令argument。这是我的解决方案的基础知识:

              <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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多