从ASP.net VB中的子页面访问母版页属性

Phi*_*hil 8 vb.net asp.net master-pages properties

我有masterpage.master.vb,我有属性,如;

 Private _SQLerror As String
    Public Property SQLerror() As String
        Get
            Return _SQLerror
        End Get
        Set(ByVal value As String)
            _SQLerror = String.Empty

        End Set
    End Property
Run Code Online (Sandbox Code Playgroud)

然后我有一个aspx页面,我需要在其中使用此属性,例如;

 If **[masterpage property sqlerror]** = Nothing Then
            InternalSQLErrLabel.Text = ("No Errors Reported")
        End If
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我如何解决这个问题?我试过搜索,但大多数文章都在网页控件的上下文中讨论......

谢谢.

And*_*est 8

干得好:

如何:引用ASP.NET母版页内容

从文章来看,它看起来像

If Master.SQLerror = Nothing Then
    InternalSQLErrLabel.Text = ("No Errors Reported")
End If
Run Code Online (Sandbox Code Playgroud)

应该适合你.

请务必按照描述添加MasterType指令,否则可能会出现类型转换错误.(或者您可以使用主页类型的变量而不是Master,因为daRoBBie在他的回答中建议.)

我创建了一个测试网站,只是为了测试它,它的工作原理.以下是该网站的完整来源:

Site1.Master:

<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Site1.master.vb" Inherits="WebApplication1.Site1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        This is the Master Page content.
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Site1.Master.vb:

Public Partial Class Site1
    Inherits System.Web.UI.MasterPage

    Private _SQLerror As String

    Public Property SQLerror() As String
        Get
            Return _SQLerror
        End Get
        Set(ByVal value As String)
            _SQLerror = String.Empty
        End Set
    End Property
End Class
Run Code Online (Sandbox Code Playgroud)

WebForm1.aspx:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master"
    CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>

<%@ MasterType VirtualPath="~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    This is the Content Page content.
    <asp:Label ID="InternalSQLErrLabel" runat="server" Text="Label"></asp:Label>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)

WebForm1.aspx.vb:

Public Partial Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Master.SQLerror = Nothing Then
            InternalSQLErrLabel.Text = ("No Errors Reported")
        End If
    End Sub

End Class
Run Code Online (Sandbox Code Playgroud)

  • 如果你做了Andy所说的一切,它仍然不起作用:将属性放在母版页中,重建项目....然后关闭Visual Studio.打开它,然后尝试内容页面部分.我花了大约2个小时在此之前记住有时会发生这种情况.AHHHH! (2认同)