asp.net|css|erp| optimization asp.net 2.0 adds built in MasterPage support, which is a great convenience for us. However, after a period of use, I found that MasterPage is not so perfect: nested MasterPage cannot support the design-time interface, as well as the question of adding CSS to the content Page mentioned below.
usually, before 2.0, the syntax for adding a CSS reference to the page is as follows:
< p>
was originally a very common practice. But in a MasterPage page, there's a very awkward situation: where do we put the code?
because of the MasterPage content page, you can only define the contents of <asp:content/> tags. We do not control the page's
at the same time, we are not able to put a ContentPlaceHolder in MasterPage's
so my approach 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);
}
}
so, on the specific page, we can add CSS references by following code:
protected void Page_Load (object sender, EventArgs e)
{
Controlhelper.addstylesheet (this. Page, "Css/projectpage.css");
}
Also, this code supports use in a specific content page, or in a nested Master page.
said there may be some people will ask, why do you want to separate CSS to load it? Is it necessary? Wouldn't it be nice to have all the CSS in my pages defined in a few public CSS files?
In fact, friends familiar with the Web standards of HTML production must know that in a more perfect State, page HTML and the CSS for performance should be completely separated. Our web-standard design, in general, is to first make a positioning code for each frame div in the Master Page, as well as the header,footer part of the cosmetic code. These are for every content page to use, will be placed in a unified CSS inside. Other specific content pages, each page will have their own different content layout, decoration style, so I put each specific part of the page in its own CSS. This creates a CSS model that merges layers by layer, according to the implementation level of Master Page (nested). The advantage is that the separation of responsibilities for each CSS file is achieved and is easier to understand and maintain.