Optimized CSS model used in ASP. NET 2.0 with Master Page

Source: Internet
Author: User

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.

Related Article

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.