以编程方式添加和删除GridView中的事件

时间:2009-03-26 19:40:15

标签: asp.net gridview

我有一个如下所示的GridView:

<asp:GridView ID="Results" runat="server" OnRowDataBound="Results_RowDataBound">
    <EmptyDataTemplate>No results found</EmptyDataTemplate>
</asp:GridView>



Protected Sub Results_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            'do a bunch of work here
End Sub

根据用户输入,有时我想要触发OnRowDataBound事件,有时我不会。

有没有办法以编程方式打开或关闭事件?

2 个答案:

答案 0 :(得分:5)

以编程方式add and remove events in VB.NET的示例代码:

If CheckBox1.Checked Then
    AddHandler Results.RowDataBound, AddressOf Results_RowDataBound
Else
    RemoveHandler Results.RowDataBound, AddressOf Results_RowDataBound
End If

答案 1 :(得分:2)

在您的事件处理程序中添加if并在不需要时忽略该事件会不会更容易?