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

前端之家收集整理的这篇文章主要介绍了从ASP.net VB中的子页面访问母版页属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有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
原文链接:https://www.f2er.com/aspnet/248818.html

猜你在找的asp.Net相关文章