0

I'm new to ASP so a complete noob with C#, however I am strong in css/html and know some Javascript.

I have an ASP.NET web-app using an external "style.css" - please note that I hate using inline styles.

I want to modify the colour of an element based on user credentials from the server.

(In style.css): .userColour {background-color: #3c3c3c;}

So when the user logs in: (In C# of page):

  • fetch user colour (for this user) from server (assume user has already been verified)
  • scan style.css for the #3c3c3c colour value
  • overwrite colour value to new users colour #whatever
  • serve the page

Getting the user colour from the server probably has too many methods to warrant an answer here - I just need to be shown some basic method of addressing the style.css and iterating through it overwriting the colour value and saving it on completion.

Please note: I don't want to add or modify css classes server-side, or add inline styles, etc. I want to have the flexibility to assign ANY colour value at a later stage and for the app this is the most appropriate solution.

Thanks.

2
  • Thanks for excluding all of the techniques that might actually work. Commented Apr 2, 2014 at 23:29
  • Replacing values in the CSS file at runtime sounds unusual. What if you don't have write access to the css folder? What about browser caching? Why not have selectors for each role then replace that at runtime? Similar to @recursive's answer. Or even a CSS for each role then replace that external link in your master page, layout view,etc? Commented Apr 2, 2014 at 23:44

1 Answer 1

2

You can create a stylesheet that has a codebehind just like an aspx file. Here is a tutorial. http://cfouquet.blogspot.com/2006/06/making-dynamic-css-content-with-aspnet.html

Basically, your css template looks like this:

<%@ Page Language="C#" %> 
h1 { background-color:<%= ColorManager.Color %>; }
Sign up to request clarification or add additional context in comments.

3 Comments

That's pretty cool, although I don't think it meets the restrictions the OP placed in the last paragraph of his question.
I assumed that "change css classes server-side" meant change which CSS classes were assigned to DOM elements, not change the contents of CSS rules. But it does seem ambiguous.
That tutorial is along the lines of what I need to do - thanks Recursive. Also Recursive is correct in that I am happy to change the contents of the css rules and wanted to avoid changing CSS in the DOM because the Telerik plugins have complex contextual selectors that would be further complicated by not using static class names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.