I believe everyone has used the nested masterpage function in Asp.net 2.0. This function is very powerful and allows users to create a template page first, then it is easy to apply to other pages. The template page can also be nested, but the nested template page, we will find that there is a problem, that is, when using the nested template page, in the design view state, there is no way to visualize it. How can this problem be solved? I found a foreigner's blog and found a method provided by a foreigner. Now I can take a detour to solve this problem. The following explains:
we can write a base class, it is called basepage. CS: Put it in the app_code directory. In this class, add an attribute called
runtimemasterpagefile, which is a string type and specifies which template file is used during running, and rewrite the onpreinit
method. The Code is as follows:
public class basepage: system. web. UI. page
{< br> private string runtimemasterpagefile;
Public String runtimemasterpagefile
{< br> Get
{< br> return runtimemasterpagefile;
}< br> set
{< br> runtimemasterpagefile = value;
}< BR >}
Protected override void onpreinit (eventargs E)
{
If (runtimemasterpagefile! = NULL)
{
This. masterpagefile = runtimemasterpagefile;
}
Base. onpreinit (E );
}
}
Next, we construct a template page called mainmaster. Master, which creates a header and footer information, leaving
Contentplaceholder of maincontent, and then create a template page named submaster. master.
Masterpagefile = "~ /Mainmaster. Master "to apply the mainmaster template page. Put a table with one row and two columns, as shown below:
<Asp: Content ID = "foo" contentplaceholderid = "maincontent" runat = "server">
<Table>
<Tr>
& Lt; TD width = "300" & gt;
Left column in submaster
<Br/>
<Asp: contentplaceholder id = "leftcolumn" runat = "server">
</ASP: contentplaceholder>
</TD>
<TD>
Right column in submaster
<Br/>
<Asp: contentplaceholder id = "rightcolumn" runat = "server">
</ASP: contentplaceholder>
</TD>
</Tr>
</Table>
</ASP: content>
Finally, in An ASPX page, specify
<% @ Page Language = "C #" masterpagefile = "" runtimemasterpagefile = "submaster. master "codefilebaseclass =" basepage "autoeventwireup =" true "codefile =" default. aspx. CS "inherits =" _ default "Title =" untitled page "%>
We can see that here, we do not set the attributes of masyterpage, but specify the attributes of runtimemasterpagefile
Submaster. master. This is the template loaded at runtime, And the codefilebaseclass attribute specifies
The class basepage. CS, so that we can see the pull in the design view of this aspx, you can drag the design.
It should be noted that, according to Microsoft, it is said that only the next version of Visual Studio can fully support the full design view during template nesting.
Switching status
Even provide download of an example in this article, click here to download the http://files.cnblogs.com/jackyrong/Nestedsample.zip