加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 04:36:34 所属栏目:asp.Net 来源:网络整理
导读:我有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 然后我有一个aspx页面,我需要在其中
我有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

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

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

任何人都可以给我一个想法如何去做?我试过搜索,但大多数文章都在网页控件的上下文中讨论……

谢谢.

解决方法

干得好:

How to: Reference ASP.NET Master Page Content

从文章来看,它看起来像

If Master.SQLerror = Nothing Then
    InternalSQLErrLabel.Text = ("No Errors Reported")
End If

应该适合你.

只需确保按照描述添加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>

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

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>

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读