Checking for the existence of a dynamically added style

  • Thread starter Nathan Sokalski
  • 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.
 
N

Nathan Sokalski

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top