locked
Dynamic CSS Styles RRS feed

  • Question

  • User960677137 posted

    I want to be able to create a style sheet at session startup (based on a large number of variables... this sheet must be created on the fly and can not be hard coded).

    At the moment i can set up styles and apply them to controls using the VS designer (html behind of a page). But i need to be able to create these styles at runtime because they will differ for each session. The names of the styles may not differ, but the elements inside the style will be changing.

    I figured that one thing i could do is create a css file using System.IO to create the file and then use that to apply (but even then, im not sure how i go from creating the file to including it in the project at runtime), but is there an easier way?

    Monday, June 7, 2010 7:27 PM

Answers

  • User1759999623 posted

    While this is not the efficient solution, you can always build an HtmlGenericControl that is an HTML style block and populate it on the fly.  This way, you would not need to build a CSS file.

    ASPX HTML code:

    <style id="hgcStyle" runat="server" />

    C# code-behind (in Page_Load):

    hgcStyle.Attributes.Add("type", "text/css");
    hgcStyle.InnerHtml = ".myclass{height:10px;background-color:Aqua;}";



     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 8, 2010 12:43 AM

All replies

  • User1759999623 posted

    While this is not the efficient solution, you can always build an HtmlGenericControl that is an HTML style block and populate it on the fly.  This way, you would not need to build a CSS file.

    ASPX HTML code:

    <style id="hgcStyle" runat="server" />

    C# code-behind (in Page_Load):

    hgcStyle.Attributes.Add("type", "text/css");
    hgcStyle.InnerHtml = ".myclass{height:10px;background-color:Aqua;}";



     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 8, 2010 12:43 AM
  • User-1802908944 posted

    hope this will help you. and you can customize as you want.


    1. import TextField.StyleSheet;
    2. var css:String = "a { color:#FF0000; text-decoration:underline; }"
    3.                + "a:hover { color:#0000FF; }";
    4. var style:StyleSheet = new StyleSheet();
    5. style.parseCSS( css );
    6. textField.styleSheet = style;
    7. textField.htmlText = 'Ultra<a href="http://...">shocked</a>';

    http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS


    http://www.codeproject.com/KB/aspnet/ASPNET_Dynamic_Styling.aspx

    with jquery

    http://api.jquery.com/category/css/

    happy codiing

    Tuesday, June 8, 2010 12:52 AM