Dynamically Setting CSS Values Using ASP.NET

Dynamically setting CSS values using ASP.NET

Is keeping the CSS file on the image server an option? If that it possible, you could make all the image references relative, and then you just need to update the link to the css file.

<link rel="stylesheet" href="<%= ConfigurationManager.AppSettings("css-server") %>style.css" />

If you still want to send or generate a css file dynamically:

css files don't have to end in css. aspx is fine. You could do this:

<link rel="stylesheet" href="style.aspx" />

and then in your style.aspx page:

protected void page_load(){
Response.ContentType = "text/css";
if (ConfigurationManager.AppSettings("css-server") == "local") {
Server.Transfer("css/local.css");
} else {
Server.Transfer("css/production.css");
}
}

If you still want to dynamically generate a css file, I'd use an HttpHandler, set the contenttype to "text/css", then generate the css with Response.Write. If you insist on having the page end in css, you could always register css to go to asp.net in IIS, then on incoming requests in global.asax application_Begin request, if the file ends in .css, use httpcontext.current.rewritepath to your handler.

This will have a net effect of style.css being dynamically generated at runtime.

CSS Style not being set Dynamically in ASP.NET Code behind

It was actually a simple error. I've used the wrong code to change the css class of the span named "followbtn_mainbtn".

I replaced this

followbtn_mainbtn.Attributes.CssStyle.Add("className", "follow-btner-no-hoverer");

with this

followbtn_mainbtn.Attributes["class"] = "follow-btner-no-hoverer";

Voila! Done deal. Thanks for all your answers and comments though :)

Change css dynamically from controller

You can use your model or ViewBag.

In controller pass your data into an object like circle.MarginLeft = 12 or use ViewBag['marginLeft'] = 12

and in your .cshtml file set

<div class="circle" style="margin-left:@ViewBag.marginLeft"></div>

or

<div class="circle" style="margin-left:@Model.marginLeft"></div>


Related Topics



Leave a reply



Submit