问题
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?
回答1:
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
回答2:
Wouldn't it be easier to add an if inside your event handler and ignore the event when you don't need it?
来源:https://stackoverflow.com/questions/687267/programatically-adding-and-removing-events-from-a-gridview