“Re-Raising” Custom Events in .NET (ASP.NET in this case)

I'm working on an ASP.NET page, using VB.NET and I have this hierarchy:

Page A   - Web User Control 1     - Web User Control A     - Web User Control B     - Web User Control C

I need to raise an event from Web User Control B that Page A will receive (the event flow will be Web User Control B -> Web User Control 1 -> Page A).

My only approach so far has been this: 1) Add a custom event declaration to both Web User Control B and Web User Control 1 and simply RaiseEvent twice until it gets to Page A (this seems ugly and I don't particularly like it).

My other idea was to create a custom Event class that inhertis from some magical base Event class and create an instance of it in both Web User Control B and Web User Control 1, but that is proving fruitless because I can't find any event base classes (maybe b/c they're aren't any, since it appears to be a keyword, not a class name).

Any help would be appreciated! Thanks and happy coding!

Answers


You can use the BubbleEvent concept to do this. A BubbleEvent goes up the control hierarchy until someone handles it. The GridView and Repeater controls do this with their Row/ItemCommand events.

You could implement it into WebUserControl1, turning it into a standard event for the page (like the GridView does):

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

Or, do it directly in the page. UserControlB (Child) is the same as above, and UserControl1 (Parent) doesn't need to do anything special - OnBubbleEvent defaults to returning False, so the event bubbles up:

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

If your initial event is from a server control (like a Button.Click), then it will have been coded to already raise the bubble event - so UserControlB (Child) doesn't need to do anything to get that to the parent either. You just need to call RaiseBubbleEvent for any of your custom events, or if you want to transform the EventArgs in some way.


Need Your Help

SQL Hybrid Connection Error - An attempt was made to access a socket in a way forbidden by its access permissions

sql-server asp.net-mvc azure biztalk

I have a website setup in Azure Web Apps with a Biz Talk Hybrid connection to My In House SQL Database.

C linux regex performance issue

c regex linux cpu-usage

I'm creating a program that reads file line by line, match that line to regex and diplay how many lines has matched that regex. The problem is, this program is using pretty great precentage of CPU....

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.