Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ViewState and User Defined Data Type

0.00/5 (No votes)
29 Sep 2004 1  
How to play with ViewState and user defined datatype.

Introduction

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();
       this.ViewState["Key"]=UC;
       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.

       //

       InitializeComponent();
       base.OnInit(e);
      }
      private void InitializeComponent()
    {    
       this.Load += new System.EventHandler(this.Page_Load);
     }
    #endregion
 }

 public class UserClass
 {
      public UserClass()
    {
        _number=1;
        _name="cp.com";
      }
      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.

����������..
����������..
����������..

[Serializable()] 
 public class UserClass
 {
 
  public UserClass()
  {
    _number=1;
    _name="cp.com";
  }
  private int _number;
  private string _name;
 }

����������..
����������..
����������..

License

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