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