Click here to Skip to main content
15,860,972 members
Articles / Web Development / XHTML
Article

ViewState and Postback

Rate me:
Please Sign up or sign in to vote.
4.71/5 (38 votes)
12 Dec 2008CPOL6 min read 204.1K   67   37
Why some Web controls like Textbox retain values even after disabling the ViewState while others do not?

Introduction

The aim of this article is to clarify the question that many new Web developers might have about ViewState.

Why do some Web controls like Textbox retain values even after disabling the ViewState while others do not?

Background

Let’s build a simple Web application to examine how ViewState works.

Create a blank Web project and paste the code given below in the page:

ASP.NET
<script runat="server">
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
						Handles btnSubmit.Click
        lblMessage.Text = "Goodbye everyone"
        lblMessage1.Text = "Goodbye everyone"
        txtMessage.Text = "Goodbye everyone"
        txtMessage1.Text = "Goodbye everyone"
    End Sub
</script>
<form id="form1" runat="server">
  
   <asp:Label runat="server" ID="lblMessage" EnableViewState =true  
   Text="Hello World"></asp:Label>
   <asp:Label runat="server" ID="lblMessage1" EnableViewState =false  
   Text="Hello World"></asp:Label>
  <asp:Textbox runat="server" ID="txtMessage" EnableViewState =true  
   Text="Hello World"></asp:Textbox>
   <asp:Textbox runat="server" ID="txtMessage1" EnableViewState =false  
   Text="Hello World"></asp:Textbox>
<br />
<asp:Button runat="server" 
  Text="Change Message" ID="btnSubmit"></asp:Button>
<br />
<asp:Button ID="btnEmptyPostBack" runat="server" Text="Empty Postback"></asp:Button>
</form>

The page rendered will have four controls (two text boxes and two labels) initialized with Hello World and two buttons.

Click on the Change Message button, the value in controls will be changed to Goodbye Everyone.

Now click on the Empty Postback button.

The expected result is, after postback the Textbox (txtMessage) and label (lblMessage) with EnableViewState = false should not retain the value and hence the value should be Hello world, while the controls with ViewState enabled (txtMessage1 and lblMessage1) should retain the value and hence value should be <code><code><code><code><code><code><code><code>Goodbye world.

But this does not happen. Both the Textbox will maintain the value irrespective of whether ViewState is enabled or disabled, but in the case of label control if ViewState is disabled, the value we changed programmatically is not retained.

Let's examine why this happens?

Page LifeCycle and ViewState

In page life cycle, two events are associated with ViewState:

  • Load View State: This stage follows the initialization stage of page lifecycle. During this stage, ViewState information saved in the previous postback is loaded into controls. As there is no need to check and load previous data, when the page is loaded for the first time this stage will not happen. On subsequent postback of the page as there may be previous data for the controls, the page will go through this stage.
  • Save View State: This stage precedes the render stage of the page. During this stage, current state (value) of controls is serialized into 64 bit encoded string and persisted in the hidden control (__ViewState) in the page.
  • Load Postback Data stage: Though this stage has nothing to do with ViewState, it causes most of the misconception among developers. This stage only happens when the page has been posted back. ASP.NET controls which implement IPostBackEventHandler will update its value (state) from the appropriate postback data. The important things to note about this stage are as follows:
  1. State (value) of controls are NOT retrieved from ViewState but from posted back form.
  2. Page class will hand over the posted back data to only those controls which implement IPostBackEventHandler.
  3. This stage follows the Load View State stage, in other words state of controls set during the Load View State stage will be overwritten in this stage.

Answers

Now with the above information, let us try to answer the question:

Why some controls retain values even after disabling the ViewState while others do not?

The answer is Controls which implements IPostBackEventHandler like Textbox, Checkbox, etc. will retain the state even after disabling the viewstate. The reason is during the Load Postback Data stage, these controls will get state information from Posted back form.

But controls like label which do not implement IPostBackEventHandler will not get any state information from posted back data and hence depend entirely on viewstate to maintain the state.

Following are the changes in textbox during the page life cycle.

Textbox with ViewState Enabled

Page Events Page is visited for first time On click of “Change Message” button On click of “Empty Post back” button
Init

Textbox is set value Hello World

Textbox is set value Hello World

Textbox is set value Hello World

Load View State

Textbox is set with value Good Bye Everyone from ViewState <textbox>

Load Post back data stagePostback data is Hello World so Textbox is set with Hello WorldPostback data is Goodbye Everyone so Textbox is set with Goodbye Everyone
Controls Postback event (button click )

Textbox is set with Goodbye everyone

Save view stateHello World is saved to ViewStateGoodbye Everyone is saved to ViewStateGoodbye Everyone is saved to ViewState
RenderTextbox is rendered with text Hello worldTextbox is rendered with text Goodbye EveryoneTextbox is rendered with text Goodbye Everyone

Textbox with ViewState Disabled

Page Events Page is visited for first time On click of “Change Message” button On click of “Empty Post back” button
Init

Textbox is set value Hello World

Textbox is set value Hello World

Textbox is set value Hello World

Load View State

Textbox is set with value Good Bye Everyone from ViewState <textbox>

Load Post back data stagePostback data is Hello World so Textbox is set with Hello WorldPostback data is Goodbye Everyone so Textbox is set with Goodbye Everyone
Controls Postback event (button click )

Textbox is set with Goodbye everyone

Save view state
RenderTextbox is rendered with text Hello worldTextbox is rendered with text Goodbye EveryoneTextbox is rendered with text Goodbye Everyone

