简体   繁体   English

GridView-动态按钮控件的Click事件未触发-有时

[英]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. 我在单个页面上有两个GridView,每个GridView都在代码中被修改为使用RowDataBound进行分组,并且在RowDataBound内为每个分组添加一个TextBox和Button。 The Button is added with a Click event. 该按钮添加了Click事件。 Also, each GridView is in an UpdatePanel. 另外,每个GridView都位于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. 为了解决RowDataBound项和动态控件的PostBack问题,我在同一页面上添加了一个简单的Response.Redirect。 This code works BUT the Buttons stop firing the Click event about half way down the GridView list. 这段代码有效,但Buttons停止触发GridView列表中途大约一半的Click事件。 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. 中途标记之后,单击的行为就像启动PostBack而不是触发click事件一样,其结果与RowDataBound信息被剥离的问题相同。

From what I can tell, the click event is not being called. 据我所知,没有调用click事件。 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. 就像我说的那样,该代码适用于GridView BUT中的前几个项目,然后停止工作。 Any help? 有什么帮助吗?

You cannot add controls dynamically, and catch the event like the way you are doing. 您无法动态添加控件,也无法像执行操作那样捕获事件。

Step 1 第1步

You need to place all ServerControls inside the GridView's Template columns. 您需要将所有ServerControl放置在GridView的Template列中。

Step 2 第2步

Find the ServerControl you want to manipulate inside GridViewRowDatabound . GridViewRowDatabound中找到要操作的ServerControl。 For example, 例如,

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

Then do whatever you want to the ServerControl based on your logic. 然后根据您的逻辑对ServerControl做任何您想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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