Cross page postbacks in ASP.NET 2.0

Brock Allen posted a couple of interesting tidbits about cross page postbacks in ASP.NET 2.0. He talks about two options for accessing data from the post source page - weakly typed access using the FindControl method and strongly-typed access using the the PreviousPage directive. The problem with the PreviousPage option comes if you want to post several different source pages to the same target page. There is, however, another alternative that isn't obvious and isn't really described much in the documentation - the Reference directive. Inserting this directive makes the specified page type accessible in the declaring page. Including the following in your post target page:
<%@ Reference Page="SourcePage.aspx" %>
types the PreviousPage property  as Page but allows you to cast the it to the SourcePage_aspx type.

Some people don't like the cross-page posting feature, as it causes strong coupling between pages. I think it definitely has its uses (eliminating the need for the Server.Transfer abomination, for example). One way to address the strong coupling issue is to use interfaces instead of page types, in which case the Reference directive isn't needed. But it's handy to know about if you need it.

Incidentally, this directive is actually part of .NET 1.1, but I've never needed it with that version.. It seems to become more useful in .NET 2.0.

No Comments