Click here to Skip to main content
6,306,412 members and growing! (17,428 online)
Email Password   helpLost your password?
Web Development » View State » Viewstate     Intermediate

Myth Regarding ViewState in ASP.NET

By Vivek Thakur

Common myth that ViewState holds values for controls such as TextBoxes.
C#, .NET CF, Mobile, .NET 1.1, Win2K, WinXP, Win2003, Vista, TabletPC, DotGNU, ASP.NET, WebForms, VS.NET2003, Dev
Posted:1 Jul 2005
Updated:6 Sep 2006
Views:65,746
Bookmarked:25 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
38 votes for this article.
Popularity: 5.35 Rating: 3.38 out of 5
7 votes, 18.4%
1
5 votes, 13.2%
2
1 vote, 2.6%
3
5 votes, 13.2%
4
20 votes, 52.6%
5

Introduction

Most ASP.NET developers think that the ASP.NET ViewState is responsible for holding the values of controls such as TextBoxes so that they are retained even after postback. But this is not the case.

I'll explain this with an example. You can download the project files from the link above (you can set the Virtual Directory by the name of ViewState) and run the application. Let's start with a web project in C#.

Open a new ASP.NET Web Application, and on the WebForm, place the following controls accordingly:

Place a web server TextBox control, an HTML server text box control (normal HTML input box with runat=server), a normal HTML input control, and a web server Label control. Name the four controls as:

  • txtWebServerTest
  • txtHtmlServerTest
  • txtHtmlTest
  • lblTest

Set the Text/Value property to �Initial Text� for the above three TextBoxes, and set the Text property of the Label control to �Initial Label Value�.

Set the EnableViewState property to false for the first and the last controls.

Beneath these controls, place two Button controls and set their text as �Change Label�s Text� and �Postback to Server�. On the button click event handler of the first button, write the following code:

private void btnChangeLabel_Click(object sender, System.EventArgs e)
{
    lblTest.Text = "Label's Text Changed";
}

There is no code on the second button�s Click event. It just submits the page to the server.

Now run this application. You can see the initial texts in the controls as you have set.

Now, change the text in all TextBoxes and set them to �Changed Text�. Now click the Post to Server button. What happens is that the first two textboxes retain their values, in spite of the ViewState property being set to false. But the last textbox, which is a simple HTML input control, loses the modified value when the page is reloaded.

Most developers would have expected all three textbox controls to lose their modified values (�Changed Text�), and after page re-loading, they expect �Initial Value� being written on all textboxes as we had disabled the ViewState.

The reason for this behavior is that ViewState is not responsible for storing the modified values for controls such as TextBoxes, dropdowns, CheckBoxList etc., i.e., those controls which inherit from the IPostBackDataHandler interface. After Page_Init(), there is an event known as LoadViewState, in which the Page class loads values from the hidden __VIEWSTATE from the field for those controls (e.g., Label) whose ViewState is enabled. Then the LoadPostBackData event fires, in which the Page class loads the values of those controls which inherit from the IPostBackDataHandler interface, (e.g., TextBox) from the HTTP POST headers.

Now, start the application again, and this time, click the �Change Label�s Text� button. When the page reloads, you will see that the programmatic change (made by our code in the event handler of the button�s Click event) was lost, i.e., we don�t see the Label�s text changed to �Label's Text Changed�. Instead, we see the initial value of the Label again. This is because the Label control does not inherit from the IPostBackDataHandler interface. So the ViewState is responsible for persisting its value across postbacks, and since it has been disabled, the Label loses its value after clicking the �Change Label�s Text� button.

Now enable the ViewState for the Label control, and you can see the modified value (�Label's Text Changed�) after clicking the same button.

So we conclude that controls which inherit from the IPostBackDataHandler interface will retain their values even if the ViewState has been disabled, as their values are stored in HTTP POST headers.

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

About the Author

Vivek Thakur


Member
Vivek Thakur is the admin and moderator of the popular ASP.NET community http://www.codeasp.net (CodeAsp.Net).

All his articles can be found at:

http://www.codeasp.net/people/vivek_iit#prArticles

Join the premiere ASP.NET community for free today:

http://www.codeasp.net/register

For questions/comments, do post them in http://www.codeasp.net/forums so that I can answer to each of them.
Occupation: Web Developer
Location: India India

Other popular View State articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
Generalquestion Pinmemberrawan awad alden7:39 17 Mar '09  
GeneralHow I change the Lable1.Text from javaScript. PinmemberRakesh Bhavsar5:36 4 Nov '08  
GeneralHi Vivek Pinmembersheetaloza198020:33 5 Mar '07  
GeneralIt's not magic... Pinmembershaul_ahuva7:02 8 Sep '06  
GeneralRe: It's not magic... PinmemberVivek Thakur2:31 12 Sep '06  
GeneralIf you want to truly understand ViewState PinmemberIgor Vigdorchik12:40 6 Sep '06  
GeneralRe: If you want to truly understand ViewState PinmemberLeaper5:55 19 Jul '07  
GeneralI cann't read text property Pinmembermfd118:58 16 Jan '06  
GeneralRe: I cann't read text property Pinmembervivekthakur2:29 18 Apr '06  
GeneralClose but not entirely accurate PinmemberShane Holder12:21 21 Aug '05  
GeneralRe: Close but not entirely accurate Pinmembervivekthakur22:39 1 Jan '06  
GeneralRe: Close but not entirely accurate Pinmembervik2020:08 6 Sep '06  
GeneralVivek thanx. Pinmemberdejawoo0:12 9 Jul '05  
GeneralRe: Vivek thanx. Pinmembervivekthakur2:20 11 Jul '05  
GeneralRe: Vivek thanx. PinmemberTony Belcher22:28 29 Jul '05  
GeneralControls which are not inherited IPostBackDataHandler/ IPostBackEventHandler Pinmemberpshinde@brickred.com1:56 1 Jul '05  
GeneralRe: Controls which are not inherited IPostBackDataHandler/ IPostBackEventHandler Pinmembervivekthakur2:11 1 Jul '05  
GeneralRe: Controls which are not inherited IPostBackDataHandler/ IPostBackEventHandler PinmemberGuenaVan1:22 16 Aug '05  
GeneralRe: Controls which are not inherited IPostBackDataHandler/ IPostBackEventHandler Pinmemberrnaravane20:08 17 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Sep 2006
Editor: Smitha Vijayan
Copyright 2005 by Vivek Thakur
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project