?
Solved

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

Posted on 2012-03-17
3
Medium Priority
?
2,953 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
[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 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 300 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 1050 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 150 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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

771 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