Page Inheritance in ASP.NET 2.0

Monday, September 12, 2005

Rick Strahl has a fantastic post: “Understanding Page Inheritance In ASP.NET 2.0”. Go read the post now.

Here are some ideas I thought of while reading.

The @ Reference, @ MasterType and @ PreviousPage directives are the only reliable mechanisms for referencing a type defined in another page (or user control, or master page) from inside a web form.

In Rick’s case he wanted to create a new web form (UploadItemPictureEx) derived from UploadItemPicture, where UploadItemPicture is defined as a Page derived class in the CodeFile for a second web form. In the new compilation model UploadItemPicture isn’t visible from the code inside the UploadItemPictureEx web form. This behavior is dramatically different from 1.1, as Rick clearly explains.

One solution is to define a base class for both web forms and stick the class in App_Code or a class library. Another solution is to use @ Reference, e.g:

]]>

If you place the above in UploadItemPictureEx.aspx, you’ll be able to use the UploadItemPicture type inside the CodeFile without any hassle.

The @ MasterType directive is useful when you want a strongly typed Master property for your page. I described @ MasterType in a previous post.

Likewise, the @ PreviousPage directive is useful during Server.Transfer and cross page postbacks. The runtime will give you a strongly typed PreviousPage property. I have some example of using @ PreviousPage in this article.

If you are dynamically loading user controls with LoadControl and don’t know which control you need to @ Register or @ Reference, I don’t see any choice but to define a base class for the control and keep the class in App_Code or in a class library referenced by the web project.


Comments
Rick Strahl Monday, September 12, 2005
Ah, good point. I didn't realize you could actually put a reference to a page in the @Reference tag. This helps...

Still a bummer though that there looks to be no way to truly dynamically load a control/page reference in another way.

Thanks...
Mike B Friday, October 14, 2005
How can you strongly type the Page.Master object on a User Control? Currently I use the @Reference directive on the design page to my master file and cast the Page.Master obejct to my master class. This works fine in Beta 2 and allows me to access all of properties on my master page. I recently installed the Release Candidate and now a get a circular reference compile time error. Any ideas?

Thanks,

Mike
scott Saturday, October 15, 2005
Mike: I don't think it's possible. The problem as I see it is getting a stronly typed Page property. What I've been able to do is something like the following (assumes you have a master page called MasterPage.master and a default.aspx).

<%@
Control Language="C#"
ClassName="WebUserControl" %>
<%@ Reference Page="~/Default.aspx" %>
<%@ Reference
VirtualPath="~/MasterPage.master" %>

<script runat="server">

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
((default_aspx)Page).Master.Message =
"foo";
}
</script>

Pawel Pabich Tuesday, November 1, 2005
hajo,

I can not still use UploadItemPicture.aspx
in UploadItemPictureEx code behind class. Can you post some simple example of code?

thanks in advance

pawel pabich
Sven Aelterman Friday, November 4, 2005
Strongly typing the Page.Master page is possible if you use the @MasterPage directive instead of the @Reference directive on the page.

HTH,

Sven.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!