How do I Share/Access properties between User Controls??

J

Jon Hyland

This might be a dumb question, but what is the best way for one
instance of a user control to access properties of an instance of
another user control?

For example, let's say I have an instance of UserControlA (ucA1) and
an instance of UserControlB (ucB1) on a particular ASP.NET page.
UserControlA has a string property called StringA that gets generated
from a DB call in it's Page_Load event. I want UserControlB to be
able to access UserControlA's StringA property in its Page_Load event
(which gets fired afterward). How would you do this?

Would you need to create a page level variable and assign the value to
this? Is it even possible for a user control to access its
container's objects?

I want to avoid using Session variables here.. the text contained in
this string should not persist in memory beyond the page call.

Thanks in advance!
 
J

Jim Corey

You could use a page level variable as you stated.

Here's one way I did it:

Say the page holding these controls
is MainPage.ascx

Create a property in MainPage like this:

Public Property StringA() As String
Get
Return CType(viewstate("StringA"), String)
End Get
Set(ByVal Value As String)
viewstate("StringA") = Value
End Set
End Property


Then in each control you could have

Protected MyParent as MainPage

and refer to MyParent.StringA

(Actually I created a base class that each user control inherits, and
put MyParent there)

HTH,
Jim
 
J

John Hyland

Jon Hyland said:
This might be a dumb question, but what is the best way for one
instance of a user control to access properties of an instance of
another user control?

For example, let's say I have an instance of UserControlA (ucA1) and
an instance of UserControlB (ucB1) on a particular ASP.NET page.
UserControlA has a string property called StringA that gets generated
from a DB call in it's Page_Load event. I want UserControlB to be
able to access UserControlA's StringA property in its Page_Load event
(which gets fired afterward). How would you do this?

Would you need to create a page level variable and assign the value to
this? Is it even possible for a user control to access its
container's objects?

I want to avoid using Session variables here.. the text contained in
this string should not persist in memory beyond the page call.

Thanks in advance!

FYI -

What I ended up doing was creating a class with the globals I wanted to
share between user controls, and put a public instance of this class on my
main page.

Then in the user controls I do something like this:

//to set the global value in one user control
((ProjectNamespace.MainPage)this.Page).MyClass.sStringA = "1234";

//to retrieve from another user control
string sValue = ((ProjectNamespace.MainPage)this.Page).MyClass.sStringA;

Each user control has a "Page" property that points to it's container. You
have to cast this property to the specific page it refers to, then you can
access its public members.

Of course, this makes the user control useful only on this specific page
(you can't put it on a page other than MainPage), but this is ok for what I
need it for.. I'm not reusing these controls.

If I end up wanting to reuse these controls there is another method of
calling contriner functions using delegates.
http://www.serversidecode.net/absolutenm/templates/articleDetails.aspx?articleid=97&zoneid=8 shows some good examples of this.
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top