Use CSS on the master page content page
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 <Header/> content of the page in Aspx. The <link/> label must be placed in <Header/>. I tried to add this line of code in <asp: Content/>, but an error will be prompted.
At the same time, we cannot put a contentplaceholder inside the <Header/> of masterpage for reference code to put CSS in the future.
Therefore, my practice is to define a helper class as follows:
Static public class controlhelper
{
Static public void addstylesheet (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.