您需要將其公開爲屬性以在標記上使用它。
在後臺代碼:
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
然後在標記:
<% If UseCss = True Then %>
Your stylesheet link tag here
<% Else %>
else could be optional if you won't load anything
<% End If %>
或者你可以有:
<% If UseCss = True Then
Response.Write("text")
Else
Response.Write("something else")
End If
%>
另一種選擇是給你的head
標籤的編號並以編程方式將CSS文件添加到它。要做到這一點,你不需要一個屬性,並可以直接使用該變量。
在您的標記:
<head runat="server" id="head">
<%-- whatever you typically place here --%>
</head>
在您的代碼隱藏,例如在頁面加載:
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:+ 1爲建議屬性:) – Mahin 2010-02-01 13:08:37