"Jon Hyland" <jo*******@hotmail.com> wrote in message
news:f2**************************@posting.google.c om...
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.sSt ringA = "1234";
//to retrieve from another user control
string sValue = ((ProjectNamespace.MainPage)this.Page).MyClass.sSt ringA;
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/absolu...id=97&zoneid=8 shows some good examples of this.