Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why cannot Viewstate persist across pages ?

Posted on 2004-10-20
9
Medium Priority
?
807 Views
Last Modified: 2008-02-01
Hi,
I have a funfdamental doubt on Viewstates.

We know from traditional ASP that we can accesshidden fields on one page in the other page.
Now for ASP wweb server controls like labels whose values might be stored in Viewstates, why cannot we access these values on other pages ? The basic question that comes to my mind is that
the Viewstate is also internally stored as __VIEWSTATE hidden field. So how is it different in its accessibility on other pages ?

Any help is greatly appreciated.
0
Comment
Question by:Amit01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 6

Assisted Solution

by:ryerras
ryerras earned 75 total points
ID: 12361203
Hmm ok. Think about this way. When you have a TextBox server control on a page, if you look in the Code behind file, you will see the declaration of textbox as
protected System.Web.UI.WebControls.TextBox TextBox1;

So, from another class you can instantiate this WebForm1 class and access this TextBox1 control based on its access modifiers. Thats cool, since its just another class and another class variable.

Now think about ViewState. ViewState is never stored on the server. Its stored only on the browser. So, when you load a webpage, and submits, its viewstate is submitted along with in, then your code behind file would access that viewstate and do whatever. This is fine. So, as you can see, ViewState is available only when the postback is in progress, once the post back is over, viewstate is no longer there in terms of the server. Its only on the browser. I emphasize again, its stored only on the browser.

But where as for server controls, their declaration presents on the server, so you can use them on the server or another pages code behind. But, viewstate can not be accessed unless you do postback
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 90 total points
ID: 12361217
Viewstate is dedicated to the specific page for which it is defined.  If you are trying to pass values from one page to another, use the Session object instead.

The purpose of Viewstate is NOT to facilitate passing values between pages, but rather to hold the View State of the current page, so that that state can be preserved upon a PostBack of the page to the server, in order to handle Server Side event processing.

This is from the Microsoft MSDN page  :

"ViewState is the mechanism ASP.NET uses to keep track of server control state values that don't otherwise post back as part of the HTTP form. For example, the text shown by a Label control is saved in ViewState by default. As a developer, you can bind data or programmatically set the Label just once when the page first loads, and on subsequent postbacks, the label text will be repopulated automatically from ViewState. So in addition to less grunt work and less code, the benefit of ViewState is often fewer trips to the database.

How ViewState Works
There's really nothing magical about ViewState. It's a hidden form field managed by the ASP.NET page framework. When ASP.NET executes a page, the ViewState values from the page and all of the controls are collected and formatted into a single encoded string, and then assigned to the value attribute of the hidden form field (specifically, <input type=hidden>). Since the hidden form field is part of the page sent to the client, the ViewState value is temporarily stored in the client's browser. If the client chooses to post the page back to the server, the ViewState string is posted back too. You can actually see the ViewState form field and its postback value in Figure 2 above.

Upon postback, the ASP.NET page framework parses the ViewState string and populates the ViewState properties for the page and each of the controls. The controls, in turn, use the ViewState data to rehydrate themselves to their former state.

There are three more small, but useful things to know about ViewState.

You must have a server-side form tag (<form runat=server>) in your ASPX page if you want to use ViewState. A form field is required so the hidden field that contains the ViewState information can post back to the server. And, it must be a server-side form so the ASP.NET page framework can add the hidden field when the page is executed on the server.
The page itself saves 20 or so bytes of information into ViewState, which it uses to distribute PostBack data and ViewState values to the correct controls upon postback. So, even if ViewState is disabled for the page or application, you may see a few remaining bytes in ViewState.
In cases where the page does not post back, you can eliminate ViewState from a page by omitting the server side <form> tag. "

look at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp11222001.asp

for a more complete explanation of ViewState.

AW
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12361232
in general, FORGET (ABSOLUTELY FORGET) everything you ever knew about ASP, as it has no relevance whatsoever to ASP.NET.

AW
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Amit01
ID: 12362544
Well dwelling on my issue a little further, i came up with the following thoughts -

I am not thinking of the Viewstate in terms of ASP way , but I am like trying to digest the fact that
the Viewstate is nothing but a hidden field, base 64-bit encoded and encrypted and called __VIEWSTATE. So why do we have to treat the two differently ? Now,

1. A Hidden field, say <input type="hidden" name = "testme" runat = "server">  on page1 , say
    Page1.aspx, can I access this testme on Page2.aspx ?
    If I used method = "POST" and request.form on Page2.aspx, it did not return me anything.

    Well, if you can use it, could you let me know how. Also that would lead to another question -
    When you can access "testme", why do you say you cannot access "__VIEWSTATE" in a similar  
    way, which means you are using Viewstate across pages.
    Both are hidden fields, both are input server controls.

2. If the hidden field testme cannot be accessed from page2.aspx, then that answers my question, I
    guess.

WEll, the MSDN article that Arthur suggested , I went through it but besically I did not find much on hidden fields or a comparison between __VIEWSTATE field and any other hidden field.

Regards
Amit
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12363593
_Viewstate is not a server control. Does the msdn saysy so. Can you please provide the links
0
 
LVL 3

Assisted Solution

by:nitrogenx
nitrogenx earned 60 total points
ID: 12364867
Well Viewstate contains specific information about the controls on your page.  If you have some reason for wanting to access the viewstate you can submit the page via post to another page and then access it with the Request.Form["__ViewState"] command.  I cant think of a good reason to do this, but it is possible.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12366245
did you not read the post from MSDN that I quoted, above:

"It's a hidden form field managed by the ASP.NET page framework"  note the phrase MANAGED BY THE ASP.NET PAGE FRAMEWORK.  

This means that ViewState is NOT a normal, run-of-the-mill <hidden> field, and cannot be treated as if it were.

Stop trying to force ASP.NET into your understanding of ASP.  That will never get you anywhere.

AW
0
 
LVL 3

Expert Comment

by:nitrogenx
ID: 12366320
<input type="hidden" name="__VIEWSTATE" value="/VudG...lsIG==" />

That right there is a hidden field, you CAN pass it with Request.Form["__VIEWSTATE"] I HAVE done it. Now like i just said I am doubtful that you can find any useful purpose for it, but it IS possible.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question