Last post Oct 31, 2013 01:35 PM by Fixy
Jul 24, 2007 10:02 PM|gomaz|LINK
How to pass Viewstate value from one aspx page to another aspx page?
For an example :
In page1.aspx i have the following code
viewstate["username"] = "aspnet"
How to retrieve this value in page2.aspx page.
You help would be appreciated..
Thanks in advance
viewState asp.net controls page size
view state controls
View state Problem
Jul 24, 2007 11:46 PM|aus_nexxus|LINK
You can't do that as ViewState is page specific and is actually stored in the HTML of the rendered page. You will need to pass the value via a POST or in the query string or store it in a session or you could cache the value in the asp.net cache which you
will be able to access on the other page.
Jul 25, 2007 02:47 AM|leandrokoiti|LINK
hey gomaz, what I used to do on ASP.NET 1.1 was:
1. create a public property on the source page that returns the viewstate value.
public class SourcePageClassName
public string MyValue
if ( this.ViewState("MyViewStateValue") == null )
this.ViewState("MyViewStateValue") = "default value";
this.ViewState("MyViewStateValue") = value;
2. do a Server.Transfer to the destination page.
3. On the destination page, on the page load event, I check whether the Context.Current.Handler is of the type of the source page and if it's then I cast it and read the property
protected void Page_Load(object sender, System.EventArgs e)
if ( Context.Current.Handler != null && Context.Current.Handler.GetType() == typeof(SourcePageClassName) )
SourcePageClassName source = Context.Current.Handler as SourcePageClassName;
and that's it...
on ASP.NET 2.0 you can do the same thing or do a cross postback creating the property I mentioned on step 1
Jul 26, 2007 02:59 PM|ominbox|LINK
Check this out....
You can use ViewState to Transfer data to the same page on Postback.
For setting Viewstate:
For retrieving Viewstate on postback:
string sFirstName = ViewState["FirstName"].ToString();
You can use the Context to Transfer data to the one page to another.
string sFirstName = this.Context.Items["FirstName"].ToString();
You can use the Session Variable to preserve the common data which is required almost for every page or across the application for the specific user.
For setting Session:
For retrieving Session from any page till the session is valid:
string sFirstName = Session["FirstName"].ToString();
Hope this helps you,
Jul 26, 2007 03:46 PM|moredotnet|LINK
Viewstate cannot be passed from one aspx page to another page.
You may make use of hidden variables and post the data you want to send to the new page.
To know more on passing values from one page to another, checkout this link ...
Jul 26, 2007 04:32 PM|leandrokoiti|LINK
Hey moredotnet, have you tried this code:
Although it doesn't look pretty good it makes the Page2.aspx
reiceve the "MyViewStateVar" property from Default.aspx
What you meant by "Viewstate cannot be passed from one aspx page to another page." is that "MyViewStateVar" isn't actually on Default.aspx anymore since the response flow has been redirected to Page2.aspx? Is that it?
I'm sorry... Is just that that's the way I learnt to pass a viewstate var from one page to another... but I'm not sure if is it a good practice or not... so I'm always looking for new ways to improve everything
Aug 03, 2007 10:21 AM|moredotnet|LINK
I agree with you to some extent. But notice here, you have used Server.Transfer instead of Response.Redirect.
Server.Transfer method simply renders the next page to the browser
without an extra round trip. Variables can stay in scope and Page2 can read properties directly from Default page because it’s still in memory. This technique would be ideal if it wasn’t for the fact that the browser is never notified that the page has
changed. The address bar in the browser will still show “Default.aspx” even though the Server.Transfer statement actually caused Page2.aspx to be rendered instead. It often causes problems related to the browser being out of touch with the server. Say, the
user reloads the page, the browser will request Default.aspx instead of the true page (Page2.aspx) that they were viewing.
Clear enough now :)
Aug 03, 2007 10:37 AM|leandrokoiti|LINK
Aug 05, 2012 08:09 AM|Mrinal Shendey|LINK
Hi Gomaz ,
we can access the
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (Page.IsPostBack == false)
ViewState["Mrinal"] = "Page1 ViewState";
public StateBag ReturnViewState()
protected void Button1_Click(object sender, EventArgs e)
How to retrive the data in second page =>
use the Reflection with MethodInfo and StateBag Property
partial class Default2 : System.Web.UI.
private StateBag PreviousPageViewState
StateBag returnValue = null;
if (PreviousPage != null)
Object objPreviousPage = (Object)PreviousPage;
MethodInfo objMethod = objPreviousPage.GetType().GetMethod("ReturnViewState");
return (StateBag)objMethod.Invoke(objPreviousPage, null);
if (PreviousPageViewState != null)
Label1.Text = PreviousPageViewState["Mrinal"].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
Jun 04, 2013 11:05 AM|ssjGanesh|LINK
I have tried your code to transfer value between the pages, the below code....
this.Context.Items["FirstName"] = "SuperMan";
But it gets the error,
Jun 04, 2013 11:53 AM|sujithkumar|LINK
How you are navigating from one page to another. Context will work only for single response, If you use server.transfer or cross page positing then only you can access context data in another page..
Oct 31, 2013 01:35 PM|Fixy|LINK
I'd be interested, if it's possible to not simply access the viewstate of a previous page but also restore it.
for example in a wizard or in an order process. when navigating back and forth in the different steps using crosspage postback,
the page/form field states should be retained as it is when posting back to the same page.
is this possible? otherwise I'd have to save each form value in the viewstate and restore it manually like it was earlier when using PHP.