Dynamically Using Master Pages


The dynamic use of Master Pages is a very exciting idea. Depending on individual parameters you may, for example, show the same pages with different designs. Another idea is to format content in different ways to show a printable version.

Unfortunately—and this has led to many controversial discussions in nonpublic Alpha forums—Master Pages cannot be defined dynamically at present. The reason is that Master Pages are already being integrated in the page during its dynamic compilation and they are fixed within each page.

At present, the ASP.NET team is checking on the feasibility of including dynamic Master Pages as a feature. If this is the case, the corresponding functionality could already be implemented into the Beta version. Until then, the team recommends the "misuse" of Device Filters as a workaround. The following listing shows how this works.

Two Master attributes will be placed within the @Page directive of the Content Page. One is the standard value, and the other is device specific. Additionally, the TestDeviceFilter method, which is normally used for the evaluation of the Device Filter, has to be overwritten. The alternative Master Page will be switched on if the word "print" is part of the query string.

 <%@ page language="C#" master="~/main.master"    printcontent:master="~/print.master" %> <script runat="server" language="c#">   public override bool TestDeviceFilter(string deviceFilterName)   {     if (deviceFilterName.Equals("printcontent"))     {         if (this.Request.QueryString["print"] != null)         {             return true;         }     }     return base.TestDeviceFilter(deviceFilterName);   } </script> <asp:content      contentplaceholder     runat="server">     Test Test Test Test Test Test Test Test     Test Test Test Test Test Test Test Test     Test Test<br />     <br />     <asp:hyperlink  runat="server"         navigateurl="Default.aspx?print=1">         Print me!     </asp:hyperlink> </asp:content> 

Tip

If you intend to use this type of approach, I recommend you derive a central class from the page, provide it with functionality, and derive the single pages from this class.

Tip

Naturally, you can use the device-specific application of Master Pages in its original sense in different devices (PDA, Pocket PC, mobile device, etc.) with different templates.




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net