Serialized class in web user control loses viewstate on postback

B

bminder

I have a web user control that contains a serializable class. I persist
this class in viewstate, but on postback, the viewstate is null. Is
this behavior by design?

This technique works fine on regular pages -- just not in a control.

Is the main page's viewstate interfering with the control's viewstate?

Thanks,

Brent

Here's a simplified version of the control's code:
--------------------------------------------------
Partial Class WebUserControl
Inherits System.Web.UI.UserControl

<Serializable()> _
Friend Class _ThisPage
Friend _FirstName As String
Friend _LastName As String
End Class
Friend ThisPage As _ThisPage

Public WriteOnly Property LastName() As String
Set(ByVal value As String)
ThisPage._LastName = value
End Set
End Property

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
If IsPostBack Then
ThisPage = DirectCast(ViewState("ThisPage"), _ThisPage)
'ViewState("ThisPage") is nothing on postback. Why?
Else
ThisPage = New _ThisPage
End If
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ThisPage._FirstName = "Sandra"
ViewState.Add("ThisPage", ThisPage) 'ViewState("ThisPage")
is valid at this point.
End If
lblName.Text = ThisPage._FirstName & " " & ThisPage._LastName
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top