访问内容页面中的 MasterPage 变量
我正在尝试引用 MasterPage 中的变量,但收到错误。
我已经尝试过
<%@ MasterType" %>
,出现以下错误:
编译器错误消息:CS0030: 无法转换类型 'IPAMIntranet.IPAMIntranetMaster' 至 'ASP.ipamintranetmaster_master'
并
string tVar = ((MyNamespace.MyMasterPage)Master).variable
给出以下错误:
无法转换类型的对象 'ASP.ipamintranetmaster_master' 到 类型 'IPAMIntranet.IPAMIntranetMaster'。
有谁知道发生了什么事或者我错过了什么。
I am trying to reference a variable within my MasterPage but I am receiving errors.
I have tried
<%@ MasterType" %>
which gives the following error:
Compiler Error Message: CS0030:
Cannot convert type
'IPAMIntranet.IPAMIntranetMaster' to
'ASP.ipamintranetmaster_master'
and
string tVar = ((MyNamespace.MyMasterPage)Master).variable
which gives the following error:
Unable to cast object of type
'ASP.ipamintranetmaster_master' to
type
'IPAMIntranet.IPAMIntranetMaster'.
Does anyone know what is happening or am I missing something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在内容页面中指定母版页的虚拟路径。
You need to specify the virtual path to the Masterpage in the content page.
从表面上看,您的母版页似乎不是
IPAMIntranet.IPAIntranetMaster
类型,或者不是从IPAMIntranet.IPAIntranetMaster
继承,这是实现这一点的唯一方法。解决这个问题的方法是让它继承,或者确保类型是正确的。MasterType 指令可以采用 Master 可转换到的任何类,它主要用于智能感知。您可以向 Master 提供
VirtualPath
或TypeName
,它可以是 Master 的类、基类或接口,以更适合您的情况为准。From the looks of it, it seems that your master page either isn't the type
IPAMIntranet.IPAMIntranetMaster
, or doesn't inherit fromIPAMIntranet.IPAMIntranetMaster
, the only way to resolve this would be to make it inherit, or make sure the type is correct.The
MasterType
directive can take the whatever class the Master is castable to, it's mainly for intellisense. You can provide either theVirtualPath
to the Master orTypeName
, which can be the Master's class, a base class, or an interface, whichever is more appropriate for your situation.我通过使用接口来解决这个问题。
I worked this out by using an interface instead.