ViewState data is data that ASP.NET encoded end sent to the client
in the _ViewState hidden field. It's basically the page as
it was when it was sent to the client.
PostBack data is data
that the user submits.
For example suppose you
have a have a textbox on a page defined like so:
<asp:TextBox id="TextBox1" runat="server" text="Some Text" />
You type in My user input into
the textbox and submit the form. Some Text would
be ViewState data and My user
input would be the
PostBack data.
If you set view state of the page to false than check b/w postback view state value( in the View source of page) will always be same no matter how many time you post back same page.
Each server control manages to its own information after the post back since it manages in View state so next in
Reference ViewState and Postback
<asp:Label runat="server" ID="lblMessage" EnableViewState =true
protected void btnSubmit_Click(object sender, EventArgs e)
If you set view state of the page to false than check b/w postback view state value( in the View source of page) will always be same no matter how many time you post back same page.
Each server control manages to its own information after the post back since it manages in View state so next in
Load
|
During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
|
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:
<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 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 encodedstring
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 implementIPostBackEventHandler
will update its value (state
) from the appropriate postback data. The important things to note about this stage are as follows:
- State (
value
) of controls are NOT retrieved fromViewState
but from posted back form. - Page class will hand over the posted back data to only those controls which implement
IPostBackEventHandler
. - 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.
Post back data is what user enter in the Text box.
<asp:Label runat="server" ID="lblMessage" EnableViewState =true
Text="Hello World"></asp:Label>
<asp:Textbox runat="server" ID="txtMessage" EnableViewState =true
Text="Hello World"></asp:Textbox>
We set some default text to both text box and Label, When get request goes to the server for this page PLC occurs and on SaveViewState event View state is saved in the form of hidden filed, If you view source of page for the first time when request the page than you can see values in the hidden field. For the first time Load View State stage and Load Postback Data stage will not happen, only Save View state happens
Now when user click on button(If link button then request will be get for the new page) or submit the page than IsPostBack becomes true again page life cycle occurres again all the event take place. Now
Load View State stage and Load Postback Data stage ,Save View state happens.
<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>
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblMessage.Text = "Goodbye everyone";//state false
lblMessage1.Text = "Goodbye everyone";//True
txtMessage.Text = "Goodbye everyone";//
txtMessage1.Text = "Goodbye everyone";
}
If submit is clicked than event occurs and set to the new values. but when click on emptypostback than Label with VS true will get its previews value and other lose its values. but for read full article.
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 implementsIPostBackEventHandler
likeTextbox
,Checkbox
, etc. will retain the state even after disabling theviewstate
. 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 implementIPostBackEventHandler
will not get any state information from posted back data and hence depend entirely onviewstate
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 | ||
Load Post back data stage | Postback data is Hello World so Textbox is set with Hello World | Postback data is Goodbye Everyone so Textbox is set withGoodbye Everyone | |
Controls Postback event (button click ) | Textbox is set with Goodbye everyone | ||
Save view state | Hello World is saved toViewState | Goodbye Everyone is saved to ViewState | Goodbye Everyone is saved toViewState |
Render | Textbox is rendered with textHello world | Textbox is rendered with text Goodbye Everyone | Textbox is rendered with textGoodbye 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 | ||
Load Post back data stage | Postback data is Hello World so Textbox is set with Hello World | Postback data is Goodbye Everyone so Textbox is set withGoodbye Everyone | |
Controls Postback event (button click ) | Textbox is set with Goodbye everyone | ||
Save view state | |||
Render | Textbox is rendered with textHello world | Textbox is rendered with text Goodbye Everyone | Textbox is rendered with textGoodbye 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 valueHello 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 | ||
Load Post back data stage | |||
Controls Postback event (button click) | Label is set withGoodbye everyone | ||
Save view state | Hello World is saved to label | Goodbye Everyone is saved to ViewState | Goodbye Everyone is saved toViewState |
Render | Label is rendered with text Hello world | Label is rendered with text Goodbye Everyone | Label is rendered with textGoodbye 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 valueHello World | Label is set value Hello World | Label is set valueHello World |
Load View State | |||
Load Post back data stage | |||
Controls Postback event (button click ) | Label is set with Goodbye everyone | ||
Save view state | |||
Render | Label is rendered with text Hello world | Label is rendered with textGoodbye Everyone | Label is rendered with text Hello World |
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 withEnableViewState = false
) disabled then it will not retain the changed value and behave like a label
control.
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.
Please go to this link and check the correction in this article
ReplyDeletehttp://www.codeproject.com/Articles/31701/ViewState-and-Postback