Nannette Thacker ShiningStar.net

ASP.net Web Application Development

Sponsors

News

See all Blog Posts by Nannette.

Nannette Thacker, consultant and owner of Shining Star Services LLC, specializes in development of custom dynamic database driven web applications utilizing ASP.net technologies. Nannette has been developing ASP sites since 1997. Nannette has written numerous articles on web development techniques and tutorials.

Nannette is the owner and developer of ChristianSinglesDating.com.

 Subscribe in a reader




Storing and Retrieving Objects from View State - The Serializable Attribute

View State allows you to retain page property values, such as string and numeric types, between postbacks. You may also store class objects in View State, but you must first add the Serializable attribute. If you do not add the Serializable attribute, you will receive this error when trying to add the object to View State:

Type 'SuchAndSuch.ThisAndThat' in Assembly 'SuchAndSuch, Version 1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 

Here is an example of adding Serializable to a class:

<Serializable()>_
Public Class aMenu
   Public MenuName As String
   Public MenuId as Integer
   Public Url as String

  Public Sub New(ByVal menuName as String, ByVal menuId as Integer, ByVal url as String)

   MenuName = menuName
   MenuId = menuId
   Url = url

  End Sub

End Class


The aMenu class can now be added to View State:

Dim myMenu as New aMenu("Home",1,"/default.aspx")
ViewState("myMenu") = myMenu

To use the aMenu object:

If ViewState("myMenu") IsNot Nothing then
   Dim myMenu as aMenu
   myMenu = DirectCast(ViewState("myMenu"),aMenu)
End If

May your dreams be in ASP.NET!

Nannette Thacker

Comments

Storing and Retrieving Objects from View State - The Serializable Attribute - Nannette Thacker ShiningStar.net said:

Pingback from  Storing and Retrieving Objects from View State - The Serializable Attribute - Nannette Thacker ShiningStar.net

# March 10, 2009 8:57 AM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# March 10, 2009 10:44 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)