English 中文(简体)
如何在masterpage.master中访问masterpage.master.vb中定义的变量
原标题:how do you access variables defined in masterpage.master.vb in masterpage.master

我有一些饼干集合,里面包含了masterpage.master.vb中的Browserhawk信息;

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

我遇到了找不到正确语法来让它工作的问题。

最佳回答

你需要将它作为属性公开,才能在标记上使用它。

在代码后台:

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
问题回答

请使用CSS变量作为公共变量,并在您的主文件中编写此代码。

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

注:我是C# Guy :) 我很想知道,你是否必须改变这一结构,以便与VB合作。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签