IsCrossPagePostBack

Published 12 May 05 10:27 AM | despos

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.

 

Comments

# Brock Allen said on May 14, 2005 11:15 AM:

Also, are you aware that events on the previous page fire? I mention it here:
http://staff.develop.com/ballen/blog/PermaLink.aspx?guid=483742fd-01a2-4975-b76c-d3b8b4f29eaf

# DinoE said on May 16, 2005 04:15 AM:

Hi Brock
yeah I noticed that. Thanks for pointing it out!

# TrackBack said on May 23, 2005 01:36 PM:
# buy viagra said on June 4, 2005 09:08 AM:

...

# thecarspeakercellini said on June 16, 2007 04:57 PM:

The best links in internet

music-in-phone.info/.../verizon-wirless-ringtone.html

music-in-phone.info/.../free-ringtone-for-verizon-wireless-customer.html

<a href="music-in-phone.info/.../verizon-ringtone-and-wallpaper.html">verizon ringtone and wallpaper</a>

music-in-phone.info/.../free-ringtone-for-verizon-wireless-phone.html

<a href="music-in-phone.info/.../lg-ringtone-verizon-vx3300.html">lg ringtone verizon vx3300</a>

music-in-phone.info/.../verizon-ringtone.html

music-in-phone.info/.../free-ringtone-from-verizon.html

<a href="music-in-phone.info/.../free-verizon-samsung-ringtone.html">free verizon samsung ringtone</a>

music-in-phone.info/.../family-guy-ringtone-verizon.html

<a href="music-in-phone.info/.../2366i-nokia-ringtone-verizon.html">2366i nokia ringtone verizon</a>

music-in-phone.info/.../100-free-verizon-ringtone.html

music-in-phone.info/.../ringtone-for-lg-verizon-wireless-phone.html

<a href="music-in-phone.info/.../free-verizon-ringtone.html">free verizon ringtone</a>

<a href="music-in-phone.info/.../verizon-polyphonic-ringtone.html">verizon polyphonic ringtone</a>

music-in-phone.info/.../verizon-true-ringtone.html

Thanks!

# Sameera said on July 1, 2007 01:22 AM:

Made the same mistake too. Searched for the problem and found your post. Just after reading your solution, it acutally did make a lot of sense.

For the page being posted to, it's not a *PostBack but a first time loading. Only the original page can have a postback in the real sense.

# dotnetjunkie said on July 1, 2007 07:57 PM:

I made the same mistake today and thought that maybe it had something to do with me using master pages...

Thank god I found your blog post!

Thanks a lot!

# louise,Remember it? <a href= ></a> [url=][/url] said on July 10, 2007 09:30 PM:

Remember it?  

<a href=  ></a>  [url=][/url]  

# AJ said on November 20, 2007 10:57 AM:

thanks for the explanation on that.  it's not very intuitive the way they have it.  you'd figure IsCrossPagePostBack would refer to the current Request context or something.

# psmithphil said on February 29, 2008 11:25 PM:

I had the same problem this evening as the others, and this post saved the day.  Thank you for providing this solution and explanation.

# PaulB said on March 1, 2008 11:57 AM:

Still find this is very counter-intuitive when you consider how the page class is used for all other scenarios...

Maybe should be a removed from Page and only available on PreviousPage? (I'm assuming that PreviousPage is derived from Page and not simply an instance...  probably wrong... come to think of it definately wrong...).

I've found no use for this when used on the first pages instance.

# Todd said on June 1, 2008 03:13 AM:

Thanks for the blog post!  You saved me hours trying to solve this problem.

Leave a Comment

(required) 
(required) 
(optional)
(required)