Using a loop to populate a series of Drop down lists

ivgoneriding

Member
Joined
Feb 4, 2011
Messages
10
Programming Experience
Beginner
I have an aspx page with 5 DDLs on it, those DDLs are called for example DDL1 through to DDL5 and will all be bound to the same select statement (they are for 5 different contact fields)

I would like to be able to populate the boxes, either all together on page load or one after another so that when one is changed the next one populates - it doesn't matter to me which way I do it, but I don't want to have to repeat the blocks of code, just changing the number at the end of the DDL name each time.

Is there a piece of looping code that I can use, something along the lines of a for next loop or while loop that increments an integer and adds that to the rest of the name. I tried the following but got caught out trying to convert a string to the name of the DDL:

VB.NET:
For i = 1 To 5
                Do While i <= 5
                    selectcontact = countcontact & i
                    cxbasecode.PopulateDDL(ssql, selectcontact)
 
                   i=i+i                  
                Loop
            Next
Thanks
 
Hi,

Bit hard to glean anything from your code example without know what your PopulateDDL method does, but if it's taking a string for a control name you'd want to pull the ID of the control then add the increment to pass the new name. Something along the lines of the following should work:

VB.NET:
selectcontact = countcontact.ID & i.ToString()

If you wanted to head along your 'one after another' scenario, you'd probably want to google cascading dropdown's and take a look at those.

** also noticed your
VB.NET:
i=i+i
I guess that's a typo, since that'll give you 1,2,4,8... etc.
 
Hi,

Bit hard to glean anything from your code example without know what your PopulateDDL method does, but if it's taking a string for a control name you'd want to pull the ID of the control then add the increment to pass the new name. Something along the lines of the following should work:

VB.NET:
selectcontact = countcontact.ID & i.ToString()

Thanks, that was what I was looking for (i think!) I'll give it a try

If you wanted to head along your 'one after another' scenario, you'd probably want to google cascading dropdown's and take a look at those.

I'm sorted on the cascading drop downs, have got that going on already

** also noticed your
VB.NET:
i=i+i
I guess that's a typo, since that'll give you 1,2,4,8... etc.

Ah, yes, that is a typo, I meant i=i+1 :eek:
 
Looks like you're grabbing the control instead of the control's ID

**Edit**

Ah, ok just took a look at that code and it's expecting a reference to the control, rather than the controls name to be passed - If you want to do this as an iterative loop, you'll probably have to do a findcontrol on the container with the constructed name to get the control reference to pass to the method.
 
Last edited:
Looks like you're grabbing the control instead of the control's ID

**Edit**

Ah, ok just took a look at that code and it's expecting a reference to the control, rather than the controls name to be passed - If you want to do this as an iterative loop, you'll probably have to do a findcontrol on the container with the constructed name to get the control reference to pass to the method.

Errr? Sorry, could you explain that for me? I'm a bit of a coding newb.

Thanks
 
Basically, if you've got your dropdowns within a DIV element, give that DIV an ID and then use findcontrol to find your constructed dropdown name. Very roughly, something like this :

VB.NET:
divid.findcontrol("dropdownname" & ddlnum.ToString())

Will give you the reference to your control.

Google findcontrol which should give you some examples of it's use.
 
Ah, OK, thank-you. I have the DDLs within a table, so I guess I could do the same with that, give the table an ID then use findcontrol on that?
 
Yep, that should work - sometimes Findcontrol can be a bit tempermental with containers, but give it a whirl ;)
 
Thank-you, that was actually easier than I was expecting it to be, just for completeness and in case anyone else is trying to do the same thing, this is how the code looks now
VB.NET:
For i = 1 To 5
                Do While i <= 5
                    Dim mycontact As DropDownList = CType(FindControl("ddlcontact" & i.ToString()), DropDownList)
                    cxBaseCode.PopulateDDL(ssql, mycontact)
                    i = i + 1
                Loop
            Next
 
Back
Top