Add a style sheet file to the ASP.net page dynamically

Source: Internet
Author: User
asp.net| Dynamic | style sheet

Because at some point in order to save the cost of the stylesheet download, we will divide the CSS into different files, and call with only the available
In. NET WEB programming based on template pages, you sometimes encounter the problem of how to dynamically add CSS style sheet files to the head tag.

I've thought about some ways in the past.

For example, write some if else statements directly in the head to determine the current access page

But this is definitely not the best choice.
Next I'll introduce another better way to add

Write the following code in the Page_Init of the page
Protected Sub Page_Init (ByVal sender as Object, ByVal e as System.EventArgs) Handles Me.init
Dim Myhtmllink as New htmllink ()
Myhtmllink.href = "~/css/msg.css"
MYHTMLLINK.ATTRIBUTES.ADD ("rel", "stylesheet")
MYHTMLLINK.ATTRIBUTES.ADD ("type", "Text/css")
PAGE.HEADER.CONTROLS.ADD (Myhtmllink)
End Sub to solve this problem.

The same can be added meta
Dim hm1 as New Htmlmeta ()
Dim HM2 as New Htmlmeta ()
Dim Head as HtmlHead = Page.header

Hm1. Name = "keywords"
Hm1. Content = "Words that describe your Web page"
Head. Controls.Add (HM1)
HM2. Name = "Date"
HM2. Content = DateTime.Now.ToString ("Yyyy-mm-dd")
HM2. Scheme = "Yyyy-mm-dd"
Head. Controls.Add (HM2)



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.