Master Pages and User Controls


At first glance, it seems to be strange, but it's actually possible to combine user controls and Master Pages without limitations and with the aim of reusing existing layouts. Therefore, the @Control directive offers a "master" attribute similar to @Page.

The sample in Listing 4-6 shows such an approach in combination with Master Pages, a user control, and a page putting it all together. The Master Page contains a table with two ContentPlaceHolder controls in it. Those are filled through the user control. As you can see from Figure 4-7, all elements are displayed correctly. Of course, you could also think about adding a Master Page for the page itself.

click to expand
Figure 4-7: Gosh—it works!

Listing 4-6: Combining User Controls and Master Pages

start example
 // UCMaster.master <%@ master language="C#" %> <table  bordercolor="black" cellspacing="1" cellpadding="1" width="100%"     border="2">     <tr>        <td>            <asp:contentplaceholder  runat="server">            </asp:contentplaceholder>        </td>        <td>        </td>        <td>        </td>     </tr>     <tr>        <td>        </td>        <td>This is my Master Page!</td>        <td>        </td>     </tr>     <tr>        <td>        </td>        <td>        </td>        <td>            <asp:contentplaceholder  runat="server">            </asp:contentplaceholder>        </td>     </tr> </table> // UCMaster.ascx <%@ control language="C#" classname="UCMaster" master="~/UCMaster.master" %> <asp:content      contentplaceholder     runat="server">     This is my User Control </asp:content> <asp:content      contentplaceholder     runat="server">     This is my User Control, too </asp:content> // UCMaster.aspx <%@ page language="C#" %> <%@ register tagprefix="uc1" tagname="UCMaster" src="/books/2/697/1/html/2/~/UCMaster.ascx" %> <script runat="server"> </script> <html> <head runat="server">     <title>Untitled Page</title> </head> <body>     <form runat="server">         <p>This text is embedded in the page itself.</p>         <uc1:ucmaster  runat="server"></uc1:ucmaster>         <p>This text is again embedded in the page.</p>     </form> </body> </html> 
end example




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