HighTechTalks DotNet Forums  

Checking for the existence of a dynamically added style

ASP.net Web Controls microsoft.public.dotnet.framework.aspnet.webcontrols


Discuss Checking for the existence of a dynamically added style in the ASP.net Web Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Nathan Sokalski
 
Posts: n/a

Default Checking for the existence of a dynamically added style - 04-03-2010 , 12:05 AM






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
njsokalski (AT) hotmail (DOT) com
http://www.nathansokalski.com/

Reply With Quote
  #2  
Old   
Nathan Sokalski
 
Posts: n/a

Default Re: Checking for the existence of a dynamically added style - 04-04-2010 , 10:25 AM






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
njsokalski (AT) hotmail (DOT) com
http://www.nathansokalski.com/

"Nathan Sokalski" <njsokalski (AT) hotmail (DOT) com> wrote

Quote:
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
njsokalski (AT) hotmail (DOT) com
http://www.nathansokalski.com/

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.