Handling events of usercontrols within listview

后端 未结 2 1021
孤街浪徒
孤街浪徒 2021-01-24 16:06

I have a simple usercontrol which raises an event on button click

Public Class UcPaymentCheque
    Inherits System.Web.UI.UserControl

    Public Event OnCancelC         


        
2条回答
  •  情深已故
    2021-01-24 16:21

    You can check out my response to a similar question here: creating and listening for events

    Essentially, you want a user control to raise its own event, like this:

    Partial Class myControl
        Inherits System.Web.UI.UserControl
        Public Event MyEvent As EventHandler
    
        'your button click event
        Protected Sub bnt_click(ByVal sender As Object, ByVal e As EventArgs)
          'do stuff
          'now raise the event
           RaiseEvent MyEvent (Me, New EventArgs)
        end sub
    end class
    

    In this example, I raise the event when the user clicks a button within the user control. You can easily raise the event anywhere, such as when the control loads, using a timer, whatever.

    Then, in the main page, you want to and an event handler to the user control, like this:

     
    

    Now, in the code behind, you can add the event, like this:

    Protected Sub myControl_MyEvent(ByVal sender As Object, ByVal e As EventArgs)
     'do stuff 
    end sub
    

提交回复
热议问题