How do I handle events from an object stored as a property of a custom control?

I have an aspx page that contains a custom control. The custom control renders the business object and allows the user to edit the object. The custom control has a property that contains the displayable.

Public Property Item() As Widget       
    Get
        If ViewState("Item") IsNot Nothing Then
            Return DirectCast(ViewState("Item"), Widget)
        End If
        Return Nothing
    End Get
    Private Set(ByVal value As Widget)
        ViewState("Item") = value
    End Set
End Property

      

I recently added a new function to widgets to trigger an event under certain circumstances. I want to handle this event in my user control.

How do I connect a handler to a property? Protected Sub WidgetHandler (Sender ByVal as Object, ByVal e as WidgetEventArgs) Handles Me.Item.WidgetEvent does not work. It throws a serialization error indicating that the custom control is not serializable. Widgets and WidgetEventArgs are marked Serializable. I tried Protected WithEvents myItem As Widget , changed the handler declaration to ... handled myItem.WidgetEvent, and added myItem = value to the Setter, but it didn't matter. I also tried to use AddHandler in the installer but it didn't work either.

What is the correct way to maintain a reference to the business object in my custom control so that I can handle its events?

thank

0


source to share


1 answer


Any event on the page will trigger PostBack. Upon postback, the OnLoad event fires for every control on the page, including your UserControl and the widget itself. You can check which event was raised in your UserControl's OnLoad event and see the sender object; if this is your widget you can act in this case.



I'm not saying this is the best solution, but the first thing that comes to mind without trying to do it.

+1


source







All Articles