tags:

views:

87

answers:

2

hi, i got a user control which implements a reference to a css file, now i want to use this user control many times on page, and this will lead to duplication of inclusion of the css file.

with java scripts it is simple by using the scriptmanager.

so what is your suggestion for a solution or similar approach to the scriptmanager?

+2  A: 

Here's a technique I've used before, although it may not be the best one:

    Dim cssLink As String = String.Format("<link rel='stylesheet' type='text/css' href='{0}/css/cssLink.css' />", Request.ApplicationPath)
    If (From c As LiteralControl In Page.Header.Controls.OfType(Of LiteralControl)() Where c.Text = cssLink).Count = 0 Then
        Page.Header.Controls.Add(New LiteralControl(cssLink))
    End If
Dave L
+1  A: 

There is no easy way to check if the styles are registered to the page like ClientScript utility.

If you register your styles as an external css file to page like that :

HtmlLink link = new HtmlLink();
link.Href = relativePath;
link.Attributes["type"] = "text/css";
link.Attributes["rel"] = "stylesheet";
Page.Header.Controls.Add(link);

You can then check if it exists by looping the page header's Controls collection and looking for the path of the CSS file.

Canavar
but wouldn't looping cause overhead ?
header's control collection will have only tags that has runat=server attribute. And probably the only control will be this css file in the collection if it's added :)
Canavar
thanks a lot mate i think this is the best way :)