CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Grant Killian's Blog

No, this has nothing to do with beer -- but maybe it should?

Viewing ViewState

My ASP.Net class is just getting underway and we're getting into the topic of state management.  We briefly covered the intrinsic Application and Session objects as State repositories, and ViewState was brought into the conversation.  ViewState plays an important role in maintaining control values (a.k.a state) between postbacks of your ASP.Net pages.  Let's learn a bit more about ViewState by decoding it in a simple web application.

I added a textbox, a button, and a datagrid to a webform and added the following code to the page (be sure to make the textbox TextMode="MultiLine" for ease of reading):

  private void Page_Load(object sender, System.EventArgs e)
  {
   string cn = "server=server;database=northwind;uid=sa;pwd=password;";
   string sql = "SELECT TOP 5 * FROM Customers";
   DataGrid1.DataSource = SqlHelper.ExecuteDataset( cn, CommandType.Text, sql );
   DataGrid1.DataBind();
  }

  //writes the decoded viewstate to the textbox
  protected void decodeViewState()
  {
   string strV = HttpContext.Current.Request[ "__VIEWSTATE" ].ToString();
   byte[] arrBytes = System.Convert.FromBase64String( strV );
   ASCIIEncoding enc= new ASCIIEncoding();
   char[] charArray = new char[ enc.GetCharCount( arrBytes, 0, arrBytes.Length ) ];
   enc.GetDecoder().GetChars( arrBytes, 0, arrBytes.Length,charArray, 0 );
   foreach( char c in charArray )
   {
    Textbox1.Text += c;
   }
  }

  private void Button1_Click(object sender, System.EventArgs e)
  {
   decodeViewState();
  }

