Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LinkButton。单击事件在GridView行数据绑定中动态附加时不触发_C#_Asp.net_Gridview - Fatal编程技术网

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