IsCrossPagePostBack
I know it's just my fault but for a while I've been thinking that IsCrossPagePostBack was supposed to return true if the current page was called through a cross-page posting. So I was led to writing code like this:
// This code lives in TARGET.ASPX--the target of a posting
protected void Page_Load(object sender, EventArgs e)
{
if (!IsCrossPagePostBack)
{
Response.Write("Sorry, you can only invoke me through cross-page posting.");
Response.End();
return;
}
// other code here
}
This code never worked as expected, in Beta 1 as well as in Beta 2. Recently, I realized it was my fault as IsCrossPagePostBack is designed to return true for the PreviousPage page object, not for the current page. In other words, it returns true for the page which STARTED a cross-page posting, not for the page being invoked that way. The code above should be rewritten as follows.
// This code lives in TARGET.ASPX--the target of a posting
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null && PagePrevious.IsCrossPagePostBack)
{
Response.Write("Sorry, you can only invoke me through cross-page posting.");
Response.End();
return;
}
// other code here
}
It works but to me this code raises some questions. Why checking both? Does it really exist a scenario with a non-null PreviousPage and IsCrossPagePostBack set to false? YES.
In ASP.NET 2.0, Server.Transfer integrates with this mechanism and enjoys a non-null PreviousPage too for you to comfortably access any controls in the caller. In this case, though, IsCrossPagePostBack is false.
In summary, checking PreviousPage against null is sufficient if you don't need further details--was it called through cross-page posting or Server.Transfer.
Great information here.