Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding StyleSheets Programmatically in Asp.Net
    primarykey
    data
    text
    <p>I want to add StyleSheets programmatically in the head section but one of the examples I saw seemed to need to many lines of code to add just one style sheet even though I may need a lot:</p> <p><strong>Example Code:</strong></p> <pre><code>HtmlLink css = new HtmlLink(); css.Href = "css/fancyforms.css"; css.Attributes["rel"] = "stylesheet"; css.Attributes["type"] = "text/css"; css.Attributes["media"] = "all"; Page.Header.Controls.Add(css); </code></pre> <p>I also use <code>Page.Header.RenderControl()</code> method but it didn't work either. Object null something error was thrown.</p> <p>I also used <code>Page.Header.InnerHtml</code> and <code>InnerText += "&lt;link .... "/&gt;</code> things but they threw the Literal error which is I think common error.</p> <p>I used this code :</p> <pre><code>List&lt;Literal&gt; cssFiles = new List&lt;Literal&gt;(); cssFiles.Add(new Literal() { Text = @"&lt;link href=""" + ResolveUrl("~/Resources/Styles/MainMaster/MainDesign.css") + @""" type=""text/css"" rel=""stylesheet"" /&gt;" }); cssFiles.Add(new Literal() { Text = @"&lt;link href=""" + ResolveUrl("~/Resources/Styles/MainMaster/MainLayout.css") + @""" type=""text/css"" rel=""stylesheet"" /&gt;" }); AddStyleRange(cssFiles); private void AddStyleRange(List&lt;Literal&gt; cssFiles) { foreach (Literal item in cssFiles) { this.Header.Controls.Add(item); } } </code></pre> <p>It worked at first but when I change the pages it stopped working. </p> <p>I am using Master Page and I am writing these codes on <code>Master.cs</code> file and also some people recommended to use <code>this.Header</code> instead of <code>Page.Header</code> but when I built it throws an error which says I cannot declare that like this.</p> <p>It shouldn't be that hard to add many styles. </p> <p>It is getting complicated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload