469,331 Members | 6,696 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

Serialized class in web user control loses viewstate on postback

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

Apr 7 '06 #1
0 1296

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Guadala Harry | last post: by
2 posts views Thread by Sam Kuehn | last post: by
5 posts views Thread by Steve Richter | last post: by
5 posts views Thread by serge calderara | last post: by
5 posts views Thread by =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.