In case of dotnetnuke, we should not add JS and CSS files by hard coding the path. It will work in case of asp.net as well.
using System.Web.UI.HtmlControls;
This namespace is required for HtmlGenericControl.
override protected void OnInit(EventArgs e)
{
HtmlGenericControl js1 = new HtmlGenericControl(“script”);
js1.Attributes[“type”] = “text/javascript”;
js1.Attributes[“src”] = this.TemplateSourceDirectory + “/js/Script.js”;
Page.Header.Controls.Add(js1);
HtmlGenericControl css1 = new HtmlGenericControl(“link”);
css1.Attributes[“type”] = “text/css”;
css1.Attributes[“href”] = this.TemplateSourceDirectory + “/css/Style.css”;
css1.Attributes[“rel”] = “stylesheet”;
css1.Attributes[“media”] = “screen”;
Page.Header.Controls.Add(css1);
}
Another way to do the same,
In design page:
<head>
<script id=”myscript” type=”text/javascript” runat=”server”></script>
<link id=”mystyle” rel=”stylesheet” type=”text/css” media=”screen” runat=”server” />
</head>
In code behind:
override protected void OnInit(EventArgs e)
{
myscript.Attributes[“src”] = this.TemplateSourceDirectory + “/js/Script.js”;
mystyle.Attributes[“href”] = this.TemplateSourceDirectory + “/css/Style.css”;
}
Such a well written post.. Thanks for sharing this post!