Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programatically filling dropdown lists

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I have inherited a website which fills all the dropdown lists on the page during the page load event. Code below:

Code:
 Private Sub FillDropDowns()


        Dim Ctrl As Object
        Dim j As Integer

            For Each Ctrl In Page.Controls

                For i As Integer = 0 To Ctrl.Controls.Count() - 1 'Step 1



                    If Ctrl.Controls.Item(i).ToString().Equals("System.Web.UI.WebControls.DropDownList") Then


                        'ADD A DEFAULT TO THE DROPDOWNS
                        Dim defItem As New ListItem
                        defItem.Text = " -- select -- "
                        defItem.Value = ""
                        Ctrl.Controls.Item(i).Items.Add(defItem)

                        'ADD THE VALUES 1 to 31 TO EACH DROPDOWN
                        For j = 1 To 31
                            Dim liItem As New ListItem
                            liItem.Text = j.ToString
                            liItem.Value = j.ToString
                            Ctrl.Controls.Item(i).Items.Add(liItem)
                        Next
                    End If

                Next
            Next

        End Sub

I have another dropdown list which is populated with other data but the above code appends to this dropdown list. Is there a way I can modify the code above to exclude a specific combo box/boxes?
 
use a nested if statement after the initial if to check the name of the control.

if you have multiple controls to check you can use an array i.e. myArray.Contains(someValue);
or
you can use the Array class i.e. Array.Exists(myArray, somevalue);
 
why not just
For j = 1 To 31
ddlFirstddl.Items.Insert(0, new ListItem(j.ToString(), j.ToString()))
Next

For j = 1 To 15
ddlSecondddl.Items.Insert(0, new ListItem(j.ToString(), j.ToString()))
Next

and so on
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top