Use CSS on the master page content page

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.