Checking for the existence of a dynamically added style

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

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.
 
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.
 
Back
Top