Serialized class in web user control loses viewstate on postback

Discussion in 'ASP .Net' started by bminder@gmail.com, Apr 7, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spam Catcher
    Replies:
    0
    Views:
    582
    Spam Catcher
    Feb 18, 2006
  2. Fahim
    Replies:
    0
    Views:
    465
    Fahim
    Aug 18, 2006
  3. Dwight Johnson
    Replies:
    0
    Views:
    2,710
    Dwight Johnson
    Oct 7, 2008
  4. Graham Stevenson

    Dropdownlist in Server Control loses Viewstate

    Graham Stevenson, Dec 1, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    134
    Guest
    Dec 3, 2003
  5. Robert Phillips

    Disabled Panel control loses viewstate

    Robert Phillips, Apr 28, 2004, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    271
    Scott Mitchell [MVP]
    Apr 28, 2004
Loading...

Share This Page