Skip navigation.

Stock.XCHNG RelaunchedAll recent postsA Quitter or Mismanaged Talent?

Passing Values Between ASP.NET Pages

In ASP.NET 1.x we’ve had very few ways to pass information from page to page. The idea was to declare public properties in a page, call Server.Transfer, and do a funky cast of Context.Handler to access properties of the calling page (for an in-depth discussion see the MSD article Passing Values Between Web Forms Pages).

In ASP.NET 2.0 we’ve got more options. There’s an MSDN section entitled Redirecting Users to Another Page which introduces cross-page posting. In 1.x one page could not post the server-side form to another page. In 2.0 this limitation is lifted. Such controls as Button, LinkButton, and ImageButton have a new property, PostBackUrl which specifies which page the form should be posted to (see How to: Post ASP.NET Web Pages to a Different Page).

You still have Server.Transfer at your disposal which is a server-side operation, while a cross-page postback is a client-side operation.

It gets a little messy when you need to know where a page request originated from. Is it a post-back within the same page? Is it a cross-page postback? Did you get here via Server.Transfer? Although MSDN provides an answer, I still wish there was a property on the Page class itself to spell out the request origin. Fortunately, that’s easy enough: we can put this logic in a base class.

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

public class PageBase : Page
{
  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;
      }
  }
}

Comments

No comments yet

Emails and Notifications

Would you like to be notified when somebody responds to this post? 

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):