2016-07-29 53 views
1

我在asp:UpdatePanel内部有一个asp:GridView,它有一列asp:LinkButton控件。LinkBut​​ton.Click事件在GridView行数据绑定中动态附加时未触发

在行数据绑定事件上,LinkBut​​ton获取它分配的单击事件处理程序。

我已经尽了各种方法试图找出连接点击甚至没有事件发生。

我做错了什么?

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

回答

0

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

+0

我会尽力实施。为什么它不合适? –

+1

@JamesWierzba:已编辑 –

+0

Tim我正在使用'TemplateField'作为此'LinkBut​​ton'行的列。我可以看到如何在aspx中添加OnClick,但是如何在click处理程序事件中获取数据行其余部分的上下文? –

相关问题