Solved

How can I access ViewState in a class other than a code-behind file?

Posted on 2012-03-17
3
2,800 Views
Last Modified: 2012-03-26
For both Session State and Application State, we can access the values stored there from a class other than a code-behind file by using the Current property of the HttpContext class. For example:
•      HttpContext.Current.Application["HitCount"];
•      HttpContext.Current.Session["Email"].ToString();
If we were in the code-behind file, we could access these by simply using
•      Application["HitCount"]
•      Session["Email"].ToString()
For View State, how can we access values stored in View State when we are in a class other than a code-behind file?

I experimented a bit, and verified a method in a class located in the App_Code folder would not recognize Session or Application unless I preceded it with HttpContext.Current. Further, it did not recognize ViewState at all. Further, I found that "static" methods in any class (including the code-behind) would fit this scenario where the HttpConext.Current would need to precede the Session and Application in order to be recognized, and the ViewState is not recognized at all (at least not by the Intellisence of Visual Studio 2008). How can I get ViewState from the page to be recognized in these methods/classes?
0
Comment
Question by:dwoolley3
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 37733882
Have you considered taking in a parameter of type System.Web.UI.StateBag? Then you can simply pass the ViewState from your page.
0
 
LVL 58

Accepted Solution

by:
amit_g earned 350 total points
ID: 37733894
ViewState is about the page and that is why it is available within the page only unlike Application or Session that are applicable to all the pages. If you need to access it in another class, it needs to be passed to that class from the calling page. You cannot (and should not) access page's ViewState in any random class unless that class is being instantiated from the Page itself.
0
 
LVL 14

Assisted Solution

by:quizwedge
quizwedge earned 50 total points
ID: 37750181
As others said, you can't directly access it (or at least shouldn't if you find some hack.) Here are two examples of how to pass the information, one of which is the StateBag method mentioned by kaufmed.

http://stackoverflow.com/questions/2445155/can-i-access-viewstate-of-one-page-in-another-page-in-asp-net
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 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