in

ASP.NET Weblogs

Andy Smith's Blog

Page.RegisterStartupScript('Andy', 'MetaBuilders_WebControls_GainKnowledge();');

MetaBuilders version of MasterPages

I just put up my own version of the venerable masterpages page templating framework given out by the asp.net team. I've added the ability to nest regions, which opens up some great multi-masterpage scenarios.

Getting the feature in there was a bit harder than I'd imagined, but the actual implementation ended up simpler than I'd first imagined. The reason it's not automaticly supported is because a ContentContainer, Content, and Region are all naming containers. This means that FindControl can't search inside them to find the correct region by ID. Not to mention that conents aren't put in the control hierarchy until after the masterpage is loaded.

My first instinct was to change the way FindControl worked, so that it would find regions that are inside, this was a really complicated bit of code, and while it worked it was also not very performant. Then I stepped back from the solution, and looked at it differently. I decided that FindControl just doesn't do what I want it to do, because what I wanted was to find these regions regardless of where they were declared. So what I did was have regions keep track of themselves, registering themselves when their ID is set, and exposing a FindRegion method that does a simple lookup by ID to find the region I want. My first run thru of that idea was done by having a staic hashtable that stored the regions, which appeared to be fine at first, but then I realized that changing a page doesn't restart the app, which means that the static hashtable will point to incorrect regions. This realization took place around 1:30 am. So I figured I'd just sleep on it, and fix it the next day. But then around 3AM I woke up with the answer, got out of bed and went to the computer to quickly code the fix. The idea was to store the region references in the HttpContext.Items. I found that it worked perfectly, which is surpising considering my drowsy state at the time.

Anyway, if you've been looking to implement nested regions with MasterPages, go grab my MetaBuilders version of MasterPages.

Comments

 

Paul Wilson said:

Great update to MasterPages!
July 28, 2003 9:53 AM
 

Michael Cook said:

Awsome, this is just what I was needing. Thank you for your work.
July 28, 2003 12:48 PM

Leave a Comment

(required)  
(optional)
(required)  
Add