MASTER PAGE STRUCTURE
TOP MASTER
PAGE MASTER "TopMaster.ErrorMsg(ErrMsg) Here throws NO error"
PAGE "TopMaster.ErrorMsg(ErrMsg) Here throws error"
我无法从基页访问顶级课程。
TOP MASTER ASPX
<asp:Literal ID="litMsg" runat="Server"/>
PAGE.VB
Partial Public Class BasePage
Inherits System.Web.UI.Page
Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
TopMaster.ErrorMsg(ErrMsg)
"Error BC30451: TopMaster is not declared it may be inaccessible due to its protection level."
End Sub
End Class
MASTER.VB
Partial Public Class PageMaster
Inherits System.Web.UI.MasterPage
End Class
TOP MASTER.VB
Partial Public Class TopMaster
Inherits System.Web.UI.MasterPage
Public Shared Sub ErrorMsg(ErrMsg As String)
Dim myPage = TryCast(HttpContext.Current.Handler, Page)
If myPage IsNot Nothing Then
Dim master = myPage.Master
Dim myMaster = TryCast(master.Master, TopMaster)
While master.Master IsNot Nothing AndAlso myMaster Is Nothing
master = master.Master
myMaster = TryCast(master, TopMaster)
End While
myMaster.litMsg.Text = ErrMsg
End If
End Sub
End Class
答案 0 :(得分:2)
更新:现在很明显,请创建课程Public
,否则您无法访问它:
Partial Public Class TopMaster
你应该向我们展示包括stacktrace在内的确切错误信息。似乎没有声明类型 Top Master
,因为你在班级TopMaster
中没有任何意义。
所以我怀疑以下修复了你的核心问题“错误:Top Master未被声明”但无论如何它可能都有用。
如果您想访问该文字,则应在TopMaster
中提供如下属性:
Public Property ErrorMsg As String
Get
Return Me.litMsg.Text
End Get
Set(value As String)
Me.litMsg.Text = value
End Set
End Property
通过这种方式,您甚至可以在不破坏代码的情况下更改控件类型。它比暴露控件本身要好得多。
我猜你还必须将作业从循环内移到外面。你也应该在开头使用Dim myMaster = TryCast(master, TopMaster)
,而不是像TryCast(master.Master, TopMaster)
那样直接跳到页面的主人的主人:
Public Shared Sub ErrorMsg(ErrMsg As String)
Dim myPage = TryCast(HttpContext.Current.Handler, Page)
If myPage IsNot Nothing Then
Dim master = myPage.Master
Dim myMaster = TryCast(master, TopMaster)
While master.Master IsNot Nothing AndAlso myMaster Is Nothing
master = master.Master
myMaster = TryCast(master, TopMaster)
End While
myMaster.ErrorMsg = ErrMsg
End If
End Sub
否则,如果主人是页面主人的主人的主人,或者甚至更深层次地嵌套,你只会分配ErrorMsg
。