471,350 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

foreach with dropdownlists

Hi,
I have some dropdownlist web controls on as aspx page. I want to loop
through them on first (!IsPostBack) Page_Load in order to populate them. The
foreach control structure seems to be a good condidate for this though I'm
having problems getting the right syntax/form. I've been trying variations on
the following:

foreach (DropDownList myDDL in Controls)
{
//do something here
}

Can anyone give me an indication of what I maight be doing wrong?

many thanks,

Pete

Dec 9 '05 #1
2 9780
Italian Pete wrote:
I have some dropdownlist web controls on as aspx page. I want to loop
through them on first (!IsPostBack) Page_Load in order to populate them. The
foreach control structure seems to be a good condidate for this though I'm
having problems getting the right syntax/form. I've been trying variations on
the following:

foreach (DropDownList myDDL in Controls)
{
//do something here
}

Can anyone give me an indication of what I maight be doing wrong?


The above will fail if any controls are *not* DropDownLists. You need:

foreach (Control control in Controls)
{
Control ddl = control as DropDownList;
if (ddl != null)
{
// Do something here
}
}

Dec 9 '05 #2
Hi,

Beside jon's sugestion I would find really weird that you do not know
beforehand how many dropdown you have, or the name of them, if you do know
this and your datasource support IList (arraylist, datatable, array, etc )
you can use binding.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Italian Pete" <It*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
Hi,
I have some dropdownlist web controls on as aspx page. I want to loop
through them on first (!IsPostBack) Page_Load in order to populate them.
The
foreach control structure seems to be a good condidate for this though I'm
having problems getting the right syntax/form. I've been trying variations
on
the following:

foreach (DropDownList myDDL in Controls)
{
//do something here
}

Can anyone give me an indication of what I maight be doing wrong?

many thanks,

Pete

Dec 9 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chris | last post: by
reply views Thread by Steve Caliendo | last post: by
2 posts views Thread by Jay | last post: by
reply views Thread by Carlos | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.