Added built-in MasterPage support in ASP. NET 2.0, which is a great convenience for us. However, after a period of use, I found that MasterPage is not so perfect: The nested MasterPage cannot support the design-time interface, and the problem of adding CSS to the Content Page mentioned below.
Generally, before 2.0, we need to add a CSS Reference syntax on the page as follows:
<Link rel = "stylesheet" href = "css/test.css"/>
This is a common practice. However, in a MasterPage subpage, there is a very embarrassing situation: Where should we place the above Code?
Because the Content page of MasterPage can only define the Content of <asp: Content/> labels one by one. In general, we cannot control the At the same time, we cannot put a ContentPlaceHolder inside the Therefore, my practice is to define a helper class as follows:
Static public class ControlHelper
{
Static public void AddStyleSheet (Page page, string cssPath)
{
HtmlLink link = new HtmlLink ();
Link. Href = cssPath;
Link. Attributes ["rel"] = "stylesheet ";
Link. Attributes ["type"] = "text/css ";
Page. Header. Controls. Add (link );
}
}
In this way, on the specific page, we can add CSS reference using the following code:
Protected void Page_Load (object sender, EventArgs e)
{
ControlHelper. AddStyleSheet (this. Page, "css/projectPage.css ");
}
In addition, this code can be used in a specific content Page or a nested Master Page.
Some may ask why we need to split the CSS into such a file to load the file? Is it necessary? Isn't it good that I define the CSS of all pages into several public CSS files?
Actually, be familiarWeb standardsYou may know that in the perfect state, the HTML of the page and the CSS used for representation should be completely separated. Based on the web standard design, we usually first make the positioning code of each frame div in the Master Page, as well as the modifier code of header and footer. These are required for every content page and will be placed in a unified CSS. For other specific content pages, each page has different content la S and styles. Therefore, I put specific parts of each specific page in its own CSS. This forms an implementation level based on the Master Page (Nesting),Layer-by-layer merge. The advantage is that each CSS fileSeparation of duties, Easier to understand and maintain.
Since ASP. NET 2.0 has been around for a short time, the above is only a little bit of personal experience. If you have a more convenient solution, please kindly advise.