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.
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
System.Drawing
Partial Class _Default
Inherits System.Web.UI.Page
Public Property MyViewStateVar()
Get
If ViewState("__MyViewStateVar")
Is Nothing
Then
ViewState("__MyViewStateVar") =
"hello world " & DateTime.Now
End If
Return ViewState("__MyViewStateVar")
End Get
Set(ByVal value)
ViewState(
"__MyViewStateVar") = value
End Set
End Property
Protected
Sub Page_Load(ByVal sender
As Object,
ByVal e As System.EventArgs)
End Sub
Protected
Sub Button1_Click(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles Button1.Click
Me.MyViewStateVar =
Me.TextBox1.Text
Server.Transfer("Page2.aspx")
End Sub
End
Class
-------------- Page2.aspx
Partial
Class Page2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles Me.Load
If Not IsPostBack
Then
If Not
Me.Context.Handler
Is Nothing
Then
Dim source
As Global._Default =
Me.Context.Handler
Response.Write(source.MyViewStateVar)
End If
End If
End
Sub
End
Class
-----------------------------
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
Age is a very high price to pay for maturity. ~Tom Stoppard
Marked as answer by Rex Lin - MSFT on Jul 30, 2007 01:23 AM
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.
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
gomaz
Member
86 Points
45 Posts
How to pass Viewstate value from one aspx page to another page?
Jul 24, 2007 10:02 PM|LINK
Hi all,
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
Gomaz
ViewState viewState asp.net controls page size view state controls View state Problem Web Ajax Viewstate
aus_nexxus
Star
8018 Points
1156 Posts
Re: How to pass Viewstate value from one aspx page to another page?
Jul 24, 2007 11:46 PM|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.
leandrokoiti
Member
138 Points
27 Posts
Re: How to pass Viewstate value from one aspx page to another page?
Jul 25, 2007 02:47 AM|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.
e.g.:
namespace MyNameSpace
{
public class SourcePageClassName
{
public string MyValue
{
get
{
if ( this.ViewState("MyViewStateValue") == null )
{
this.ViewState("MyViewStateValue") = "default value";
}
return this.ViewState("MyViewStateValue").ToString();
}
set
{
this.ViewState("MyViewStateValue") = value;
}
}
}
}
2. do a Server.Transfer to the destination page.
e.g.:
Server.Transfer("Destination.aspx")
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
e.g.:
...
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;
System.Diagnostics.Debug.WriteLine(source.MyValue);
}
}
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
ominbox
Member
192 Points
39 Posts
Re: How to pass Viewstate value from one aspx page to another page?
Jul 26, 2007 02:59 PM|LINK
Check this out....
You can use ViewState to Transfer data to the same page on Postback.
For setting Viewstate:
ViewState[
"FirstName"] = "SuperMan";For retrieving Viewstate on postback:
string sFirstName = ViewState["FirstName"].ToString();
You can use the Context to Transfer data to the one page to another.
Page1.aspx.cs
this
.Context.Items["FirstName"] = "SuperMan";Page2.aspx.cs
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:
Session[
"FirstName"] = "SuperMan";For retrieving Session from any page till the session is valid:
string sFirstName = Session["FirstName"].ToString();
Same way you can use Cookies also, but cookies will be stored on the client.
Hope this helps you,
Note: Please don't forget to Mark the "Answer" for the post which helps you.
moredotnet
Contributor
4685 Points
887 Posts
Re: How to pass Viewstate value from one aspx page to another page?
Jul 26, 2007 03:46 PM|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 ... http://www.dotnetuncle.com/Aspnet/60_pass_value.aspx
HTH
Cheers
Vishal Khanna
BOOK: .NET INTERVIEW CRACKERJACK
WEBSITE: ASP.NET, C#, AJAX, SQL, Design Patterns
leandrokoiti
Member
138 Points
27 Posts
Re: How to pass Viewstate value from one aspx page to another page?
Jul 26, 2007 04:32 PM|LINK
Hey moredotnet, have you tried this code:
---------- Default.aspx
Imports
System.Drawing Partial Class _Default Inherits System.Web.UI.Page Public Property MyViewStateVar() Get If ViewState("__MyViewStateVar") Is Nothing Then ViewState("__MyViewStateVar") = "hello world " & DateTime.Now End If Return ViewState("__MyViewStateVar") End Get Set(ByVal value)ViewState(
"__MyViewStateVar") = value End Set End Property Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Me.MyViewStateVar = Me.TextBox1.Text Server.Transfer("Page2.aspx") End SubEnd
Class-------------- Page2.aspx
Partial Class Page2 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then If Not Me.Context.Handler Is Nothing Then Dim source As Global._Default = Me.Context.HandlerResponse.Write(source.MyViewStateVar)
End If End If End SubEnd
Class -----------------------------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
moredotnet
Contributor
4685 Points
887 Posts
Re: How to pass Viewstate value from one aspx page to another page?
Aug 03, 2007 10:21 AM|LINK
Hi Leandrokoiti
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 :)
Cheers
Vishal Khanna
BOOK: .NET INTERVIEW CRACKERJACK
WEBSITE: ASP.NET, C#, AJAX, SQL, Design Patterns
leandrokoiti
Member
138 Points
27 Posts
Re: How to pass Viewstate value from one aspx page to another page?
Aug 03, 2007 10:37 AM|LINK
Mrinal Shend...
Member
2 Points
1 Post
Re: How to pass Viewstate value from one aspx page to another page?
Aug 05, 2012 08:09 AM|LINK
Hi Gomaz ,
we can access the
variables across pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user to other page. If is used, then cannot be accessed across pages.Let See
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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()
{
return ViewState;
}
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("Default2.aspx");
}
}
How to retrive the data in second page =>
use the Reflection with MethodInfo and StateBag Property
using
System.Reflection;
public
partial class Default2 : System.Web.UI.
Page
{
private StateBag PreviousPageViewState
{
get
{
StateBag returnValue = null;
if (PreviousPage != null)
{
Object objPreviousPage = (Object)PreviousPage;
MethodInfo objMethod = objPreviousPage.GetType().GetMethod("ReturnViewState");
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["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
control placed in Page in current Page.