How to add CSS, JS, and Meta tags to the Asp.net background

Source: Internet
Author: User

The following describes how to add CSS, JS, and Meta tags to the backend of Asp.net. Here we write functions for future use. If a function is placed in a Page class, the Page parameter is optional.
First, import the namespace using System. Web. UI. HtmlControls;
Copy codeThe Code is as follows:
///
/// Add the JS script Link
///
/// Page
/// Path
Public void AddJS (System. Web. UI. Page page, string url)
{
HtmlGenericControl jsControl = new HtmlGenericControl ("script ");
JsControl. Attributes. Add ("type", "text/javascript ");
JsControl. Attributes. Add ("src", url );
Page. Header. Controls. Add (jsControl );
}


///
/// Add JS script content
///
/// Page
/// Script content
Public void AddScript (System. Web. UI. Page page, string content)
{
HtmlGenericControl scriptControl = new HtmlGenericControl ("script ");
ScriptControl. Attributes. Add ("type", "text/javascript ");
ScriptControl. InnerHtml = content;
Page. Header. Controls. Add (scriptControl );
}


///
/// Add a CSS style Link
///
/// Page
/// Path
Public void AddCss (System. Web. UI. Page page, string url)
{
HtmlLink link = new HtmlLink ();
Link. Href = url;
Link. Attributes. Add ("rel", "stylesheet ");
Link. Attributes. Add ("type", "text/css ");
Page. Header. Controls. Add (link );
}


///
/// Add CSS style content
///
/// Page
/// Style content
Public void AddStyle (System. Web. UI. Page page, string content)
{
HtmlGenericControl styleControl = new HtmlGenericControl ("style ");
StyleControl. Attributes. Add ("type", "text/css ");
StyleControl. InnerHtml = content;
Page. Header. Controls. Add (styleControl );
}


///
/// Add a Meta tag
///
/// Page
/// Meta name
/// Meta content
Public void AddMeta (System. Web. UI. Page page, string name, string content)
{
HtmlMeta = new HtmlMeta ();
Meta. Name = name;
Meta. Content = content;
Page. Header. Controls. Add (meta );
}

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.