C# LinkButton。单击事件在GridView行数据绑定中动态附加时不触发
我在一个C# LinkButton。单击事件在GridView行数据绑定中动态附加时不触发,c#,asp.net,gridview,C#,Asp.net,Gridview,我在一个asp:UpdatePanel中有一个asp:GridView,其中有一列asp:LinkButton控件 在行数据绑定事件上,LinkButton获得其分配的click事件处理程序 我已经尝试了所有我能找到的方法来连接点击均线,但没有一个事件发生 我做错什么了吗 aspx: <asp:UpdatePanel ID="MainUpdatePanel" runat="server" UpdateMode="Conditional"> <ContentTemplat
asp:UpdatePanel
中有一个asp:GridView
,其中有一列asp:LinkButton
控件
在行数据绑定事件上,LinkButton获得其分配的click事件处理程序
我已经尝试了所有我能找到的方法来连接点击均线,但没有一个事件发生
我做错什么了吗
aspx:
<asp:UpdatePanel ID="MainUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTest" Text="test" runat="server" />
<asp:GridView ID="gvClientsArchive" runat="server" AllowSorting="true" DataSourceID="dsClients"
OnRowDataBound="gvClientsArchive_RowDataBound" SkinID="gvList"
AllowPaging="true" PageSize="25" Visible="false">
...
按钮处理程序代码:
private void doRestore(object sender, EventArgs e)
{
lblTest.Text = "restore clicked";
}
我也试过:
protected void gvClientsArchive_RowDataBound(object sender, GridViewRowEventArgs e)
{
...
LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
lnkRestore.Click += delegate
{
lblTest.Text = "restore clicked";
};
如果要注册事件处理程序,
RowDataBound
是不合适的。使用创建的行
:
protected void gvClientsArchive_RowCreated(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
lnkRestore.Click += new System.EventHandler(this.doRestore);
}
}
RowDataBound
仅当您对网格进行数据绑定而不是在每次回发时触发,因为所有控件都是在页面生命周期结束时释放的。也太晚了
如果使用
TemplateFields
在aspx上声明注册处理程序会更容易。我现在就尝试实现它。为什么不合适?Tim我正在使用模板字段
作为此链接按钮
行的列。我可以看到如何在aspx中添加OnClick,但如何在click handler事件中获取数据行其余部分的上下文?@JamesWierzba:您必须将发送方
转换为链接按钮(或控件),然后将NamingContainer
转换为GridViewRow
。现在您可以使用row.FindControl
查找此行中的所有其他控件。您可以在谷歌搜索1天后保存我
protected void gvClientsArchive_RowCreated(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
lnkRestore.Click += new System.EventHandler(this.doRestore);
}
}