Passing Values Between Pages in ASP.NET 1.x and 2.0

Posted on July 16, 2006  |  

Posted in Development

3 comments

While reading up on what’s new about passing values between pages in APS.NET 2.0—namely cross-page postbacks—I noticed some folks complaining this wasn’t possible in 1.x, while others complained about the need to give properties of the calling page public visibility. Technically, neither complaint is correct.

The 1.x Way

Passing values between pages is possible in 1.x. ASP.NET provides less support, but it’s easy to fill the gap. Suppose, PageA needs to pass a book title and price to PageB. Normally, documentation suggests you declare public properties as “parameters”, and some folks (understandably) aren’t comfortable exposing to the outside world what needs to stay “in the family.” As my sample PageA shows, these properties may as well be internal.

You initialize “parameters” you want to pass and call Server.Transfer ("PageB.aspx"). The receiving page inspects the HttpContext.Current.Handler property to see where a request came from. If the caller is PageA, it references its properties and displays the book title and price.

Why am I storing passed values in view state? This is something documentation never mentions: if PageB happens to receive postbacks, Ajax callbacks, etc, you won’t have PageA around anymore, so you need to hold on to what PageA passed you. View state is a good fit for small chunks of data.

Therein lie a couple of gotchas. If you’re passing, for example, large collections of objects, view state will get bloated. Also, whatever you pass should better be serializable, or the view state serializer will throw an exception. Finally, if PageB has view state turned off, you’ll have to get creative and think of other ways to store passed data.

The Ajax Predicament

If you employ third-party Ajax components, they will probably interfere with how PageA and PageB talk to each other. For example, I bet an Ajax component won’t like a page being Server.Transfer’ed in the middle of a call. Also, extracting values in PageB, if it’s Ajax-enabled, may get complicated, if not impossible.

The 2.0 Way

In 2.0 you have help in the form of the Page.IsCrossPagePostBack property. You can also strongly-type the Page.PreviousPage property by declaring the calling page type via @PreviousPageType. My good friend Scott Allen covered this extensively in his article Design Considerations for Cross Page Post Backs in ASP.NET 2.0. Fritz Onion also makes some interesting points in his post Multi-source cross page posting.

In closing, I’d like to share a property I bake into my page base classes these days. The property indicates how you got to a page, whether it’s an original, regular request; a postback; a callback, etc. I’ve excluded an Ajax callback because it’s product-specific, but you’re welcome to add your own if you use a third-party offering.

public enum PageInvocationMethod
{
    OriginalRequest,
    Postback,
    CrossPagePosting,
    ServerTransfer,
    Callback
}

protected PageInvocationMethod PageInvocationMethod
{
  get
  {
     if (IsPostBack)
        return PageInvocationMethod.Postback;
        
     if (IsCallback)
        return PageInvocationMethod.Callback;
        
     if (PreviousPage == null)
        return PageInvocationMethod.OriginalRequest;
        
     if (!PreviousPage.IsCrossPagePostBack)
        return PageInvocationMethod.ServerTransfer;
        
      return PageInvocationMethod.CrossPagePosting;
    }
}

3 comments

Bob
on July 17, 2006

Can't you pass values between pages by simply dumping them in the user's Session? This way, your pages don't depend on each other to exist - they just take parameters.


Milan Negovan
on July 17, 2006

Bob: yes, you surely can. Corss-page posting is just another way of passing data around.


almny
on April 25, 2009

thanks man
it's useful from me
i coming here for this code :)
thanks again


Leave a comment

  •