C# ASPX:如何获取控件中父页面的继承属性?
我有一个 aspx 页面,它继承了一个具有受保护属性的母版页。像这样:
母版页 { 受保护的字符串 propX.. MyPage
: masterpage
---myControl:UserControl
在 myControl 代码隐藏中我想访问 propX
有什么想法吗?
谢谢!
I have an aspx page that inherits a master page which has a protected property. Like this:
masterpage
{
protected string propX..
}
MyPage : masterpage
---myControl:UserControl
In myControl code-behind I'd like to access propX
Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许尝试将
myControl
类的Page
属性转换为MyPage
类?如果您想从其他类(例如
myControl
)访问此属性,则属性propX
的访问修饰符应该设置为internal
或public
我假设您已将
myControl
对象放置在MyPage
页面上。Maybe try to cast the
Page
property ofmyControl
class toMyPage
class?And if you want to access this property from other class (like
myControl
), the access modifier of propertypropX
should be set tointernal
orpublic
I've assumed, that you've placed
myControl
object onMyPage
page.您可以更改字符串的访问修饰符。也许将其设置为内部。
You could possibly change the access modifiers for the string. Maybe set it to internal.
您确定要从母版页继承吗?添加 MasterPage 指令并不意味着它继承自它。通常aspx页面应该直接或间接继承自System.Web.UI.Page。
母版页不是“继承的”,这意味着无法从页面类(或控件类)访问受保护的成员。您最好的选择是将财产公开或内部化。
Are you sure you're inheriting from the master page? Adding the MasterPage directive doesn't mean that it inherits from it. Usually an aspx page should directly or indirectly inherit from System.Web.UI.Page.
The master pages aren't "inherited" which means that protected members cannot be accessed from the page class (or control class). Your best option is to make the property public or internal.