Asp.net background to add CSS JS Meta tag method

  • 2020-10-23 20:04:59
  • OfStack

The following is how to write CSS, JS, Meta tags from Asp.net background. We write it as a function for future use. If the function is in the page class, the Page parameter can also be left out.
First import the namespace using System. Web. UI. HtmlControls;


/// 
///  add JS Script links  
/// 
///  page  
///  The 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 The script content  
/// 
///  page  
///  The 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 CSS Style links  
/// 
///  page  
///  The 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 Content of the style  
/// 
///  page  
///  Content of the style  
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 Meta The label  
/// 
///  page  
/// Meta The name  
/// Meta content  
public void AddMeta(System.Web.UI.Page page, string name, string content) 
{ 
HtmlMeta meta = new HtmlMeta(); 
meta.Name = name; 
meta.Content = content; 
page.Header.Controls.Add(meta); 
} 


Related articles: