繁体   English   中英

在GridView行数据绑定中动态附加时,LinkBut​​ton.Click事件未触发

[英]LinkButton.Click event not firing when dynamically attached in GridView row databound

我在asp:UpdatePanel里面有一个asp:GridView ,它有一列asp:LinkButton控件。

在行数据绑定事件中,LinkBut​​ton获取它的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">
        ...

代码背后:

protected void gvClientsArchive_RowDataBound(object sender, GridViewRowEventArgs e)
{
    ...
    int company_id = int.Parse(drvRow["company_id"].ToString());
    LinkButton lnkRestore = (LinkButton)e.Row.FindControl("lnkRestore");
    lnkRestore.Click += new System.EventHandler(this.doRestore);

按钮处理程序代码

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是不合适的。 使用RowCreated

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上以声明方式注册处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM