Programmatically Adding and Removing Events from a GridView

I have a GridView as shown below:

<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

Based on user input, sometimes I want the OnRowDataBound event to fire, sometimes I don't.

Is there a way to programmatically enable or disable an event?

+3
source share
2 answers

The following is sample code for adding and removing events in VB.NET :

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

Wouldn't it be easier to add an if inside the event handler and ignore the event when you don't need it?

+2
source

Source: https://habr.com/ru/post/1705433/


All Articles