I have masterpage.master.vb where I have properties, such as;
我有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
Then I have an aspx page in which I need to use this property in, such as;
然后我有一个aspx页面,我需要在其中使用此属性,例如;
If **[masterpage property sqlerror]** = Nothing Then
InternalSQLErrLabel.Text = ("No Errors Reported")
End If
Can anyone give me an idea how to go about this? I've tried searching but most articles talk in the context of web controls...
任何人都可以给我一个想法如何去做这个?我试过搜索,但大多数文章都在网页控件的上下文中讨论......
Thanks.
4 个解决方案
#1
8
Here you go:
干得好:
How to: Reference ASP.NET Master Page Content
如何:引用ASP.NET主页面内容
From the article, it looks like
从文章来看,它看起来像
If Master.SQLerror = Nothing Then
InternalSQLErrLabel.Text = ("No Errors Reported")
End If
should work for you.
应该适合你。
Just be sure to add the MasterType directive as described or you might get a type conversion error. (Or you could use a variable of your master page type instead of Master, as daRoBBie suggests in his answer.)
只需确保按照描述添加MasterType指令,否则可能会出现类型转换错误。 (或者您可以使用主页类型的变量而不是Master,就像daRoBBie在他的回答中所建议的那样。)
I have created a test Web site just to test this out, and it works. Here is the full source of the site:
我创建了一个测试网站,只是为了测试它,它的工作原理。以下是该网站的完整来源:
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
#2
3
you can cast the masterpage to the correct type:
您可以将母版页转换为正确的类型:
MyMasterPageType m = (MyMasterPageType)Master;
Then you can access your properties:
然后,您可以访问您的属性:
m.SqlError
If you have multiple master pages, let all your masterpages inherit from an interface, and cast the masterpage to that interface.
如果您有多个母版页,请让所有母版页继承自接口,并将母版页强制转换为该接口。
#3
1
You can use <%@ MasterType %> also for this.
您也可以使用<%@ MasterType%>。
#4
0
If you have followed the steps in Andy West's answer and have one or many compile errors reading: Foo is not a member of 'System.Web.UI.MasterPage'
, make sure that they are the only compile error in your list. If there are any other compile errors that need to be fixed, they should be fixed before you continue troubleshooting your MasterPage
.
如果你已经按照Andy West的回答中的步骤操作并且有一个或多个编译错误,那么:Foo不是'System.Web.UI.MasterPage'的成员,请确保它们是列表中唯一的编译错误。如果还有其他需要修复的编译错误,则应在修复MasterPage之前修复它们。
These other compile errors may be preventing the compiler from parsing your MasterPage
and detecting the extra properties. Once they are resolved, do a full recompile.
这些其他编译错误可能会阻止编译器解析您的MasterPage并检测其他属性。解决后,执行完整的重新编译。
#1
8
Here you go:
干得好:
How to: Reference ASP.NET Master Page Content
如何:引用ASP.NET主页面内容
From the article, it looks like
从文章来看,它看起来像
If Master.SQLerror = Nothing Then
InternalSQLErrLabel.Text = ("No Errors Reported")
End If
should work for you.
应该适合你。
Just be sure to add the MasterType directive as described or you might get a type conversion error. (Or you could use a variable of your master page type instead of Master, as daRoBBie suggests in his answer.)
只需确保按照描述添加MasterType指令,否则可能会出现类型转换错误。 (或者您可以使用主页类型的变量而不是Master,就像daRoBBie在他的回答中所建议的那样。)
I have created a test Web site just to test this out, and it works. Here is the full source of the site:
我创建了一个测试网站,只是为了测试它,它的工作原理。以下是该网站的完整来源:
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
#2
3
you can cast the masterpage to the correct type:
您可以将母版页转换为正确的类型:
MyMasterPageType m = (MyMasterPageType)Master;
Then you can access your properties:
然后,您可以访问您的属性:
m.SqlError
If you have multiple master pages, let all your masterpages inherit from an interface, and cast the masterpage to that interface.
如果您有多个母版页,请让所有母版页继承自接口,并将母版页强制转换为该接口。
#3
1
You can use <%@ MasterType %> also for this.
您也可以使用<%@ MasterType%>。
#4
0
If you have followed the steps in Andy West's answer and have one or many compile errors reading: Foo is not a member of 'System.Web.UI.MasterPage'
, make sure that they are the only compile error in your list. If there are any other compile errors that need to be fixed, they should be fixed before you continue troubleshooting your MasterPage
.
如果你已经按照Andy West的回答中的步骤操作并且有一个或多个编译错误,那么:Foo不是'System.Web.UI.MasterPage'的成员,请确保它们是列表中唯一的编译错误。如果还有其他需要修复的编译错误,则应在修复MasterPage之前修复它们。
These other compile errors may be preventing the compiler from parsing your MasterPage
and detecting the extra properties. Once they are resolved, do a full recompile.
这些其他编译错误可能会阻止编译器解析您的MasterPage并检测其他属性。解决后,执行完整的重新编译。