Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now