Access Viewstate across pages in ASP.NET

Posted by Fenil Desai | 6:00 AM | View Comments

Introduction

There was one question during my recent work in one of my ASP.NET Project.

“Is it possible to access the ViewState variable of one page on another page?”

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 not true. 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 one.aspx & two.aspx

Step 2 : Code for one.aspx.cs

01protected void Page_Load(object sender, EventArgs e)
02{
03    ViewState["Page1"] = "Page1 ViewState";
04    Server.Transfer("two.aspx");
05}
06 
07public StateBag ReturnViewState()
08{
09    return ViewState;
10}

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

Step 3 : Code for two.aspx.cs

01private StateBag PreviousPageViewState
02{
03    get
04    {
05         StateBag returnValue = null;
06        if (PreviousPage != null)
07        {
08            Object objPreviousPage = (Object)PreviousPage;
09            MethodInfo objMethod = objPreviousPage.GetType().GetMethod
10                    ("ReturnViewState");
11            return (StateBag)objMethod.Invoke(objPreviousPage, null);
12        }
13        return returnValue;
14    }
15}
16 
17protected void Page_Load(object sender, EventArgs e)
18{
19    if (PreviousPage != null)
20    {
21        if (PreviousPageViewState != null)
22        {
23            Label1.Text = PreviousPageViewState["Page1"].ToString();
24        }
25    }
26}

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

Hope this will helps

Posted via email from fenildesai's posterous

Share Related Posts with Thumbnails
blog comments powered by Disqus