Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update stylesheet on each page request?
    primarykey
    data
    text
    <p>In my ASP.NET WebForms project I need to update stylesheet on each page request.</p> <p>No problem for now - I added css.ashx handler</p> <pre><code>public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/css"; Random r = new Random(); int red = r.Next(0, 255); int green = r.Next(0, 255); int blue = r.Next(0, 255); string css = string.Format("body {{ background-color: rgba({0}, {1}, {2}, 1); }}", red, green, blue); context.Response.Write(css); } </code></pre> <p>and put it to Master page:</p> <pre><code>&lt;link href="/Scripts/css.ashx?tmp=&lt;%=(new Random()).Next() %&gt;" rel="stylesheet" type="text/css" /&gt; </code></pre> <p>Still no problems. Css will be loaded per each request.</p> <p>Problems appear with UpdatePanels:</p> <pre><code>&lt;asp:UpdatePanel runat="server"&gt; &lt;ContentTemplate&gt; &lt;asp:Button Text="Update" runat="server" OnClick="btnOk_Click" ID="btnOk" /&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; </code></pre> <p>Pressing on "Update" button, css will not be updated. It's correct behavior, but I need to find work around.</p> <p>My suggestion is to move <code>&lt;link href="/Scripts/css.ashx" rel="stylesheet" type="text/css" /&gt;</code> block to another <code>UpdatePanel</code> with <code>UpdateMode="Always"</code> in MasterPage:</p> <pre><code>&lt;asp:UpdatePanel runat="server" UpdateMode="Always"&gt; &lt;ContentTemplate&gt; &lt;link href="/Scripts/css.ashx?tmp=&lt;%=(new Random()).Next() %&gt;" rel="stylesheet" type="text/css" /&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; </code></pre> <p>It works perfectly, but I can add this block in form element only. In result I have invalid HTML document (link element located in body, not head).</p> <p>How can I prevent it? Any ideas? I don't want to register javascript per each request to update link href.</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.
    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