Click here to Skip to main content
Click here to Skip to main content

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.

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

Share

About the Author

Sendilkumar.M
Technical Lead ITC Infotech India Ltd
India India
*****
Follow on   Twitter

Comments and Discussions

 
Questionviewstate PinmemberMuraliKrishna120412-Mar-12 4:23 
Generalstore address class object into viewstate Pinmemberdeepalititi9-Aug-07 21:28 
GeneralRe: store address class object into viewstate [modified] PinmemberSendilkumar.M10-Aug-07 1:50 
QuestionNew Datatype Pinmembersubject_helper@rediffmail.com30-Apr-07 2:03 
GeneralGood..... PinmemberDelhiRainbow14-Sep-06 22:09 
GeneralCustom Control (viewstate) Pinmemberruven.naidoo@gmail.com4-Oct-05 22:20 
GeneralOther Article said no serialization PinsussEka K.S20-May-05 19:13 
GeneralThanks for your article Pinmemberlxw9912-Apr-05 16:24 
GeneralRe: Thanks for your article PinsussAnonymous15-Aug-05 3:03 
Generalummmmmm... Pinmemberl a u r e n29-Sep-04 13:55 
GeneralRe: ummmmmm... PinsussAnonymous29-Sep-04 18:37 
GeneralRe: ummmmmm... Pinmemberl a u r e n30-Sep-04 9:38 
GeneralRe: ummmmmm... PinmemberMtt28-Oct-04 2:55 
GeneralRe: ummmmmm... PinsussIan Michael14-Jan-05 5:48 

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

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

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