[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 846
  • Last Modified:

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

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?

Thanks!
GRCHELPDESK
0
GRCHELPDESK
Asked:
GRCHELPDESK
1 Solution
 
Elvio Lujan.Net Senior DeveloperCommented:
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
                ddlODRTC.databind()

            End If
        Next xCounter
0
 
Bob LearnedCommented:
If you are binding the DropDownList to data source, then you can bind as many DropDownList controls to the same data source.

Bob
0
 
GRCHELPDESKAuthor Commented:
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.
0
 
madhevan_pillaiCommented:
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"
            drop.DataBind()
        Next
    End Sub
0
 
GRCHELPDESKAuthor Commented:
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!!
GRCHELPDESK
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now