Beware the IsCrossPagePostback property

ASP.NET 2.0 introduces the ability to post back to a different URL. Version 1.1 did not have this option and always posted back to the same page. The System.Web.UI.Page class got a new property IsCrossPagePostback, that tells you whether the postback you got was due to a crosspage postback. Or so you think.

If you evaluate the IsCrossPagePostback like so:

if (this.Page.IsCrossPagePostback) { … }

you will always get false, even if you are sure that it should be true. Instead, you should check the property like this:

if (this.Page.PreviousPage != null && 
    this.Page.PreviousPage.IsCrossPagePostback) { … }

Using Reflector you can see that the IsCrossPagePostback is only set on Page objects when they refer to the previous page and are accessed from the PreviousPage property. The PreviousPage property is a non-null reference to a Page object for both cross-page postbacks, but also for Server.Transfer calls. So, it does make sense to evaluate the IsCrossPagePostback property this way. The only reason that the property is available in the first example is because it is part of the Page class. A kind of weird construction if you ask me.

Also, when you set the <%@ PreviousPage %> directive on the postback page (the one that receives the cross-page postback), you can access this.PreviousPage in a strong typed way.

<%@ PreviousPage VirtualPath=”~/CrossPagePostbackSender.aspx” %>

this.PreviousPage will now be of type YourCompany.Web.CrossPagePostbackSender, if that’s the underlying type.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s