Master Pages in ASP.Net 2.0 - Going Beyond the Basics
(Page 4 of 4 )
There are many additional options we have with Master Pages, and I’ll highlight a few of what I believe to be the more useful ones.
Site-wide DefinitionSomething I could see becoming a pain is having to remember to reference the Master Page for every single content page in your site, especially if your application has many pages. Well of course the thoughtful ASP.Net team planned ahead to circumvent this issue, by allowing us to define a default Master Page for the entire application in the web.config file. To create this definition, you would place the following in the web.config file:
<configuration>
<system.web>
<pages masterpagefile=”site.master” />
</system.web>
</configuration>
Client-side ScriptingThis is more of a caveat than anything. Quite often we developers will implement some custom client-side JavaScript to manipulate the contents of our controls without having to perform a post back to the server. A good example of this would be a timesheet, for which you create JavaScript code that changes totals in real time as you enter your hours, and then checks and saves the values once you’re done and hit submit.
When not using Master Pages, it’s quite simple to do this, because all of your web controls have an id parameter, and that id is transmitted when the HTML control is created. Therefore all you need to do is reference the ID of the object in JavaScript as you would in any HTML page.
When I created my first Master/Content pages, I was annoyed to see that all of my client-side script "broke." As soon as I checked the generated source code, it was easy to see why. Every control id had been changed. Each still contained the original id, but now they had appended to the beginning a string something like the following: “_ctrl105_”.
To fix any broken JavaScript references, what you’ll need to do is examine your browser’s source code, find what the appended string is in your case, and tell your JavaScript code to add it to the beginning of any object references it’s using.
Customized Master PagesWhile the major browser creators still struggle to come to terms with complete standards compliance, there remains the possibility that we as developers may need to customize the UI somewhat to meet any inconsistencies. Coincidentally, the Microsoft team has allowed us to create different Master Pages for different browsers. You can form your own conclusions about the reasons why, I’m only here to show you how to do it!
So we could take our standard Master Page reference:
<%@ Page masterpagefile=”site.master” %>
And we could tweak it to provide a Firefox friendly alternative:
<%@ Page netscape:masterpagefile=”nsSite.master” masterpagefile=”site.master” %>
And then even further to customize for Internet Explorer as well:
<%@ Page netscape:masterpagefile=”nsSite.master”
ie:masterpagefile=”ieSite.master” masterpagefile=”site.master” %>
While you as a developer and a believer in web standards may grimace at the idea of creating separate templates for separate browsers, this is a still a good example of how flexible Master Pages are, even in their first revision!
Conclusion Master Pages in my opinion will be enormously popular, among developers and designers alike. Another forward stride in the world of Rapid Application Development, I really have to hand it to the ASP.Net team on this one! I hope that with the assistance of this article, you will find them enjoyable to use, flexible, and simple to work with.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |