ASP.NET - VB  - Need to make exact copies of a dropdown list

Posted on 2007-07-30
Last Modified: 2008-01-09
Hello Experts,

I have a form with 14 dropdown lists on it.  I'm populating the first of the 14 though a database query.  This is working fine.  The other 13 dropdown lists need to be identical to the first one.  Instead of hitting the database for each one, I'd rather just make the others carbon copies of the first one.  But I'm not sure how to do that.  The dropdown lists are named named ddlODRTC1 - ddlODRTC14.

I'm thinking of something like this:

        'Create a loop to copy data from ddlORTC1 to the other DDLs
        For xCounter As Integer = 2 To 14
            Dim ddlODRTC As DropDownList = Me.FindControl("ddlODRTC" & xCounter)
            If Not ddlODRTC Is Nothing Then

                'copy values of ddlODRTC1 to ddlODRTC

            End If
        Next xCounter

I'm fairly certain this'll work, I'm just not sure how to actually copy the data from one dropdown list to another.  Can anyone help me with this?

    LVL 17

    Accepted Solution

    dim dt as datatable = ....

            'Create a loop to copy data from ddlORTC1 to the other DDLs
            For xCounter As Integer = 2 To 14
                Dim ddlODRTC As DropDownList = Me.FindControl("ddlODRTC" & xCounter)
                If Not ddlODRTC Is Nothing Then

                    ddlODRTC.datasource = dt

                End If
            Next xCounter
    LVL 96

    Expert Comment

    by:Bob Learned
    If you are binding the DropDownList to data source, then you can bind as many DropDownList controls to the same data source.

    LVL 2

    Author Comment

    Well, I'm using a datareader to bind my data ... and I haven't had much luck getting it to bind to more than one.
    LVL 12

    Expert Comment

    try this

        Private Function DataDrop() As DataTable

            Dim dt As New DataTable
            dt.Columns.Add("No", GetType(Integer))
            dt.Columns.Add("Name", GetType(String))

            dt.Rows.Add(New Object() {10, "aaa"})
            dt.Rows.Add(New Object() {9, "bbb"})
            dt.Rows.Add(New Object() {11, "ccc"})

            Return dt

        End Function

      Private Sub FillDrop()
            Dim dt As DataTable
            Dim i As Integer
            Dim drop As DropDownList
            dt = DataDrop()
            For i = 1 To 3
                drop = CType(Me.Page.FindControl("DropDownList" + i.ToString), DropDownList)
                drop.DataSource = DataDrop()
                drop.DataTextField = "Name"
                drop.DataValueField = "No"
        End Sub
    LVL 2

    Author Comment

    Hey Everyone,

    Thanks for the quick feedback.  In the end I opted to use a DataTable instead of a DataReader.  Lem2802 was the first to answer, and since he was the one that nudged me towards the DataTable I'll be awarding him the points.

    Thanks again to everyone!!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now