Click here to Skip to main content
13,550,950 members
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 29 Sep 2004

ViewState and User Defined Data Type

, 29 Sep 2004
Rate this:
Please Sign up or sign in to vote.
How to play with ViewState and user defined datatype.


In this article, I would like to share some of the concepts about ViewState.

State Maintenance

The web is stateless because the HTTP protocol is a stateless protocol. By default, state is not maintained between different client requests to the server by the application. But some technologies are provided to maintain the state. We have three different types of state:

  1. Page level
  2. User level
  3. Application level

ViewState is used to maintain page level state. Rest of the two are out of this article.

In ASP.NET, Control.ViewState property has been given to each control for retaining its values between round trip server requests.

How the ViewState is working

If ViewState property of a web form is enabled then we can see a segment of HTML code in browser window by right clicking the mouse and selecting View Source. It would be like:

<input type="hidden" name="__VIEWSTATE" 

       value="DDDF22DDFD3453333D2DDDFDFD332133==" />

A hidden control is added to each page and all controls' data are stored and send to client’s browser. When page is reloaded, two methods of ViewState are called: LoadViewState() and SaveViewState(), and data will be loaded into corresponding controls.

What we can store in ViewState?

We can use the ViewState to store any type of object, if that object is serializable or is having TypeConverter. ‘Serialization is the process of converting object’s data into a stream based output, and using that output later we can construct the same object’. Here, we must be aware of where this serialization plays its role in ViewState. As early, we seen that controls' data are stored into a hidden field, before that the value or data is being serialized.

User Defined DataType

Users can define their own data types. Classes and structure definitions fall under user defined data types. If we want to play with user defined datatypes and ViewState, then really we need to concentrate on defining them.

Try the following code in a C#.NET web application:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CodeProject
 public class WebForm1 : System.Web.UI.Page
    private void Page_Load(object sender, System.EventArgs e)
        UserClass UC=new UserClass();
       Response.Write("View State is workng..");

      #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
       // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      private void InitializeComponent()
       this.Load += new System.EventHandler(this.Page_Load);

 public class UserClass
      public UserClass()
      private int _number;
      private string _name;

It shows the following error:

Sample screenshot

This is because the user defined class CodeProject.UserClass is not Serializable now. So an object of this type can’t be stored into viewstate.

Add Serializable attribute to class definition and run the code, it will work well.


 public class UserClass
  public UserClass()
  private int _number;
  private string _name;



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Technical Lead ITC Infotech India Ltd
India India

You may also be interested in...


Comments and Discussions

Questionviewstate Pin
MuraliKrishna120412-Mar-12 3:23
memberMuraliKrishna120412-Mar-12 3:23 
Generalstore address class object into viewstate Pin
deepalititi9-Aug-07 20:28
memberdeepalititi9-Aug-07 20:28 
GeneralRe: store address class object into viewstate [modified] Pin
Sendilkumar.M10-Aug-07 0:50
memberSendilkumar.M10-Aug-07 0:50 
Only the address class you have made as serializable. But you have SqlConnection instance inside the address class which is not actually serilizable.Your approach is wrong. Why you want to store SqlConnection class in to viewstate?

Still you want to store,implment ISerializable interface for address have to implement GetobjectData data of ISerializable and manually serialize using Binaryserializer store the serialized bytes into ViewState.When the object is construting back from viewstate you have to deserilize it.

You need a constructor for address class which takes two arguments SerializationInfo and streamingContext.

Implement GetObjectData - like put all information which you need to open sqlConnection and add to serilaliztionInfo.

Write Construtor - like get all information from seralizationInfo and open sqlConnection again

So when you are doing serialization GetObjectData will be called.
When you are doing deserialization Constructor will be called.
<br />
public void GetObjectData(SerializationInfo info, StreamingContext context)<br />
{<br />
   info.Add("CON_STR",connectionString);<br />
}<br />
<br />
public Address(SerializationInfo info, StreamingContext context)<br />
{<br />
     connectionString =(string)info.GetValue("CON_STR");<br />
}<br />

The same approach you have to follow when object is getting stored in Session and web application has no inproc session management.



QuestionNew Datatype Pin
subject_helper@rediffmail.com30-Apr-07 1:03
membersubject_helper@rediffmail.com30-Apr-07 1:03 
GeneralGood..... Pin
DelhiRainbow14-Sep-06 21:09
memberDelhiRainbow14-Sep-06 21:09 
GeneralCustom Control (viewstate) Pin
ruven.naidoo@gmail.com4-Oct-05 21:20
memberruven.naidoo@gmail.com4-Oct-05 21:20 
GeneralOther Article said no serialization Pin
Eka K.S20-May-05 18:13
sussEka K.S20-May-05 18:13 
GeneralThanks for your article Pin
lxw9912-Apr-05 15:24
memberlxw9912-Apr-05 15:24 
GeneralRe: Thanks for your article Pin
Anonymous15-Aug-05 2:03
sussAnonymous15-Aug-05 2:03 
Generalummmmmm... Pin
l a u r e n29-Sep-04 12:55
memberl a u r e n29-Sep-04 12:55 
GeneralRe: ummmmmm... Pin
Anonymous29-Sep-04 17:37
sussAnonymous29-Sep-04 17:37 
GeneralRe: ummmmmm... Pin
l a u r e n30-Sep-04 8:38
memberl a u r e n30-Sep-04 8:38 
GeneralRe: ummmmmm... Pin
Mtt28-Oct-04 1:55
memberMtt28-Oct-04 1:55 
GeneralRe: ummmmmm... Pin
Ian Michael14-Jan-05 4:48
sussIan Michael14-Jan-05 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180515.1 | Last Updated 29 Sep 2004
Article Copyright 2004 by Sendilkumar.M
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid