Option 1 Instead of modifying your CSS file, different classes are based on the session.
.main { padding: 0px 12px; margin: 0px 0px 0px 0px; min-height: 630px; width:auto; } .agency1 { background-image: url('agency1.png'); } .agency2 { background-image: url('agency2.png'); } .agency3 { background-image: url('agency3.png'); }
Then add two classes to the div
<div class="main <%=Session["agency"]%>"></div>
Option 2 Create a generic handler that displays specific CSS and adds it to your page
<link href="GenerateCss.ashx" rel="stylesheet" />
In your GenerateCss.ashx.cs file you will have something like this
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string image = "main"; if (context.Session != null && context.Session["agency"] != null) { image = context.Session["agency"].ToString(); } string result = ".main{padding: 0px 12px; margin: 0px 0px 0px 0px; min-height: 630px; width:auto; background-image:url('" + image + ".png');}"; context.Response.Write(result); }
Be extremely careful, as this can lead to an XSS attack if used improperly . You need to make sure that the ["agency"] session is not controlled by the user. By this, I mean that the user cannot provide this value, as this will allow them to enter anything there.
I do not recommend the second option, though, because you will name it for each request, and not to continue to generate CSS when you can just make it static. If you can use option 1, I would say that it would be better.
source share