background how to dynamically add JS file and css file reference

  • 2021-01-11 01:58:16
  • OfStack

First add the namespace using System.Web.UI.HtmlControls;

The code dynamically adds references to css files

HtmlGenericControl myCss = new HtmlGenericControl();
myCss .TagName = "link";
myCss .Attributes.Add("type", "text/css");
myCss .Attributes.Add("rel", "stylesheet");
myCss .Attributes.Add("href", ResolveUrl(Page.ResolveClientUrl("css The file path ")));
this.Page.Header.Controls.AddAt(0, myCss );

The code dynamically adds references to JS files

HtmlGenericControl myJs = new HtmlGenericControl();
myJs .TagName = "script";
myJs .Attributes.Add("type", "text/javascript");
myJs .Attributes.Add("src", ResolveUrl(Page.ResolveClientUrl("js The file path ")));
this.Page.Header.Controls.AddAt(1, myJs );

It's an easy way to do it. Let's record 1 here

Related articles: