views:

316

answers:

2

I have a collection of cookies filled with Browserhawk information in masterpage.master.vb such as;

Dim useCSS as boolean = 0
Response.Cookies("Stylesheets").Value = brHawk.Stylesheets
if Response.Cookies("Stylesheets") = True then useCSS = 1

if Stylesheets is True I set useCSS to 1, if false I set useCSS to 0 I need to access these in the section of the masterpage.master such as;

if useCSS = true 
Then load stylesheet 
else 
Dont load stylesheet

I'm having problems finding the right syntax to get this working.

+3  A: 

Make useCSS variable public variable and write this code in your master file.

<% if ( useCSS == true ) { %>
  <link rel="stylesheet" href="" type="text/css" media="screen" />
<% } %>

Note : I am C# Guy :) . I dont know whether you will have to change it to work with VB.

Mahin
As Ahmed suggested, instead of making useCSS public variable, expose it as porperty.
Mahin
+1 you had the right answer as well :)
Ahmad Mageed
+3  A: 

You need to expose it as a property to use it on the markup.

In the code-behind:

Private _useCss As Boolean
Public Property UseCss() As Boolean
    Get
        Return _useCss
    End Get
    Set(ByVal value As Boolean)
        _useCss = value
    End Set
End Property

Then in the markup:

    <%  If UseCss = True Then %>
    Your stylesheet link tag here
    <% Else %>
    else could be optional if you won't load anything
    <%  End If %>

Alternately you could have:

    <%  If UseCss = True Then
            Response.Write("text")
        Else
            Response.Write("something else")
        End If
    %>

Another option is to give your head tag an id and programmatically add the CSS file to it. To do this you don't need a property and could use the variable directly.

In your markup:

<head runat="server" id="head">
   <%-- whatever you typically place here --%>
</head>

In your code-behind, for example on page load:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If useCss Then
        Dim stylesheet As New HtmlGenericControl("link")
        stylesheet.Attributes.Add("rel", "stylesheet")
        stylesheet.Attributes.Add("type", "text/css")
        stylesheet.Attributes.Add("href", "../css/myCssFile.css")
        FindControl("head").Controls.Add(stylesheet)
    End If
End Sub
Ahmad Mageed
@Ahmad : + 1 for suggesting property :)
Mahin