Access property and control of master page from a customized parent page class

R

RedHair

I have a ASP.NET 2.0 web page which inherits a customized base page class
and have a master page,
their relation is as below

Customized base page class --> web page --> master page

How to access the property and control of master page from the customized
base page class?

Btw, I can access the server control of master page from base page class via
Cast + FinndControl,
however this method doesn't work on access a user control. how to solve?

Thanks in advance!
 
C

CaffieneRush

I don't know if I understood your question correctly but here goes.

'Access base class' master page.
Dim myBMP As MasterPage = Me.MyBase.Master

'Find a user control called "myControl" in base class' master
Dim myControl As UserControl = myBMP.FindControl("myControl")
'Or perhaps
Dim myControl As UserControl = CType(MyBMP.FindControl("myControl"),
UserControl)
 
C

CaffieneRush

Accessing the subclass from the base class violates the general OO
principle that the base class should not know the subclass. It makes
the base class dependent on its subclasses.

Having said that, you can pass a reference of the subclass' masterpage
to the base class. I have not tested the following code snippet.
'In base class. Declare a variable to hold a reference to subclass'
masterpage
Protected ChildMaster As MasterPage

'In subclass. Pass the reference to masterpage to base class
Me.MyBase.ChildMaster = Me.Master

Back in the base class you can access subclass' masterpage through
ChildMaster.

However, I would urge you to rethink your design.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top