469,916 Members | 1,953 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

Problems getting values from nested controls

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

Oct 10 '06 #1
1 1491
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.
--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"Roffers" <mr*****@hotmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
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

Oct 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Marco Martin | last post: by
4 posts views Thread by krzysiek | last post: by
7 posts views Thread by charliewest | last post: by
2 posts views Thread by Kiran Kumar Pinjala | last post: by
5 posts views Thread by WesIdell | last post: by
6 posts views Thread by Yehia A.Salam | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.