473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 9974
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: theo | last post by:
Hello..I have a similar issue as yesterday but the situation has slightly changed so perhaps someone can shed some more light on the issue... Program flow - load file,extract xml text tags from...
1
by: Chris | last post by:
Hello All, Need some help here. I'm building an asp.net app that uses a datagrid. I have created a template column and added a dropdownlist to it. What I'd like to happen is have the database...
0
by: Steve Caliendo | last post by:
Hi, I'm having trouble with multiple dynamically generated dropdownlists. On my web page I have several text boxes / labels / checkboxes and dropdownlists I have no problem setting the other...
3
by: Jim Bancroft | last post by:
Hi everyone, I'd like to put some DropDownLists in my DataGrid, populating each from a database query. I'm having a little trouble naming them based on the current record in the DataSet. For...
2
by: Jay | last post by:
I'm having a weird problem in ASP.NET 1.1. I have two DropDownLists in a form. Both lists are build identically but separately. Somehow the DropDownLists are getting crossed in memory, because when...
9
by: Timm | last post by:
I have an ASP.NET 2.0 page with two DropDownLists. I am using declarative data binding wherever possible and trying to minimize the use of code. The list of values in DropDownList DDL2 should be...
8
by: MattB | last post by:
I have a asp.net 1.1/vb application that has a page with a bunch of dynamically added User Controls. When I add the controls, I set the UserControl.EnableViewState to true. For all my controls...
0
by: Carlos | last post by:
Hi all, I just embedded a dropdownlist templatefield inside a gridview. When I look at the control in design mode, I see 5 dropdownlists, when I bind the data to the gridview, I only bind to 3...
3
by: RSH | last post by:
Hi, I am having this very bizarre occurance with 4 dropdown lists on my ASP .Net page. Each control has its own unique id. When the user selects a value from each of the dropdownlists it is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.