So here is this brand new feature in ASP.NET 2.0 – CrossPage posting, which put simply is when you have the ability to post your form to a different page than yourself. Now, I get the feeling that this should be avoided in most scenarios, because apparently the way CrossPage posting works under the scenes is, it *does* create the previous page ~ except it tricks it out a bit. Right before the PreRender event, it creates a dummy Response object that basically .. umm .. eats up all the Response.Writes, so the client actually doesn’t see the page. But all the rest of the goo is still executed, so you get a page instance in the “posted to” page. That instance can simply be accessed using this.PreviousPage. In fact, this trick that ASP.NET 2.0 uses under the scenes, happens when you first access this.PreviousPage in the “posted to” page.
There are a few salient points regards CrossPage posting in ASP.NET 2.0 ~
1. Don’t use it, if you absolutely cannot get around not using it. (LOL)
2. The most straightforward way of implementing it is by using IButtonControl.PostBackUrl. In other words, whoever implements IButtonControl, can be specified a property called PostBackUrl, which is simply the address of the “posting to” page.
3. The not so straightforward way of implementing is Server.Transfer(“postedto.aspx”, true) – the “true” indicates that you wish to preserve the Request.Params. Also, by doing so, you can still access the previous page using the this.PreviousPage property, so it is pretty much like cross page posting, except without using IButtonControl
4. this.PreviousPage is of type System.Web.UI.Page, you either need to cast it to the previous page type, or add the <%@ PreviousPageType VirtualPath=”FromPage.aspx” %> directive to your “posted to” page.
5. Here is the ugliest part about Cross page posting – you need to start exposing data between pages using public properties. Unlike assemblies in .NET 2.0, where two assemblies can actually be “friends” so they can share the internal variables, ASPX pages on the other hand are pretty much screwed because everything by default is protected. So you need to open up the treasure box a bit by making stuff public - which is kinda yucky.
6. You have to remember that when you do CrossPage posting, a lot of what you “assume” such as validation checks, now become your responsibility. So if in case a validation check fails, you need to check PreviousPage.IsValid, and on failure you need to both redirect back to the “posted from” page, and somehow communicate enough information back to the callee page, so it can display a reasonable error message. That now becomes your responsibility.
Suggested Reading: My good friend, K. Scott Allen, has previously written an article about Cross Page Posting.