开发者

Are ASP.net Content Web Forms able to access variables declared in the Code Behind section of it's Master Page?

开发者 https://www.devze.com 2023-01-28 13:30 出处:网络
I have a Master Page which controls the styling of my site. In the Code Behind, there are a few class instances instantiated as w开发者_StackOverflow社区ell as variables. These classes validate user a

I have a Master Page which controls the styling of my site. In the Code Behind, there are a few class instances instantiated as w开发者_StackOverflow社区ell 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?


Expose the objects (and even controls) as public properties (get only for controls) on the Master page. Then, in each aspx page you want access to these objects, add the following declaration at the top:

<%@ MasterType VirtualPath="~/MyMasterPage.master" %>

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 base Page class that all my pages inherit from. Actually, I have a base master, page, and usercontrol so I have access to the same properties (for me it's CurrentUser) everywhere.


I believe you can if you make the properties public. Then in your child-page you can make the call something like this:

SiteMaster master = (SiteMaster)this.Master;
master.MyProperty = 0;

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消