ASP.net Content Web 表单是否能够访问在其母版页的代码隐藏部分中声明的变量?
我有一个控制我网站样式的母版页。在代码隐藏中,有一些实例化的类实例以及变量。这些类验证用户访问,然后创建用户对象。
我有一些 Web 内容表单,它们根据用户对象执行指令。到目前为止,我似乎必须在每个 Web 内容表单上创建母版页上找到的类的新实例。这使我对每个 Web 内容表单的工作量增加了一倍。
无论如何,我可以继承母版页代码隐藏中实例化的类和对象吗?
I have a Master Page which controls the styling of my site. In the Code Behind, there are a few class instances instantiated as well as variables. These classes validate user access and then create user objects
I have a few Web Content Forms which carries out instructions based on the user objects. So far it seems that on each Web Content Form I have to create new instances of the classes found on the Master Page. This is doubling my work for every Web Content Form.
Is there anyway I can inhereit Classes and objects instantiated in the Master Page Code Behind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将对象(甚至控件)公开为
母版
页面上的公共属性(仅获取控件)。然后,在您想要访问这些对象的每个aspx
页面中,在顶部添加以下声明:正如 @Kristof 指出的,只需访问您的属性,例如
Master.PropertyName
另外,您可以确定将对象存储在用户
Session
中是否有意义(不要忘记,如果您使用数据库作为会话状态,它们必须是可序列化的)。我经常这样做,并通过所有页面继承自的基本Page
类中的属性来控制对它们的访问。实际上,我有一个基本的master
、page
和usercontrol
所以我可以访问相同的属性(对我来说它是CurrentUser< /code>) 无处不在。
Expose the objects (and even controls) as public properties (get only for controls) on the
Master
page. Then, in eachaspx
page you want access to these objects, add the following declaration at the top:As @Kristof points out, simply access your properties like
Master.PropertyName
Also, you can determine if it makes sense to store the objects in the users
Session
(don't forget that they must be serializable if you use DB for session state). I do this often and control access to them via properties in a basePage
class that all my pages inherit from. Actually, I have a basemaster
,page
, andusercontrol
so I have access to the same properties (for me it'sCurrentUser
) everywhere.我相信如果你公开这些财产就可以。
然后在您的子页面中,您可以进行如下调用:
其中
SiteMaster
是您的母版页的类。 (SiteMaster
是应用程序模板的默认设置)虽然我的思想会欺骗我,但我已经有一段时间没有这样做了......
I believe you can if you make the properties public.
Then in your child-page you can make the call something like this:
Where
SiteMaster
is the class for your master page. (SiteMaster
is the default for the app templates)Though my mind can deceive me, I haven't done it for a while...