I believe everyone has used the nested masterpage function under ASP.NET 2.0. This function is very powerful and allows users to create template pages first and then easily apply them to other pages. Moreover, template pages can be nested, but we will find a problem with nested template pages, that is, when using nested template pages, there is no way to perform visual operations on them in the design view state. , so how to solve this problem? I searched the foreigner's blog and found a method provided by the foreigner. For the time being, I can take a detour to solve this problem. I will explain it below:
We can write a base class called basepage.cs and put it in the app_code directory. In this class, add a class called basepage.cs.
The attribute of runtimeMasterPageFile is a string type that specifies which template file to use during runtime and overrides OnPreInit.
Method, the code is as follows:
public class BasePage : System.Web.UI.Page
{
private string runtimeMasterPageFile;
public string RuntimeMasterPageFile
{
get
{
return runtimeMasterPageFile;
}
set
{
runtimeMasterPageFile = value;
}
}
protected override void OnPreInit(EventArgs e)
{
if (runtimeMasterPageFile != null)
{
this.MasterPageFile = runtimeMasterPageFile;
}
base.OnPreInit(e);
}
}
Next, we construct a template page called mainmaster.master, randomly add header and footer information in it, leave a contentplaceholder called MainContent in the middle, and then build a template page called submaster.master, in which
MasterPageFile="~/MainMaster.master", to apply the mainmaster template page, put a table with one row and two columns, as follows:
<asp:Content ID="foo" ContentPlaceHolderId="MainContent" runat="server">
<table>
<tr>
<td width="300">
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 this
<%@ Page Language="C#" MasterPageFile="" RuntimeMasterPageFile="SubMaster.master" CodeFileBaseClass="BasePage" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
As you can see, here, we do not set the properties of masyterpage, but specify the properties of RuntimeMasterPageFile as
submaster.master, this is a template that is loaded at runtime, and the codefilebaseclass attribute specifies the class basepage.cs we just wrote. In this way, we can see the pull in the design view state of this ASPX and can drag and drop the design. .
It should be noted that according to Microsoft, it is said that it will not be until the next version of VISUAL STUDIO that full support for switching the full design view state when templates are nested.
I have provided a download of an example of this article, click here to download.