Click here to Skip to main content
15,891,431 members
Articles / Web Development / ASP.NET
Tip/Trick

Are Viewstate and Postback Data the Same?

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
3 Jan 2015CPOL4 min read 17.8K   8   2
In this tip, we will try to understand whether viewstate and postback data are the same or different.

Introduction

In this tip, we will try to understand whether viewstate and postback data are the same or different.

Background

Recently, I was faced with the question "Are viewstate and postback data the same?" What will be your answer to this question? Same? Different? Well, choose your answer and proceed with the tip, where we explore the details.

Using the Code

We will be using ASP.NET Web Forms application in this post. Create a new application and remove all the contents from Default.aspx and add the following controls to the form:

  1. A label and change its Text to something out of the default (I changed it to "Initial Value")
  2. A textbox and
  3. 2 buttons. I have changed text of one button to "Change Text" and the other one to "Click Me". These are random names and you can name your button the way you want to.

After all these changes, your form should look like this:

Image 1

Now let's add some code to the "Change Text" button event handler. This is to change the text of the label and the textbox.

C#
protected void btnChange_Click(object sender, EventArgs e)
{
    lblCaption.Text = "New Lable caption";
    txtInput.Text = "New Text value";
}

Double click the "Click" button to generate the event handler. No need to add any code to the handler. Just leave it empty.

Viewstate is enabled by default for all the server controls. You might already know this, but just giving a heads up. Now run the application.

Image 2

When the UI comes up, Click "Change Text" button to change the text of label and textbox.

Image 3

Next, click the "Click Me" button, to posted back the form. When the form loads after postback, we can notice the changed values are still intact. Does this mean, Viewstate data and Postback data are the same?

Let's go one step further and disable viewstate of the textbox:

ASP.NET
<asp:TextBox ID="txtInput" runat="server" 
EnableViewState="false"></asp:TextBox>

Run the application and click "Change Text" button to change the text of label and text box. Then click "Click Me" button to postback the form.

Again, we notice the changed values are intact for both label and textbox when the form loads after postback. For label, viewstate is enabled so we understand it is getting its value from viewstate. But from where the textbox gets its changed value even though its viewstate is disabled? Does this mean viewstate and postback data are different?

Before jumping into any conclusion, let's disable the viewstate for the label as well and run the application.

Press F5 to run the application and click "Change Text" button. The texts are changed as expected.

Image 4

Next, click "Click Me" button. Now we can see, when the form loads after postback, the changed value is retained for the textbox, not for the label.

Image 5

As a final step before concluding this tip, in order to understand this thing a little better, let's dig into the page trace. Enable tracing for the page by setting Trace="true" in the page directive.

Run the application. Trace will be displayed on the page along with the UI. Go to the section "Form Collection" of the trace. When the page is initially loaded, it will be empty as shown in the screenshot below.

Image 6

Now, click "Change Text" button, and see what "Form Collection" contains. As we can see in the below screenshot, it contains Viewstate, along with it we also see two other items of our interest.

  1. ctl00$MainContent$txtInput - This contains the value of the text box (it is empty as the initial values of the text is empty)
  2. ctl00$MainContent$btnChange - This shows the control that caused the postback

Image 7

Then click "Click Me" button and check the values. Now we can see:

  1. ctl00$MainContent$txtInput - has the new value of the text box which is "New Text value"
  2. ctl00$MainContent$btnClick - This shows the control that caused the postback. Notice the Name and Value has changed.

Image 8

This shows that Viewstate and postback data are different.

This also answers the question "where the textbox gets its changed value even though its viewstate is disabled?" It gets its values from postback data.

Points of Interest

Although this tip shows that viewstate and postback data are different, it is to be noted that viewstate is sent as part of page postback information when the form is posted to the server. I have tried my best to share my understanding in this tip. If there is anything I missed or quoted incorrectly, please feel free to share in the comments section. This will help me as well as other readers to get a better understanding.

This is a vast area, and I have covered only a small part of it. The below articles may help you understand this topic better:

History

  • 01/04/2015 - First version

License

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


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
JayantaChatterjee6-Jan-15 17:06
professionalJayantaChatterjee6-Jan-15 17:06 
GeneralRe: My vote of 5 Pin
Mathi Mani6-Jan-15 19:16
Mathi Mani6-Jan-15 19:16 
Thanks JCahyaatnttearjee.

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.