Access pages base class properties from User Control

M

Mikip

Hi,

I have some properties that are common to all pages so I have created a base
class which every page inherits from. I now also want to use these
properties within User Controls on the page. Is there any way to get to
these properties through the parent property of the user control - I can't
seem to figure out how to get there. If I can't do this, do I have to do the
same thing as the page and create a base user control class with the same
code in - Seems silly to have to execute the same code twice on a page?

Thanks in advance for any help

Mike
 
A

Anatoly

UserControl.Page return reference to Page object contains this control.
Since you use another class for your page you must cast this property to
your page class:
For example
if your base page class named MyBasePage

((MyBasePage)MyUserControl.Page).[my base page property/method]

HTH
 
M

Mikip

Anatoly,
Thanks for the reply...yes, if i maybe would have used a few more of my
limited neurons, I would have seen this! Incidently, do you think its better
to declare a private field in the UserControl class, do the cast once
assigning the base property to this field and use the private field within
the UserControl class. Will it save some performance not having to make the
cast everytime I want to use a base class property.?

Thx

Mike
Anatoly said:
UserControl.Page return reference to Page object contains this control.
Since you use another class for your page you must cast this property to
your page class:
For example
if your base page class named MyBasePage

((MyBasePage)MyUserControl.Page).[my base page property/method]

HTH

Mikip said:
Hi,

I have some properties that are common to all pages so I have created a base
class which every page inherits from. I now also want to use these
properties within User Controls on the page. Is there any way to get to
these properties through the parent property of the user control - I can't
seem to figure out how to get there. If I can't do this, do I have to do the
same thing as the page and create a base user control class with the same
code in - Seems silly to have to execute the same code twice on a page?

Thanks in advance for any help

Mike
 
S

Shiv Kumar

Mike

In reality, this smells of bad design since your UserControl now is aware of
your Base Page Class and "talks" to it. Of course, if your UserControl and
base page are specific to this application then its not too bad.

Typically UserControls (like other controls) should be agnostic of the
specific page.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top