Accessing Content Page properties in Master Pages

I'm teaching our ASP.NET 2.0 Best Practices course this week. I only get to teach about 1 week a quarter, so I greatly enjoy whenever I do. One of the interesting things about teaching, even content that you know very well, is that there are always questions and topics that I haven't run into in the past. Today was no exception.

I was covering Master Pages and Theming when I was asked about accessing information on the content page as part of the master page's processing. As it turns out, there were two solutions.

1. The Page property on the ContentPlaceHolder control has a reference to the Page class formed by the content page. In other words


give you access to the Page object for the content. So you can cast that object onto the specific page class and access any public properties normally.

2. Use another ContentPlaceHolder control. In the specific request, the goal was to include a link to an optionally present, page-specific CSS file. This link (ideally) would be in the header. The solution was to put a ContentPlaceHolder into the HEAD section. Now the content page was completely capable of filling in the content with a CSS link, if one was available.

I was surprised by this second answer, as I didn't think that the ContentPlaceHolder would "work" other than in the body. No reason why I thought this. But one of the students suggested it and, lo and behold, it worked just fine.

As most instructors will tell you, the number of things you learn while teaching a class is quite astounding.