Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class UserControl1 ' Parent
- Protected Override Function OnBubbleEvent(sender as Object, e as EventArgs) as Boolean
- Dim c as CommandEventArgs = TryCast(e, CommandEventArgs)
- If c IsNot Nothing Then
- RaiseEvent ItemEvent(sender, c)
- Return True ' Cancel the bubbling, so it doesn't go up any further in the hierarchy
- End If
- Return False ' Couldn't handle, so let it bubble
- End Function
- Public Event ItemEvent as EventHandler(Of CommandEventArgs)
- End Class
- Class UserControlB ' Child
- Protected Sub OnClicked(e as EventArgs)
- ' Raise a direct event for any handlers attached directly
- RaiseEvent Clicked(Me, e)
- ' And raise a bubble event for parent control
- RaiseBubbleEvent(Me, New CommandEventArgs("Clicked", Nothing))
- End Sub
- Protected Sub OnMoved(e as EventArgs)
- ' Raise a direct event for any handlers attached directly
- RaiseEvent Moved(Me, e)
- ' And raise a bubble event for parent control
- RaiseBubbleEvent(Me, New CommandEventArgs("Moved", Nothing))
- End Sub
- End Class
- Class PageA
- Sub UserControl1_ItemEvent(sender as Object, e as CommandEventArgs) Handles UserControl1.ItemEvent
- Response.Write(sender.GetType().Name & " was " & e.CommandName)
- End Sub
- End Class
- Class PageA
- Protected Override Function OnBubbleEvent(sender as Object, e as EventArgs) as Boolean
- If sender Is UserControlB Then
- Dim c as CommandEventArgs = TryCast(e, CommandEventArgs)
- If c IsNot Nothing Then
- Response.Write(sender.GetType().Name & " was " & c.CommandName)
- Else
- Response.Write(sender.GetType().Name & " raised an event, with " & e.GetType().Name & " args)
- End If
- Return True ' Cancel the bubbling, so it doesn't go up any further in the hierarchy
- End If
- Return False ' Not handled
- End Function
- End Class
- Public Partial Class TestPage
- Inherits Page
- Public Sub PerformAction()
- 'Whatever needs to be done on Page A
- End Sub
- End Class
- Public Partial Class TestControl
- Inherits UserControl
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- 'This will call the Page, obviously this will only
- 'work when the control is on TestPage
- CType(Page, TestPage).PerformAction()
- End Sub
- End Class
Add Comment
Please, Sign In to add comment