如何在 masterpage.master 中访问 masterpage.master.vb 中定义的变量

发布于 2024-08-19 12:08:04 字数 466 浏览 6 评论 0原文

我在 masterpage.master.vb 中有一个充满 Browserhawk 信息的 cookie 集合,例如;

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

如果 Stylesheets 为 True,我将 useCSS 设置为 1,如果为 false,我将 useCSS 设置为 0 我需要在 masterpage.master 部分访问这些内容,例如;

if useCSS = true 
Then load stylesheet 
else 
Dont load stylesheet

我在寻找正确的语法来使其工作时遇到问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天 2024-08-26 12:08:04

您需要将其公开为属性才能在标记上使用它。

在代码隐藏中:

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 标记提供一个 id,并以编程方式向其添加 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

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
鸢与 2024-08-26 12:08:04

将 useCSS 变量设置为公共变量并将此代码写入您的主文件中。

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

注意:我是 C# 人:)。我不知道你是否需要更改它才能与 VB 一起使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文