tags:

views:

48

answers:

1

I have an aspx page that contains a user control. The user control displays a business object and allows the user to edit the object. The user control has a property that holds the object being displayed.

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 feature to Widgets so that it raises an event under some circumstances. I want to handle that event in my user control.

How do I wire up the handler to the Property? Protected Sub WidgetHandler(ByVal sender as Object, ByVal e as WidgetEventArgs) Handles Me.Item.WidgetEvent doesn't work. It throws a serialization error indicating that the user control is not serializable. Widgets and WidgetEventArgs are marked Serializable. I tried Protected WithEvents myItem As Widget, changed the handler declaration to ...Handles myItem.WidgetEvent, and added myItem = value to the Item Setter, but that made no difference. I also tried using AddHandler in the setter but it didn't work either.

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

Thanks

+1  A: 

Any event would in the page would cause a PostBack. After a postback, the OnLoad event is triggered for every control in the page, including your UserControl and the widget itself. You could check which event was raised in the OnLoad event of your UserControl and look at the sender object; if it is your widget then you can act on that event.

I am not saying this is the best solution, but is the first one that comes to mind without actually trying to do it.

Victor