Easy ASP.NET Page Templates - JavaScript
(Page 4 of 5 )
When adding site-wide JavaScript code, I suggest that you use an external file because it is easier to implement and provides additional performance benefits. To do this you just add the <script> tag that references the JavaScript file between the <head> tags of the page by adding it in the Render method.
When adding a page specific script you can just write the script between (or even inside) the controls on the page and it will be rendered on the page along with everything else:
<%@Page ... >
<asp:Panel ID=”LeftCol”>
...
</asp:Panel>
<script language=”javascript”>
//...place some JavaScript code here.
</script>
<asp:Panel ID=”Body”>
...
</asp:Panel>
<asp:Panel ID=”RightCol”>
...
</asp:Panel> Heirarchial Page Classes In some web sites where different sections of the site have different templates including shops, forums, admin, etc, it may be beneficial to not just have 1 base Page template but to have a tree of them.
You should have 1 truly base Page class which is derived from to make more specific templates, then, if necessary, they can even be derived from again to make even more specific base page classes.
A base Page class tree may look something like the following:
When Should Page Templates be Used? Any web site with more than one page that has items consistent on each of them such as headers, footers, menus, session summary, shopping cart summary, etc, or even common functionality such as retrieving a user’s Username and displaying it should use templates.
Advantages:- Development is simplified because every page already has a base to work from reducing the need to copy and paste common page features such as headers, menus, footers from other pages.
- Maintenance is simplified because a change to a common page feature can be made in a single place with all pages on the site reflecting these changes instantly.
- User friendliness is enhanced because every page is consistent with every other page making it easier to locate menus etc, on the page.
- Page templates can be added to a page simply by making each page inherit the custom Page class.
- While other template techniques exist, this is one of the most effective and most powerful while still remaining extremely simple to use.
Disadvantages:- The base page template uses code to design and render some consistent controls which is slightly more difficult and time-consuming than using the HTML designer.
- When writing in the HTML section of the designer, there is no assistance from VS.NET's HTML IntelliSense because you are not writing inside the <body> tag.
Next: Conclusion >>
More ASP.NET Articles
More By John Rebbeck