User Id :    Password :      New Member   Forgot Password  
Pass Viewstate across pages in ASP.NET
Description Yes, we can access the viewstate variables across pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user to other page.   No. of Views     10030
  Rating     4
Author Gaurav   Posted On     22 Oct 2010
Tags ASP.NET    

Sample Code   Download Code

Pass Viewstate across pages in ASP.NET


There was the big question during the interview and in projects also.Can we access the view state values on other page?

Before some time I thought that it is not possible and my answer was  “No” . Because i have read in books that Viewstate is page specific, it is available only on the same page on which it was created. Once you redirect to another page, the previous page’s viewstate is no longer accessible.

But that is possible. Yes, we can access the viewstate variables across pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user to other page. If Response.redirect is used, then ViewState cannot be accessed across pages.

Let us create one sample application to understand this.

Step 1 : Create two page page1.aspx & page2.aspx

Step 2 : Code for page1.aspx.cs

protected void Page_Load(object sender, EventArgs e)
	    ViewState["Page1"] = "Page1 ViewState";
	public accessViewState ReturnViewState()
	    return ViewState;

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

accessViewState 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 viewstate.

Step 3 : Code for page2.aspx.cs

	private accessViewState PreviousPageViewState
	        accessViewState returnValue = null;
	        if (PreviousPage != null)
	            Object objPreviousPage = (Object)PreviousPage;
	            MethodInfo objMethod = objPreviousPage.GetType().GetMethod
	            return (accessViewState)objMethod.Invoke(objPreviousPage, null);
	        return returnValue;
	protected void Page_Load(object sender, EventArgs e)
	    if (PreviousPage != null)
	        if (PreviousPageViewState != null)
	            Label1.Text = PreviousPageViewState["Page1"].ToString();

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 the previous page. 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 it creates 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.

Step 4: Run the application & see the effect

About Author

About Author I am as simple as c#. Gaurav
No Photo
Country India
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Posted By Annonymous on 28 Mar 2011 at 06:00 AM
accessViewState is not a class please use StateBag class instead of accessViewState - - ----surendra singh
Posted By Sumit on 13 Dec 2010 at 05:21 AM
good work gaurabv..
Posted By Najmul Hoda on 23 Oct 2010 at 01:39 AM
Nice One!!
Write your comment here.
Verification Code