我在一个页面上有两个GridView,每个GridView在代码隐藏中使用RowDataBound修改为组,在RowDataBound中我为每个分组添加了一个TextBox和Button。 Button添加了Click事件。此外,每个GridView都在UpdatePanel中。
为了解决RowDataBound项和动态控件的PostBack问题,我在同一页面添加了一个简单的Response.Redirect。这段代码工作但Buttons停止触发Click事件大约在GridView列表的一半。在中间标记之后,单击就像启动PostBack而不是触发click事件一样,这会产生与删除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> -- ";
invoiceReceiptStr = " <a href='resendReceipt.aspx?payRegID=" + drv["PaymentInfoID"] + "&totalCost=" + totalCost + "' style='color:white;' >RESEND RECEIPT</a> -- ";
}
else
{
paidStr = "<a href='confirmPaid.aspx?payRegID=" + drv["PayID"] + "' style='color:black;' >MARK PAID</a> -- ";
invoiceReceiptStr = " <a href='resendInvoice.aspx?payRegID=" + drv["PayID"] + "&totalCost=" + totalCost + "' style='color:WHITE;' >RESEND INVOICE</a> -- ";
}
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 + ", Sponsor Fee: $" + Convert.ToDecimal(drv["PartF"].ToString()).ToString("#,##0.00") + cspText + " -- " + paidStr + invoiceReceiptStr + "<div style='float:right;'>Total: $" + Convert.ToDecimal(totalCost).ToString("#,##0.00") + " -- NOTES: ";
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 BUT中的前几个项目,然后它停止工作。有什么帮助吗?
答案 0 :(得分:0)
您无法动态添加控件,并像您一样捕捉事件。
您需要将所有ServerControl放在GridView的模板列中。
在 GridViewRowDatabound 中找到要操作的ServerControl。例如,
Button button = (Button)e.Row.FindControl("MyButton");
然后根据您的逻辑对ServerControl执行任何操作。