I should note I also added a reference to the MSFT Data Access Application block (that's where the SqlHelper class originates) and set the following using statements for convenience:
using Microsoft.ApplicationBlocks.Data;
using System.IO;
using System.Text;

When this page loads, our datagrid displays the first 5 rows from the Customers table in the NorthWind database.  If we view the generated HTML source for this page, we can see the viewstate stored in a hidden HTML input tag; the HTML will look something like the following (I deleted a lot of the value since I think you get the idea):

INPUT type=hidden value=dDwxOTI0OTQwNjAwO3Q8O2w8aTwxPjs+iAzNCA2Nzs+Pjs+Ozs+Oz4+Oz4+Oz4+Oz4+Oz4+Oz4W9zriiE/kuozP9dKgDteQYb9P5w== name=__VIEWSTATE

If we press our Button1 (and invoke the decodeViewState method), our textbox will display the decoded version of the viewstate (I chose to NOT delete any of the decoded text so you could see the whole thing):

t<1924940600;t<;l;>;l;>;l;l;i<5>;i<1>;i<5>;>>;>;;;;;;;;;@0<@0;l;>>;;;;>;@0;l;>>;;;;>;@0;l;>>;;;;>;@0;l;>>;;;;>;@0;l;>>;;;;>;@0;l;>>;;;;>;@0;l;>>;;;;>;@0;l;>>;;;;>;@0;l;>>;;;;>;@0;l;>>;;;;>;@0;l;>>;;;;>;>;>;l;>;l;i<2>;i<3>;i<4>;i<5>;>;l;i<1>;i<2>;i<3>;i<4>;i<5>;i<6>;i<7>;i<8>;i<9>;i<10>;>;l;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l< \;;>>;>;;>;t;l<12209;>>;>;;>;t;l>;>;;>;t;l<030-0074321;>>;>;;>;t;l<030-0076545;>>;>;;>;>>;t<;l;i<1>;i<2>;i<3>;i<4>;i<5>;i<6>;i<7>;i<8>;i<9>;i<10>;>;l;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l< \;;>>;>;;>;t;l<05021;>>;>;;>;t;l>;>;;>;t;l<(5) 555-4729;>>;>;;>;t;l<(5) 555-3745;>>;>;;>;>>;t<;l;i<1>;i<2>;i<3>;i<4>;i<5>;i<6>;i<7>;i<8>;i<9>;i<10>;>;l;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l< \;;>>;>;;>;t;l<05023;>>;>;;>;t;l>;>;;>;t;l<(5) 555-3932;>>;>;;>;t;l< \;;>>;>;;>;>>;t<;l;i<1>;i<2>;i<3>;i<4>;i<5>;i<6>;i<7>;i<8>;i<9>;i<10>;>;l;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l<120 Hanover Sq.;>>;>;;>;t;l>;>;;>;t;l< \;;>>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l<(171) 555-7788;>>;>;;>;t;l<(171) 555-6750;>>;>;;>;>>;t<;l;i<1>;i<2>;i<3>;i<4>;i<5>;i<6>;i<7>;i<8>;i<9>;i<10>;>;l;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l< \;;>>;>;;>;t;l>;>;;>;t;l>;>;;>;t;l<0921-12 34 65;>>;>;;>;t;l<0921-12 34 67;>>;>;;>;>>;>>;>>;>>;>>;>?:??O?????????a?O?

As we see, the ViewState is simply base64 encoded text; some of it is hard to read even after the unecoding with all the angled brackets and semi-colons, but the data is all apparent.  The ASP.Net plumbing uses this to track state data (such as location in a paged datagrid, make datagrid rows available in PostBack event handling, etc.).  You will may never need to decode ViewState like I've done, but it is good to understand that there is no magic to it.  It's just some encoded text storing properties and data related to the controls on your page.  I should point out that you can add your own data to ViewState (as an alternative to Session state -- but with a different set of pros and cons); you can also encrypt ViewState using the machine.config file on the server.  These are both topics for another blog.

If you can't wait of another blog on ViewState, here are some additional resources . . .

Back in June, Chris Hale did an overview of ViewState for WeProgram.Net.  Those materials are available here (at the bottom of the page).  For a solid MSFT treatment of the subject, including how to tune or disable ViewState to improve performance, check out this link: http://msdn.microsoft.com/netframework/downloads/samples/default.aspx?pull=/library/en-us/dnaspnet/html/asp11222001.asp.

Happy .Netting!



Comments

test said:

this is test

# October 16, 2006 7:23 AM

hassan said:

value="dDw5NDE2Mzk4MTQ7dDxwPGw8aGViZG9BZFRpdGxlOz47bDxDSEVWUk9MRVQgQ0FWQUxJRVIgMTkwMCAtIDE5OTggOz4+O2w8aTwxPjs+O2w8dDw7bDxpPDA+O2k8Mj47aTwzPjtpPDU+O2k8Nz47aTw5PjtpPDExPjtpPDEzPjtpPDE0PjtpPDE2PjtpPDE3PjtpPDE4PjtpPDE5PjtpPDIwPjtpPDIxPjtpPDIyPjtpPDIzPjtpPDI0PjtpPDI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thdHJhCUtoYWp1cmFobwhLb2xoYXB1cgdLb2xrYXR

hBEtvdGEJS296aGlrb2RlBEt1bHUDTGVoCExpbGFiYXJ5B0x1Y2tub3cITH

VkaGlhbmEHTWFkdXJhaQlNYW5nYWxvcmUGTXVtYmFpBk5hZ3B1cgVOYXNpa

wlOZXcgRGVsaGkFUGF0bmEJUG9yYmFuZGFyClBvcnQgQmxhaXIEUHVuZQtQ

dXR0YXByYXRoZQZSYWlwdXILUmFqYWhtdW5kcnkGUmFqa290BlJhbmNoaQh

TaGlsbG9uZwdTaWxjaGFyBVNpbWxhCFNyaW5hZ2FyBVN1cmF0BlRlenB1cg

RUZXp1DlRpcnVjaGlyYXBhbGx5CFRpcnVwYX".

--> I am facing this issue when a page is getting loaded.

What is the reason ? Can anyone help !!

# February 7, 2007 12:22 AM

Max said:

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

id="__VIEWSTATE" value="/wEPDwUKLTQzOTc3MDU2OA9kFgICAw9kFgxmD2QWBgIDDxYCHgdW

aXNpYmxlaGQCBA8WAh8AaGQCCA8WAh4Fc3R5bGUFDWRpc3BsYXk6bm9uZTt

kAgMPEA8WBh4NRGF0YVRleHRGaWVsZAUETmFtZR4ORGF0YVZhbHVlRmllbG

QFBENvZGUeC18hRGF0YUJvdW5kZ2QQFV8PU2VsZWN0IGxvY2F0aW9uBi0tL

S0tLQlCYW5nYWxvcmUGQm9tYmF5CENhbGN1dHRhBURlbGhpA0dvYQlIeWRl

cmFiYWQHQ2hlbm5haQYtLS0tLS0IQWdhcnRhbGENQWdhdHRpIElzbGFuZAR

BZ3JhCUFobWVkYWJhZAZBaXphd2wJQWxsYWhhYmFkCEFtcml0c2FyCkF1cm

FuZ2FiYWQIQmFnZG9ncmEJQmFuZ2Fsb3JlB0JlbGdhdW0JQmhhdm5hZ2FyB

kJob3BhbAxCaHViYW5lc2h3YXIEQmh1agpDaGFuZGlnYXJoB0NoZW5uYWkG

Q29jaGluCkNvaW1iYXRvcmUIRGVocmFkdW4JRGlicnVnYXJoB0RpbWFwdXI

DRGl1BEdheWEDR29hCUdvcmFraHB1cghHdXdhaGF0aQdHd2FsaW9yBUh1Ym

xpCUh5ZGVyYWJhZAZJbXBoYWwGSW5kb3JlCEphYmFscHVyBkphaXB1cglKY

WlzYWxtZXIFSmFtbXUISmFtbmFnYXIHSm9kaHB1cgZKb3JoYXQLS2FpbGFz

aGFoYXIGS2FucHVyBUthdHJhCUtoYWp1cmFobwhLb2xoYXB1cgdLb2xrYXR

hBEtvdGEJS296aGlrb2RlBEt1bHUDTGVoCExpbGFiYXJ5B0x1Y2tub3cITH

VkaGlhbmEHTWFkdXJhaQlNYW5nYWxvcmUGTXVtYmFpBk5hZ3B1cgVOYXNpa

wlOZXcgRGVsaGkFUGF0bmEJUG9yYmFuZGFyClBvcnQgQmxhaXIEUHVuZQtQ

dXR0YXByYXRoZQZSYWlwdXILUmFqYWhtdW5kcnkGUmFqa290BlJhbmNoaQh

TaGlsbG9uZwdTaWxjaGFyBVNpbWxhCFNyaW5hZ2FyBVN1cmF0BlRlenB1cg

RUZXp1DlRpcnVjaGlyYXBhbGx5CFRpcnVwYX".

--> I am facing this issue when a page is getting loaded.

What is the reason ? Can anyone help !!

# February 7, 2007 12:22 AM

Max said:

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

id="__VIEWSTATE" value="/wEPDwUKLTQzOTc3MDU2OA9kFgICAw9kFgxmD2QWBgIDDxYCHgdW

aXNpYmxlaGQCBA8WAh8AaGQCCA8WAh4Fc3R5bGUFDWRpc3BsYXk6bm9uZTt

kAgMPEA8WBh4NRGF0YVRleHRGaWVsZAUETmFtZR4ORGF0YVZhbHVlRmllbG

QFBENvZGUeC18hRGF0YUJvdW5kZ2QQFV8PU2VsZWN0IGxvY2F0aW9uBi0tL

S0tLQlCYW5nYWxvcmUGQm9tYmF5CENhbGN1dHRhBURlbGhpA0dvYQlIeWRl

cmFiYWQHQ2hlbm5haQYtLS0tLS0IQWdhcnRhbGENQWdhdHRpIElzbGFuZAR

BZ3JhCUFobWVkYWJhZAZBaXphd2wJQWxsYWhhYmFkCEFtcml0c2FyCkF1cm

FuZ2FiYWQIQmFnZG9ncmEJQmFuZ2Fsb3JlB0JlbGdhdW0JQmhhdm5hZ2FyB

kJob3BhbAxCaHViYW5lc2h3YXIEQmh1agpDaGFuZGlnYXJoB0NoZW5uYWkG

Q29jaGluCkNvaW1iYXRvcmUIRGVocmFkdW4JRGlicnVnYXJoB0RpbWFwdXI

DRGl1BEdheWEDR29hCUdvcmFraHB1cghHdXdhaGF0aQdHd2FsaW9yBUh1Ym

xpCUh5ZGVyYWJhZAZJbXBoYWwGSW5kb3JlCEphYmFscHVyBkphaXB1cglKY

WlzYWxtZXIFSmFtbXUISmFtbmFnYXIHSm9kaHB1cgZKb3JoYXQLS2FpbGFz

aGFoYXIGS2FucHVyBUthdHJhCUtoYWp1cmFobwhLb2xoYXB1cgdLb2xrYXR

hBEtvdGEJS296aGlrb2RlBEt1bHUDTGVoCExpbGFiYXJ5B0x1Y2tub3cITH

VkaGlhbmEHTWFkdXJhaQlNYW5nYWxvcmUGTXVtYmFpBk5hZ3B1cgVOYXNpa

wlOZXcgRGVsaGkFUGF0bmEJUG9yYmFuZGFyClBvcnQgQmxhaXIEUHVuZQtQ

dXR0YXByYXRoZQZSYWlwdXILUmFqYWhtdW5kcnkGUmFqa290BlJhbmNoaQh

TaGlsbG9uZwdTaWxjaGFyBVNpbWxhCFNyaW5hZ2FyBVN1cmF0BlRlenB1cg

RUZXp1DlRpcnVjaGlyYXBhbGx5CFRpcnVwYX".

--> I am facing this issue when a page is getting loaded.

What is the reason ? Can anyone help !!

# February 7, 2007 12:22 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!