如何从一个嵌套页面内访问一个顶层母版页子? [英] How to access a top level master page sub from within a nested page?
问题描述
母版页结构
顶尖高手
PAGE MASTERTopMaster.ErrorMsg(ERRMSG)在这里不引发错误
PAGETopMaster.ErrorMsg(ERRMSG)这里抛出错误
我无法获得从基本页面访问顶层类。
顶尖高手ASPX
< ASP:文字ID =litMsg=服务器/>
PAGE.VB
部分公共类的BasePage
继承System.Web.UI.Page 公共小组的Page_Load(BYVAL发件人为对象,BYVAL E上EventArgs的)把手Me.Load
TopMaster.ErrorMsg(ERRMSG)
错误BC30451:TopMaster不宣布它可能无法访问由于其保护级别。
结束小组末级
MASTER.VB
部分公共类PageMaster
继承System.Web.UI.MasterPage末级
TOP MASTER.VB
部分公共类TopMaster
继承System.Web.UI.MasterPage 公共共享子ERRORMSG(ERRMSG作为字符串)
昏暗的MYPAGE = TryCast(HttpContext.Current.Handler,页) 如果我的空间状态并没有任何再
点心师傅= myPage.Master
昏暗myMaster = TryCast(master.Master,TopMaster)
虽然master.Master IsNot运算没有AndAlso运算myMaster是Nothing
主= master.Master
myMaster = TryCast(主,TopMaster)
虽然结束 myMaster.litMsg.Text = ERRMSG
万一 结束小组末级
更新:现在很明显,使课堂公开
,否则你不能访问它:
部分公共类TopMaster
<击>你应该向我们展示包括堆栈跟踪确切的错误信息。看来,键入顶尖高手
没有声明这是没有意义的,因为你是在类 TopMaster
。
所以我怀疑,继修复你的核心问题。错误:顶尖高手没有声明,但它可能是有用的反正击>
如果您要访问的文字,你应该提供一个属性的 TopMaster
是这样的:
公共财产ERRORMSG作为字符串
得到
返回Me.litMsg.Text
到底得的
设置(值作为字符串)
Me.litMsg.Text =价值
结束设定
高端物业
在这种方式,如果你想没有打破code,你甚至可以改变控制型。它的好得多,而不是暴露控件本身。
我猜你也必须分配从循环内移到外面。您还应该使用昏暗myMaster = TryCast(主,TopMaster)
一开始,而不是因为你与 TryCast做直接跳转到页面的主的主人( master.Master,TopMaster)
:
公共共享子ERRORMSG(ERRMSG作为字符串)
昏暗的MYPAGE = TryCast(HttpContext.Current.Handler,页) 如果我的空间状态并没有任何再
点心师傅= myPage.Master
昏暗myMaster = TryCast(主,TopMaster)
虽然master.Master IsNot运算没有AndAlso运算myMaster是Nothing
主= master.Master
myMaster = TryCast(主,TopMaster)
虽然结束
myMaster.ErrorMsg = ERRMSG
万一结束小组
否则,你只能分配 ERRORMSG
如果主是页面的硕士硕士硕士,或者它更深刻嵌套。
MASTER PAGE STRUCTURE
TOP MASTER
PAGE MASTER "TopMaster.ErrorMsg(ErrMsg) Here throws NO error"
PAGE "TopMaster.ErrorMsg(ErrMsg) Here throws error"
I am unable to gain access to the top level class from the base page.
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
Update: now it's clear, make the class Public
, otherwise you can't access it:
Partial Public Class TopMaster
You should show us the exact error message including stacktrace. It seems that the type Top Master
is not declared which makes no sense since you are in the class TopMaster
.
So i doubt that following fixes your core issue "Error: Top Master is not declared" but it might be useful anyway.
If you want to access that literal you should provide a property in your TopMaster
like this:
Public Property ErrorMsg As String
Get
Return Me.litMsg.Text
End Get
Set(value As String)
Me.litMsg.Text = value
End Set
End Property
On this way you can even change the control-type if you want without breaking the code. Its much better than to expose the control itself.
I guess you also have to move the assignment from inside the loop to outside. You should also use Dim myMaster = TryCast(master, TopMaster)
at the beginning instead of jumping directly to the page's master's master as you do with 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
Otherwise you are only assigning the ErrorMsg
if the master is the page's master's master's master or it's even more deeply nested.
这篇关于如何从一个嵌套页面内访问一个顶层母版页子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!