How to add stylesheets trough the ScriptManager in custom controls?

Last post 02-01-2007 11:57 AM by BartHolvoet. 3 replies.

Sort Posts:

  • How to add stylesheets trough the ScriptManager in custom controls?

    01-31-2007, 5:31 AM
    • Loading...
    • BartHolvoet
    • Joined on 01-31-2007, 5:28 AM
    • Posts 2
    This works fine under normal circumstances:
    Page.Header.StyleSheet.CreateStyleRule(Style, urlResolver, selector);

    But it fails with the following scenario:
    The custom control's visible property is initally set to false.
    A button click sets the visible property to true.
    The stylesheet has no effect.

    Is there any possibility to add the stylesheet through the ScriptManager like you would do with JavaScript?
    (ScriptManager.RegisterClientScriptBlock(...))

    Using VS 2005, ASP.NET 2.0, AJAX 1.0
  • Re: How to add stylesheets trough the ScriptManager in custom controls?

    01-31-2007, 12:24 PM
    • Loading...
    • paul.vencill
    • Joined on 02-01-2006, 12:57 PM
    • Gaithersburg, MD
    • Posts 681

    I'm not entirely sure I follow what you're trying to do.  But to answer the asked question, no the scripmanager has nothing to do w/ the stylesheet.

    If you're asking whether scripts can change styles on DOM elements, then  yes they can and the best way to do that (ime) is to define the style you want them to have as a class in  your css file and then toggle that class on and off as needed.

  • Re: How to add stylesheets trough the ScriptManager in custom controls?

    01-31-2007, 2:44 PM
    • Loading...
    • bogdanb
    • Joined on 07-30-2006, 7:12 AM
    • Posts 29

    Hi Bart,

     

    Please find bellow a solution for registering a css stylesheet during partial rendering for example:

    <%@ Page Language="C#" AutoEventWireup="true" %>
    <script runat="server">
     
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "css", "var a= createCSSRef('style.css');document.getElementsByTagName('HEAD')[0].appendChild(a);", true);
        }
    </script>
    <!DOCTYPE html PUBLIC "-//W3C//DTD  1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
      
    </head>
    <body>
        <script language="javascript">
        function createCSSRef(url)
        {
         var obj = document.createElement("link");
         obj["href"] = url;
         obj["rel"] = "stylesheet";
         obj["type"] ="text/css";
         return obj;
        
        }

        </script>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server"  >           
            </asp:ScriptManager>
            <div>
            <div id="test">Test</div>
            </div>
        </form>
    </body>
    </html>

    where the style.css file contains:

    #test{

        color:red; 

     The output is that the text in the test block element is red.

     

    I hope this helps,

    Bogdan 

  • Re: How to add stylesheets trough the ScriptManager in custom controls?

    02-01-2007, 11:57 AM
    • Loading...
    • BartHolvoet
    • Joined on 01-31-2007, 5:28 AM
    • Posts 2

    Thanks for the responses.

     Bogdan:

    Your solutions looks like what I'm searching for, except that I'm not using an external .css file.

    I'll see if I can get it to work with a dynamic stylesheet to.

     

    Further explanation of my problem: 

    My stylesheets are dynamically generated using the custom control's properties and rendered using the CreateStyleRule method.

    for instance: Page.Header.StyleSheet.CreateStyleRule(myStyle, null, "BODY");

     This works fine except when the custom control is not visible on the first page_load.

    The control is only set to visible after a postback from a Button click, and the stylesheet does not work in that scenario.
     

     


     

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter