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.
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:
- ViewStateContainer.aspx: This page sets the
ViewStatevariable and transfers the user to another page using
- AccessViewState.aspx: This page accesses the
ViewStatevariable of ViewStateContainer.aspx page.
This is the code of ViewStateContainer.aspx page: