开发者

Dynamically Add Click event to asp button

I am having a problem in adding the button click event dynamically. I am using a grid. one column of that grid has a button. On Row_dataBound event of that grid I am finding that button and Adding the event handler to the click button button of that grid in th开发者_如何学Goe following manner.

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

and I have declared my event handler in the following manner

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

but the issue is I am not getting this event handler called when button is clicked. can anyone tell me what wrong am I doing ???

thanks in advance.


I think this is a postback problem. Because whenever one is assigning event handler, it should be done prior to page load ideally.


I was able to resolve this problem, by simply using onclick event in mark up and then having event handler to code behind and allowing grid to do a post back.


Just to add a little more detail to the solution. You need to use the CommandName attribute for the button and then create an event handler for the entire gridview. The command name you specify will be passed to the handler. You can also dynamically add a command arguemnt which will also be passed. Here's the basics of my solution:

        <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>

Now in my code behind, I add the command argument to my button as the data row is established:

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

        }

And then finally, I create an event handler for the entire gridview and process the postback based on the buttons's command name and command argument attributes.

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

The example on the CommandName attribute helped me a lot: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.commandname(v=vs.110).aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