Label with ViewState Enabled

Page Events Page is visited for first time On click of “Change Message” button On click of “Empty Post back” button
Init

Label is set value Hello World

Label is set value Hello World

Label is set value Hello World

Load View State

Label is set with value Good Bye Everyone from ViewState <textbox>

Load Post back data stage
Controls Postback event (button click)

Label is set with Goodbye everyone

Save view stateHello World is saved to labelGoodbye Everyone is saved to ViewStateGoodbye Everyone is saved to ViewState
RenderLabel is rendered with text Hello worldLabel is rendered with text Goodbye EveryoneLabel is rendered with text Goodbye Everyone

Label with ViewState Disabled

Page Events Page is visited for first time On click of “Change Message” button On click of “Empty Post back” button
Init

Label is set value Hello World

Label is set value Hello World

Label is set value Hello World

Load View State
Load Post back data stage
Controls Postback event (button click )

Label is set with Goodbye everyone

Save view state
RenderLabel is rendered with text Hello worldLabel is rendered with text Goodbye EveryoneLabel is rendered with text Hello World

Point of Interest

An interesting behavior is if we make a control which implements IPostBackEventHandler interface disabled then the ASP.NET will not process the control during postback. So in the above sample, if we make the Textbox (one with EnableViewState = false) disabled then it will not retain the changed value and behave like a label control.

Conclusion

In this article, we examined how the ViewState of control is persisted during the life cycle of page and why some controls maintain the state even if the ViewState is disabled. Hope the information provided here is useful.

History

  • 12th December, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer CodeReflex Technologies
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiondisable button from postpack at textbox changed Pin
Member 119579615-Sep-15 8:41
Member 119579615-Sep-15 8:41 
QuestionGreat explanation, but i have one question Pin
trueknow13-Sep-14 17:15
trueknow13-Sep-14 17:15 
AnswerRe: Great explanation, but i have one question Pin
Member 1133197014-May-15 21:54
Member 1133197014-May-15 21:54 
QuestionNice Article Pin
Siva Hyderabad19-Jul-14 0:07
Siva Hyderabad19-Jul-14 0:07 
QuestionThanks Pin
ayman_mahmoud8225-May-14 11:21
ayman_mahmoud8225-May-14 11:21 
Questionwhat will trigger Load View State? Pin
Hoy Cheung14-Jan-13 14:02
Hoy Cheung14-Jan-13 14:02 
GeneralMy vote of 5 Pin
Samsani.v.s.Durga Prasad1-Oct-12 20:58
Samsani.v.s.Durga Prasad1-Oct-12 20:58 
good practical example
QuestionViewState-and-Postback Pin
pune6-Feb-12 1:23
pune6-Feb-12 1:23 
GeneralCalendar control Pin
bronnie9-Jun-11 7:18
bronnie9-Jun-11 7:18 
GeneralMy vote of 2 Pin
Ankur\m/12-Jan-11 2:07
professionalAnkur\m/12-Jan-11 2:07 
GeneralPostback is bad Pin
Xmen Real 28-Dec-10 0:35
professional Xmen Real 28-Dec-10 0:35 
GeneralCorrection in the article Pin
Rasik Bihari Tiwari25-Sep-10 17:39
professionalRasik Bihari Tiwari25-Sep-10 17:39 
GeneralRe: Correction in the article Pin
Ankur\m/12-Jan-11 2:00
professionalAnkur\m/12-Jan-11 2:00 
GeneralI doubt that the value of Text property of the TextBox is ever been written to the ViewState Pin
MeganPhan19-Aug-10 16:45
MeganPhan19-Aug-10 16:45 
GeneralRe: I doubt that the value of Text property of the TextBox is ever been written to the ViewState Pin
Hoy Cheung14-Jan-13 14:35
Hoy Cheung14-Jan-13 14:35 
GeneralRe: I doubt that the value of Text property of the TextBox is ever been written to the ViewState Pin
rdg51519-Dec-14 5:40
rdg51519-Dec-14 5:40 
GeneralExcellent article but 3 mistakes just in case somebody need accurate information Pin
rajesh erasani1-Feb-10 18:59
rajesh erasani1-Feb-10 18:59 
GeneralRe: Excellent article but 3 mistakes just in case somebody need accurate information Pin
Ankur\m/12-Jan-11 2:03
professionalAnkur\m/12-Jan-11 2:03 
GeneralRe: Excellent article but 3 mistakes just in case somebody need accurate information Pin
max042315-Dec-15 9:40
max042315-Dec-15 9:40 
GeneralSuperb article Pin
Member-495917620-Jul-09 20:21
Member-495917620-Jul-09 20:21 
QuestionGood explaination, but what can you do to stop it... Pin
rjp196612-Jun-09 4:06
rjp196612-Jun-09 4:06 
QuestionNice Article. What about sqldatasource Pin
Panayotis Matsinopoulos24-Dec-08 21:54
Panayotis Matsinopoulos24-Dec-08 21:54 
GeneralDig a little deeper Pin
Joel.Wilson18716-Dec-08 8:35
Joel.Wilson18716-Dec-08 8:35 
GeneralRe: Dig a little deeper Pin
Padmaraj Sivaraj16-Dec-08 19:30
Padmaraj Sivaraj16-Dec-08 19:30 
GeneralRe: Dig a little deeper Pin
max042315-Dec-15 9:41
max042315-Dec-15 9:41 

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.