Click here to Skip to main content
14,027,514 members
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 30 Jun 2009
Licenced CPOL

Access ViewState Across Pages

, 30 Jun 2009
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how we can share ViewState across pages


Before I start this article, let me ask you something. Is it possible to access the ViewState variable of one page on another page? I don't know what your answer is. Well, frankly speaking, my answer was also "NO" before writing this article as it is said that ViewState is page specific.


ViewState is a very misunderstood animal. It is said that ViewState is Page specific; that means, 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 not true.

Using the Code

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.

Before you continue reading this article, 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 the link at the top of this article.

I have created two *.aspx pages named:

  1. ViewStateContainer.aspx: This page sets the ViewState variable and transfers the user to another page using Server.transfer.
  2. AccessViewState.aspx: This page accesses the ViewState variable of ViewStateContainer.aspx page.

This is the code of ViewStateContainer.aspx page:

public partial class ViewStateContainer : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        ViewState["Page1"] = "Page1 ViewState";

    public StateBag ReturnViewState()
        return ViewState;

As you can see, I have set a ViewState variable in Page Load and transfer the user to AccessViewState.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 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 viewstate.

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

public partial class AccessViewState : System.Web.UI.Page
    private StateBag PreviousPageViewState
            StateBag returnValue = null;
            if (PreviousPage != null)
                Object objPreviousPage = (Object)PreviousPage;
                MethodInfo objMethod = objPreviousPage.GetType().GetMethod
                return (StateBag)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.



  • 30th June, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Talking Dotnet
Technical Lead
India India
I am an experienced Software Developer with 11+ years of hands-on experience working with Microsoft.NET technology (ASP.NET, ASP.NET Core, C#, SQL Server, Angular).

Visit Talking Dotnet
For ASP.NET Core, read ASP.NET Core Articles

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Debopam Pal25-Nov-13 1:04
professionalDebopam Pal25-Nov-13 1:04 
GeneralThanks for the article Pin
NaveedAhmed12-Nov-13 19:59
memberNaveedAhmed12-Nov-13 19:59 
GeneralMy vote of 5 Pin
Haynes10-Sep-13 3:33
memberHaynes10-Sep-13 3:33 
GeneralMy vote of 5 Pin
AshishTribhuwan125-Aug-13 21:23
memberAshishTribhuwan125-Aug-13 21:23 
GeneralMy vote of 5 Pin
anurag.sri435-Dec-11 0:00
memberanurag.sri435-Dec-11 0:00 
GeneralMy vote of 4 Pin
gourisankar mohanta24-Apr-11 8:50
groupgourisankar mohanta24-Apr-11 8:50 
GeneralRe: My vote of 4 Pin
Debopam Pal25-Nov-13 1:02
professionalDebopam Pal25-Nov-13 1:02 
GeneralMy vote of 4 Pin
karaniscool22-Jul-10 1:15
memberkaraniscool22-Jul-10 1:15 
GeneralGreat Information Pin
Tiger4567-Jul-09 19:38
memberTiger4567-Jul-09 19:38 
GeneralRe: Great Information Pin
Talking Dotnet7-Jul-09 19:52
memberTalking Dotnet7-Jul-09 19:52 
Generalgood Article Pin
Viral Upadhyay3-Jul-09 4:03
memberViral Upadhyay3-Jul-09 4:03 
GeneralRe: good Article Pin
Talking Dotnet6-Jul-09 18:57
memberTalking Dotnet6-Jul-09 18:57 
AnswerRe: Thread was being aborted exception error in using viewstate.. Pin
Talking Dotnet1-Jul-09 3:03
memberTalking Dotnet1-Jul-09 3:03 
QuestionWhy use state at all? Pin
Tragdor30-Jun-09 5:39
memberTragdor30-Jun-09 5:39 
GeneralWhy Pin
zlezj30-Jun-09 3:20
memberzlezj30-Jun-09 3:20 
GeneralRe: Why Pin
Talking Dotnet30-Jun-09 18:12
memberTalking Dotnet30-Jun-09 18:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190419.4 | Last Updated 30 Jun 2009
Article Copyright 2009 by Talking Dotnet
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid