Robert4Real
Robert4Real

Reputation: 1402

programatically adding and removing events from a GridView

I've got a GridView like 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 upon user input, sometimes I want the OnRowDataBound event to fire, sometimes I don't.

Is there a way to programatically turn the event on or off?

Upvotes: 3

Views: 3324

Answers (2)

Canavar
Canavar

Reputation: 48108

Here is a sample code to add and remove events in VB.NET programatically :

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

Upvotes: 5

laktak
laktak

Reputation: 60103

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

Upvotes: 2

Related Questions