gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:28:54 25 4
gpt4 key购买 nike

我有 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

谁能给我一个想法如何去做?我试过搜索,但大多数文章都是在 Web 控件的上下文中讨论的......

谢谢。

最佳答案

干得好:

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

关于asp.net - 从 ASP.net VB 中的子页面访问母版页属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2182481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com