programatically adding and removing events from a GridView

*爱你&永不变心* 提交于 2019-12-12 15:50:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!