Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 395
Yo Momma Avatar asked Dec 18 '25 04:12

Yo Momma


2 Answers

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.

like image 197
Brad Avatar answered Dec 21 '25 00:12

Brad


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

like image 31
Christian Wattengård Avatar answered Dec 20 '25 22:12

Christian Wattengård



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!