开发者

Dynamically loading CSS elements in asp.net

I searched google and SO, but did not find a solution.

I have an asp.net web app that allows users to customize some GUI aspects (such as font color, add a custom logo etc). This information is stored in the user profile database.

I have a css file that populates font colors etc. The main page, loads the users profile from the database, and customizes the page accordingly.

Aside from having e开发者_如何学运维ach label with its own "font-color=", how can I display the CSS elements, based on the user profile information returned from the database? Thx


You can include an extra CSS file that points to an ASPX page:

<link rel="stylesheet" type="text/css" href="/CustomStyles.aspx" />

Then in CustomStyles.aspx change the default content-type:

Response.Clear()
Response.ContentType = "text/css"

Then just start outputting your styles:

Response.Write("#awesome-button{color:" & ColorFromDatabase & ";}"

Make sure that this file is included after the other styles so that it takes precedence. You might want to also throw an !IMPORTANT in there, too.

Response.Write("#awesome-button{color:" & ColorFromDatabase & " !IMPORTANT;}"


It depends on how you have the information stored, but you can add styling to elements through code like this:

Button1.Style["font-weight"] = "bold";

Or you can just apply a CSS class to the control:

Button1.CssClass = "buttonStyle";


You could have a page that just returns a CSS file based on the preferences stored in the database. So you would have:

<link rel="stylesheet" href="somepage.aspx?userid=<%=userID%>">

You could probably even do that easily enough with a classic ASP page, a web service, etc.

The point is that that page would generate the same basic stylesheet, filling in the right colors etc. that the user has chosen. This way you don't have to perform a bunch of style changes in server-side or client-side code after the page has loaded, or mix your user preference code in with your HTML, or change much about the base pages if you want to change the way the stylesheet works. It also makes it easy to test your stylesheet outside of testing the site itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