Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing elements in one user control from another user control 1

Status
Not open for further replies.

VBRookie

Programmer
May 29, 2001
331
US
Ok, I have a user control that I named topNavBar. It contains a webcontrol for a dropdownlist.

I have another user control that I named siteFooter. It also contains a webcontrol for a dropdownlist.

I want to be able to reference and change the values in the dropdown in topNavBar from code written in siteFooter. Can this be done and if so how?

- VBRookie
 
There are a few different ways to handle this type of situation.

If you plan to approach it directly (as you've asked, and as I'll discourage), then first and foremost, you'll have to handle the issue of when events fire, and to ensure that the dependent events fire and set values subsequent to the routines that they depend on.

This is a challenge. I would approach this by putting the "guts" of the uc that the other is going to depend on in that guy's "page_init" event. This will fire first.

Then, in the dependent uc, put the dependent routines in the page_load or some other subsequent event. These will fire after the other's page_init, and make sure that the values have been set.

Then, you have to ask, "Where will I put these values, and how will I access them?"

Well, you could store them in session. That would probably be the least painful method, but then you have scalability questions to consider.

You may choose to expose these values through properties, and then give declarations of the "parent" (for lack of a better term) in the dependent uc... I'm not entirely sure this method will work, but you might give it a go.

So like I said, I'm going to discourage all of this non-sense and instead, suggest that you handle the passing of values between uc's via your main page class. You will find it much easier to control and manage your variables if you take this approach.

So, I would nix the page_load events in both uc's, and opt for my own personal "loadIt()" subroutine which can be called explicitly from the main page. You absolutely guarantee firing order if you go for this method.

You put all logic into the loadIt() subs, load the uc's programatically from the main page class, and pull variables out once you call those subs via properties that you expose.

So, for instance. Let's say uc2 depends on a variable from uc1 called 'myVar'. Then your code might look like this:
Code:
'dim needed vars
dim uc1 as uc1
dim uc2 as uc2
dim myVar as integer

'new up uc1
uc1 = loadControl("userControls/uc1.ascx")
'fire logic
uc1.loadIt()
'pull variable
myVar = uc1.myVar
'add uc1 to a placeholder on the page
myPlaceholder1.Controls.Add(uc1)

'new up uc2
uc2 = loadControl("userControls/uc2.ascx")
'assign myVar that you retrieved from uc1
uc2.myVar = myVar
'fire logic, which assumably will use value of myVar
uc2.loadIt()
'add uc2 to another placeholder on the page
myPlaceholder2.Controls.Add(uc2)

Hopefully, it's pretty clear to you why I suggest this route. I prefer the total control of this method, as opposed to the hit-or-miss proposition that I outlined first.

hth! :)
paul
penny1.gif
penny1.gif
 
Thanks Link9,

I got it to work. I had to do a little casting though. In the usercontrol called TopNavBar I made the property that I wanted to access public (dropJump1).

In usercontrol called SiteFooter I put in the following code to access that property and add to it:

((TopNavBar) Page.Controls[1].Controls[1]).dropJump1.Items.Add(new ListItem(str, str.Replace(" ","_")));

It works like a charm.
Thanks for your help.
 
Hi,

I'm trying to recreate you example. I've created the subs Loadit within the UC1 and UC2. But my main page is reporting that 'LoadIt' is not a member of 'System.Web.UI.Control'.

Any ideas
 
Well with my site I'm using a page template that all my pages inherit from. I also have a base control that all my controls inherit from. I don't know if the approach that I described in my previous posts will work for a non-templated site.

If you don't know what I mean take a look at Paul Wilson's site
If you are using a page template similar to this then please post your code so that I can take a look.

Thanks.
- VB Rookie
 
Well I'm using a page template for my site. I have a basepage that all of my pages inherit from and a basecontrol that all my controls inherit from. I don't know if the approach that I described in my previous posts will work with a non-templated site.

If you want to know more about the template that I used visit this site:
If you are using a page template and are still having trouble please post your code so that I can take a look.

Thanks,
- VB Rookie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top