public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Access ViewState Across the Pages

Posted by Virendra Dugar on July 2, 2009

Before I start this article, Let me ask you something. Is it possible to access the ViewState variable of one page to another page? I don’t know what is your answer. Well, frankly speaking, My answer was  also “NO”  before writing this article As it is said that ViewState is page specific that means it is available on the same page only on which it is created. Once you redirect to another page, previous page viewstate is no more accessible. But that is not true.Yes, we can access the viewstate variables across the pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user on other page. If Response.redirect is used then ViewState can not be accessed across the pages.

Before you go down, Please read these articles on Cross Page Posting and Server.transfer.

Ok, So all set now.. I will demonstrate this using the demo created by me. You can download the demo from here.

I have created two .aspx page named
1. ViewStateContainer.aspx : This page set the ViewState variable and transfers the user to another page using Server.transfer.
2. AccessViewState.aspx : This page access the ViewState variable of ViewStateContainer.aspx Page.

This is the code of ViewStateContainer.aspx Page :

ViewState Container Page

As you can see, I have set a ViewState variable in Page Load and transfers the user to AccessViewState.aspx page using Server.transfer() method.
This page also contains a method ReturnViewState() which actually returns the ViewState of this page to the calling function. Return type of the method is StateBag Class.

StateBag Class : This class is the primary storage mechanism for all HTML and Web server controls. It stores attribute/value pairs as strings associated with the control. It tracks changes to these attributes only after the OnInit method is executed for a page request, and saves the changes to the page’s or control’s view state.

Now let’s take look at AccessViewState.aspx Page code:

Access View State

Whenever we use Server.transfer or Cross Page Posting, We can get the previous page object via PreviousPage Property. Using Previous Page, we can find the controls of previous page. As for example, One can access Label control placed in ViewStateContainer Page in Current Page.

Looking at the code, I have created a PreviousPageViewState property in this page, which returns the previous page’s ViewState. It first checks whether PreviousPage is null or not, if it’s not null then create an object of the previous page. Now using Reflection, we can invoke the method of the previous class. Using MethodInfo class, I have invoked the ReturnViewState() method of ViewStateContainer Page.

In Page_Load event, I am able to access the ViewState variable of  ViewStateContainer Page. You can access all the viewstate variables set in ViewStateContainer Page.

Enjoy..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: