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 );
}