Solved

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

Posted on 2012-03-17
3
2,836 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 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

Industry Leaders: 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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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