Checking for the existence of a dynamically added style

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Apr 3, 2010.

  1. I have a custom control that I would like to have add style rules to the
    Page that contains it. I know how to do this, using the following method:

    Me.Page.Header.StyleSheet.CreateStyleRule

    However, I only want to add the style rule once, since it is the same for
    every instance of the custom control. When adding scripts, I would simply
    use the following two statements to add a script and check whether it was
    already added:

    Me.Page.ClientScript.RegisterClientScriptBlock
    Me.Page.ClientScript.IsClientScriptBlockRegistered

    But I could not find anything like this for stylesheets. What would be the
    best way to have a custom control add a style rule and make sure it is only
    added once? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Apr 3, 2010
    #1
    1. Advertising

  2. I think I may have found a solution. Here is the code I added:

    At the beginning with the other declarations, declare the HtmlLink as a
    Shared property:

    Private Shared linktag As New HtmlLink

    Then check whether this control exists in the header controls:

    If Not Me.Page.Header.Controls.Contains(linktag) Then
    CSSImageMap.linktag.Attributes.Add("rel", "stylesheet")
    CSSImageMap.linktag.Attributes.Add("type", "text/css")
    CSSImageMap.linktag.Href = "mystyle.css"
    Me.Page.Header.Controls.Add(linktag)
    End If

    If anybody as any comments on this technique (or if I am missing something
    that would cause a problem in it), I would greatly appreciate it. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Nathan Sokalski" <> wrote in message
    news:...
    > I have a custom control that I would like to have add style rules to the
    > Page that contains it. I know how to do this, using the following method:
    >
    > Me.Page.Header.StyleSheet.CreateStyleRule
    >
    > However, I only want to add the style rule once, since it is the same for
    > every instance of the custom control. When adding scripts, I would simply
    > use the following two statements to add a script and check whether it was
    > already added:
    >
    > Me.Page.ClientScript.RegisterClientScriptBlock
    > Me.Page.ClientScript.IsClientScriptBlockRegistered
    >
    > But I could not find anything like this for stylesheets. What would be the
    > best way to have a custom control add a style rule and make sure it is
    > only added once? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
     
    Nathan Sokalski, Apr 4, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam
    Replies:
    2
    Views:
    3,963
    Steve C. Orr, MCSD
    Aug 14, 2003
  2. Bob Roberts
    Replies:
    3
    Views:
    346
    Terry Reedy
    Oct 12, 2003
  3. Jim Hunter
    Replies:
    3
    Views:
    612
    Jim Hunter
    Mar 6, 2005
  4. AndyB
    Replies:
    1
    Views:
    293
    Paul Hankin
    Sep 28, 2007
  5. Nathan Sokalski
    Replies:
    1
    Views:
    484
    Nathan Sokalski
    Apr 4, 2010
Loading...

Share This Page