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