Welcome to AspAdvice Sign in | Join | Help

ASP.NET cross-page postbacks: PreviousPage and PreviousPage.IsCrossPagePostBack

There has been some confusion on Forums related to PreviousPage property, especially whether one should check only PreviousPage property for null reference and why there's IsCrossPagePostBack property too.

It is so that basically PreviousPage property on target page is non-null in both cases following

  • Page is loaded by being target page on cross-page postback
  • Page is loaded by being target page on Server.Transfer

However, IsCrossPagePostBack property is true only in "real" cross-page postback while with Server.Transfer it is false.

So this adds to the "soup" that if you intend to use your Page also as normal page - that is, it can be used without cross-page postback - you need to check PreviousPage property for null reference. But in practise, checking only PreviousPage property for null reference is quite enough for the cases you deal with cross-page postbacks. You usually want to access the source page's controls in a way or another and therefore it is quite the same was the Page loaded with cross-page postback or Server.Transfer.

So some sort of pseudocode to distinguish these scenarios:

[VB.NET]

    'This logic on target page, note checking via PreviousPage property
        If Not PreviousPage Is Nothing Then

            If PreviousPage.IsCrossPagePostBack Then
                'Standard cross-page postback
            Else
                'Server.Transfer
            End If

        Else
            'Standard use without cross-page postback
        End If

[C#]
 //This logic on target page, note checking via PreviousPage property
 if (PreviousPage != null)
            {
                if (PreviousPage.IsCrossPagePostBack)
                {
                    //Standard cross-page postback
                }
                else
                {
                       //Server.Transfer
                }
            }
            else
            {
                //Standard use without cross-page postback
            }

And as usual, it's also quite well in the docs: http://msdn2.microsoft.com/en-us/library/ms178141.aspx

Published Thursday, July 12, 2007 2:38 PM by joteke
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: ASP.NET cross-page postbacks: PreviousPage and PreviousPage.IsCrossPagePostBack

How can I pass my values from 1st page to the 3rd or 4th Page?
Monday, November 24, 2008 1:27 AM by Sanghamitra

# re: ASP.NET cross-page postbacks: PreviousPage and PreviousPage.IsCrossPagePostBack

Source page : source.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="source.aspx.cs" Inherits="source" %> . . . Target page : target.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="target.aspx.cs" Inherits="target" %> <%@ PreviousPageType VirtualPath="~/source.aspx" %> . . .