动态按钮控件单击事件不触发-有时

本文关键字:有时 事件 按钮 控件 单击 动态 | 更新日期: 2023-09-27 18:03:57

我在一个页面上有两个GridView,每个GridView都在代码后面修改,使用RowDataBound进行分组,并且在RowDataBound中我为每个分组添加了一个文本框和按钮。按钮与Click事件一起添加。此外,每个GridView都在一个UpdatePanel中。

为了解决RowDataBound项目和动态控件的PostBack问题,我添加了一个简单的Response。重定向到同一页面。这段代码可以工作,但是按钮在GridView列表的一半位置停止触发Click事件。在中间标记之后,单击的行为就像它在初始化PostBack而不是触发单击事件,这与剥离RowDataBound信息的问题产生相同的结果。

据我所知,click事件没有被调用。这是我的代码。

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

就像我说的,代码工作在GridView的前几个项目,但然后它停止工作。任何帮助吗?

动态按钮控件单击事件不触发-有时

您不能动态添加控件,并像您所做的那样捕获事件。

步骤1

你需要把所有的ServerControls放在GridView的Template列中。

步骤2

找到你想在GridViewRowDatabound中操作的ServerControl。例如

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

然后根据你的逻辑对ServerControl做任何你想做的事情。