tags:

views:

1230

answers:

3

In ASP.net 2.0, the PreviousPage property of a web page does not have a ViewState collection. I want to use this collection to transfer information between pages.

A: 

Use HttpContext.Current.Items instead...ViewState is only good for the page it is on.

FlySwat
This method is simple and clean. I like it.
Keith Walton
I tried to use this method, but I am posting directly from a link on the first page. No code is executing where I can populate the HttpContext.Current.Items colection: <asp:linkbutton runat="server" id="Upload" text="Upload" postbackurl="~/uploadfeescheduleterms.aspx" />
Keith Walton
+1  A: 

View State is exclusive to the page.

If you want to transfer items,

  • you can persist the data in a database, file, forms auth ticket or other cookie (Dont use Session or HttpContext.Current.Cache if you can help it)
  • do a cross page post - from your first page, post back to the second page (and get the details from HttpContext.Current.Request.Form[] collection)
  • put the values in a query string
StingyJack
I ended up using a cross page post, but accessing a public property on the first page (see below).
Keith Walton
A: 

You can't directly. (See http://msdn2.microsoft.com/en-us/library/ms178139(vs.80).aspx

Here's what you can do -

Create public properties on the first page exposing the information you want to share. On the second page, set the PreviousPageType to the first page in the header of aspx file:

<%@ previouspagetype virtualpath="~/firstpage.aspx" %>

Then, get the values of these properties in the Load event of the second page:

If (Not MyBase.IsPostBack) Then

    _someValue = Me.PreviousPage.SomeValue

End If
Keith Walton
Use the @ PreviousPageType directive to get strong typing against the PreviousPage property. "_someValue = firstPage.SomeValue" becomes "_someValue = Me.PreviousPage.SomeValue"
Keith Walton
Answer edited to use PreviousPageType
Keith Walton