Problems getting values from nested controls

R

Roffers

Okay I have a parent page, we'll call it parent.aspx and it holds the
following two user controls

<uc1:Box1 id="Box1" runat="server"></uc1:Box1> <----THIS CONTAINS A
DROP DOWN LIST
<br>
<uc1:Box2 id="Box2" runat="server"></uc1:Box2> <----THIS CONTAINS A
DATALIST

In Box1, I have a dropdown list that gets selected and should set
parameters for a datalist in Box2. My question is, how can I get Box2
to read the selected item of the DropDown list in Box1.

Any help/code samples would certainly be gratefully received.

Regards
Mark
 
M

Mark Fitzpatrick

The user controls should be completely seperate in a logical fashion. If
it's required to share data then you need to make use of what is containing
them, in this case, the page. You can expose any of your ojects that are
within a user control as properties of the control. What you'll need to do
is expose the list boxes on both controls as properties.

for example:

public DropDownList MyList
{
get{
return this.MyDropDownList;
}
}


(you may have to check this syntactically as I'm running off the top of my
head here).

Now in the parent container you could access the dropdownlist as Box1.myList
so to get the selected value property you would use
Box1.myList.SelectedValue. If you need to set values on the list you'll also
have to assign the set operation for the property as well. like so:

set
{
this.MyDropDownList = value;
}

The biggest difficulty will be if you want to attach something to the event
when the item in one of the dropdownlists is selected. You'll need to do
something called event bubbling so you can pass the selecteditemchanged
event up to the parent page. Event bubbling is a common topic, but takes a
bit more than you can get in the ng to understand. You should find some good
examples in the dotnet quickstart samples or you can search google on c#
event bubbling.

Then, you can use the values collected from control a and do whatever you
need to do to the dropdownlist in control b, such as populate it, select a
particular value, etc.. You'll have to adjust the event order that you do
things in, such as using the prerender event to do certain tasks in the
control since it fires at a time that makes it easier to get/receive
information with the parent page.

You may want to verify though if you really, really need the two seperate
controls. I say this only because a lot of programmers make this mistake of
encapsulating items that aren't really functioanally isolated and spend way
more time trying to get the two components to talk.
 

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,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top