动态创建链接按钮单击未触发的事件
本文关键字:事件 单击 创建 链接 按钮 动态 | 更新日期: 2023-09-27 18:17:41
我有一个GridView。在这个GridView中,我们动态生成列。我们还将GridView中的Autogenerate columns设置为true。现在的问题是,我正在动态地添加linkbutton到这些列。
:链接按钮点击不工作。它正在做回发,所有的链接都消失了。我试过添加命令和单击事件,但它们都不工作。如有任何帮助,不胜感激。
背后代码:
protected void GridViewTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null && e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[i].Text;
link.EnableViewState = true;
link.Click += new EventHandler(onLinkClick);
e.Row.Cells[i].Controls.Add(link);
}
}
}
protected void onLinkClick(object sender, EventArgs e)
{
this.ModalPopupExtender1.Show();
}
ASPX :
<asp:GridView
ID="GridViewTest"
runat="server"
AutoGenerateColumns="true"
CssClass="resultsTable"
Width="100%"
CellPadding="2"
CellSpacing="0"
GridLines="None"
autogenerateeditbutton="true"
OnRowDataBound="GridViewPullHistory_RowDataBound"
OnRowCreated="GridViewPullHistory_RowCreated">
<AlternatingRowStyle CssClass="gridRow1" HorizontalAlign="Center" />
<RowStyle CssClass="gridAltRow1" HorizontalAlign="Center" />
<HeaderStyle CssClass="gridHeader" Font-Bold="true" HorizontalAlign="Right" />
</asp:GridView>
只需注册一个PostBack触发器(因为您正在使用一个updatepanel):
ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(link);