Click here to Skip to main content
15,868,164 members
Articles / Web Development / ASP.NET
Article

ViewState and User Defined Data Type

Rate me:
Please Sign up or sign in to vote.
2.94/5 (12 votes)
29 Sep 20042 min read 93K   19   14
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:

HTML
<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:

C#
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.

C#
…………………………..
…………………………..
…………………………..

[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


Written By
Technical Lead ITC Infotech India Ltd
India India
*****

Comments and Discussions

 
GeneralRe: ummmmmm... Pin
Anonymous29-Sep-04 17:37
Anonymous29-Sep-04 17:37 
GeneralRe: ummmmmm... Pin
l a u r e n30-Sep-04 8:38
l a u r e n30-Sep-04 8:38 
GeneralRe: ummmmmm... Pin
Mtt28-Oct-04 1:55
Mtt28-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.