简体   繁体   中英

GridView - Dynamic Button Control Click Event Not Firing - Sometimes

I have two GridViews on a single page, each GridView is modified in the code-behind to group using RowDataBound and within the RowDataBound I am adding a TextBox and Button for each grouping. The Button is added with a Click event. Also, each GridView is in an UpdatePanel.

In order to get around the PostBack issue with RowDataBound items and dynamic controls I have added a simple Response.Redirect to the same page. This code works BUT the Buttons stop firing the Click event about half way down the GridView list. After the half way mark the click just acts like it is initiating a PostBack instead of firing the click event, which gives same results as the problem of the RowDataBound information being stripped out.

From what I can tell, the click event is not being called. This is the code I have.

protected void GridViewRowDatabound(object sender, GridViewRowEventArgs e)
{
        nonPaidCLB.Visible = true;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (tmpCategoryName != drv["Num"].ToString())
            {
                string useTable = "";
                string giftDoorPrize = "";
                if (drv["Table"].Equals("N"))
                {
                    useTable = " - NOT Using Table";
                }
                if (drv["Gift"].Equals("Y"))
                {
                    giftDoorPrize = " - Providing gift or door prize";
                }
                tmpCategoryName = drv["Num"].ToString();
                Table tbl = e.Row.Parent as Table;
                if (tbl != null)
                {
                    GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
                    TableCell cell = new TableCell();
                    cell.ColumnSpan = this.GridView.Columns.Count;
                    cell.Style.Add("font-weight", "bold");
                    cell.Style.Add("background-color", "#4382C0");
                    cell.Style.Add("color", "white");

                    TextBox notes2TXTBOX = new TextBox();
                    notes2TXTBOX.ID = "notes2TXTBOX";
                    notes2TXTBOX.Attributes.Add("runat", "server");
                    notes2TXTBOX.Text = drv["Notes"].ToString();
                    notes2TXTBOX.Style.Add("width", "400px");

                    Label payInfoID2TXTBOX = new Label();
                    payInfoID2TXTBOX.ID = "payInfoID2TXTBOX";
                    payInfoID2TXTBOX.Text = drv["PayID"].ToString();
                    payInfoID2TXTBOX.Attributes.Add("runat", "server");

                    Button saveNotes2BTN = new Button();
                    saveNotes2BTN.ID = "saveNotes2BTN";
                    saveNotes2BTN.Text = "Update";
                    saveNotes2BTN.Attributes.Add("runat", "server");
                    saveNotes2BTN.Click += saveNotes2_OnClick;

                    string paidStr = string.Empty;
                    string invoiceReceiptStr = string.Empty;
                    decimal totalCost = 0;
                    totalCost = (Convert.ToInt16(drv["NumAtt"]) - 1) * Convert.ToDecimal(drv["FullF"]) + Convert.ToDecimal(drv["PartF"]) + Convert.ToDecimal(drv["CSPF"]);

                    if (drv["pd"].Equals("Y"))
                    {
                        paidStr = "<strong>PAID</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                        invoiceReceiptStr = " <a href='resendReceipt.aspx?payRegID=" + drv["PaymentInfoID"] + "&totalCost=" + totalCost + "' style='color:white;' >RESEND RECEIPT</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    else
                    {
                        paidStr = "<a href='confirmPaid.aspx?payRegID=" + drv["PayID"] + "' style='color:black;' >MARK PAID</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                        invoiceReceiptStr = " <a href='resendInvoice.aspx?payRegID=" + drv["PayID"] + "&totalCost=" + totalCost + "' style='color:WHITE;' >RESEND INVOICE</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    string cspText = "";
                    if (!drv["CSPF"].ToString().Equals("0.0000"))
                    {
                        cspText = ", CSPresenter ($" + Convert.ToDecimal(drv["ConcurSesPresFee"]).ToString("#,##0.00") + ")";
                    }
                    HtmlGenericControl span = new HtmlGenericControl("span");
                    span.InnerHtml = "<a href='/Events/Invoice.aspx?confID=" + conferenceDDL.SelectedValue + "&payID=" + drv["PayID"] + "' target='_blank' style='color:white;' >" + tmpCategoryName + "</a>" + useTable + "" + giftDoorPrize + ",&nbsp;Sponsor Fee: $" + Convert.ToDecimal(drv["PartF"].ToString()).ToString("#,##0.00") + cspText + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + paidStr + invoiceReceiptStr + "<div style='float:right;'>Total: $" + Convert.ToDecimal(totalCost).ToString("#,##0.00") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NOTES:&nbsp;";
                    cell.Controls.Add(span);
                    cell.Controls.Add(notes2TXTBOX);
                    cell.Controls.Add(saveNotes2BTN);
                    cell.Controls.Add(new LiteralControl("</div>"));
                    row.Cells.Add(cell);
                    tbl.Rows.AddAt(tbl.Rows.Count - 1, row);
                }
            }
        }
    }
}

Like I said, the code works for the first several items in the GridView BUT then it stops working. Any help?

You cannot add controls dynamically, and catch the event like the way you are doing.

Step 1

You need to place all ServerControls inside the GridView's Template columns.

Step 2

Find the ServerControl you want to manipulate inside GridViewRowDatabound . For example,

Button button = (Button)e.Row.FindControl("MyButton");

Then do whatever you want to the ServerControl based on your logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM