User Control and Variable Question

D

David Hearn

I am creating a user control for use on several of my pages. I have some
variables that are set on my pages where the user control will be used. I
need my user control to automatically be able to pick up on these variables
and read the values out of them when it is placed on one of these pages. How
is this accomplished? I know I could call a function that is in my user
control and pass it the variable but that would take extra coding for each
page that the user control is placed on and that sort of defeats the purpose
of using them. This needs to be automatic.

Thanks in advance!
 
M

Marina Levit [MVP]

In that case your pages that use this control need to inherit from some sort
of base page, or implement some sort of interface. There would then be
methods common to the base page or interface, that the user control could
call. So it would cast its parent page to this base page or interface type,
then access the appropriate property to return the variable value.
That means that a page that doesn't inherit from your base class or
implement the interface would not be able to host the user control.

Another way is to have the a base page class, that has the code in it to set
all the right properties on the user control, or call some method to set
these variables. That way you only write it once. So you define the
variables in the base page class, and write a method to set them that the
base page calls in page_load or whenever is appropriate. Then, the
descendent pages have to set the variable values, but don't have to do it
for the user control because the base page method will do it.

Either way, this is going to require extra coding though.
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top