I think NullReference provided you with MVC solution because you checked the message "mvc". If you use ASP.NET web forms, you can use the same method that you use when creating CSS links on the fly on user controls. On the Page_Init event page, do something like the following (in the example below, I am linking to jquery-ui-CSS):
protected void Page_Init(object sender, EventArgs e) { System.Web.UI.HtmlControls.HtmlLink jqueryUICSS; jqueryUICSS = new System.Web.UI.HtmlControls.HtmlLink(); jqueryUICSS.Href = "styles/jquery-ui-1.8.13.custom.css"); jqueryUICSS.Attributes.Add("rel", "stylesheet"); jqueryUICSS.Attributes.Add("type", "text/css"); Page.Header.Controls.Add(jqueryUICSS); }
If you want the actual elements to appear in the header, use HtmlGeneric control instead of HtmlLink in my example above. This is still the same method - on the page_Inte, add to the collection Page.Header.Controls:
protected void Page_Init(object sender, EventArgs e) { System.Web.UI.HtmlControls.HtmlGenericControl mystyles; mystyles = new System.Web.UI.HtmlControls.HtmlGenericControl(); mystyles.TagName = "style"; string sampleCSS = "body { color: Black; } h1 {font-weight: bold;}"; mystyles.InnerText = sampleCSS; Page.Header.Controls.Add(mystyles); }
source share