【问题标题】:Binding multible dropdown lists from one source从一个来源绑定多个下拉列表
【发布时间】:2017-04-19 17:36:47
【问题描述】:

我有多个下拉列表要填充相同的项目。

以下代码将仅填充代码中的第一个 DropDownList (DropDownList1),不会填充其他任何一个。为了测试,我将“DropDownList3”移到代码中的第一个位置,看看会发生什么,然后只有“DropDownList3”填充了正确的数据。我需要做什么才能让所有数据都填充相同的数据?

con.Open()
cmd1 = New SqlCommand(SqlQuery & SqlQuery2, con)
dr1 = cmd1.ExecuteReader

DropDownList1.DataSource = dr1
DropDownList1.DataTextField = "FullInfo"
DropDownList1.DataValueField = "FullInfo"
DropDownList1.DataBind()

DropDownList3.DataSource = dr1
DropDownList3.DataTextField = "FullInfo"
DropDownList3.DataValueField = "FullInfo"
DropDownList3.DataBind()

DropDownList4.DataSource = dr1
DropDownList4.DataTextField = "FullInfo"
DropDownList4.DataValueField = "FullInfo"
DropDownList4.DataBind()

DropDownList5.DataSource = dr1
DropDownList5.DataTextField = "FullInfo"
DropDownList5.DataValueField = "FullInfo"
DropDownList5.DataBind()

DropDownList6.DataSource = dr1
DropDownList6.DataTextField = "FullInfo"
DropDownList6.DataValueField = "FullInfo"
DropDownList6.DataBind()

DropDownList7.DataSource = dr1
DropDownList7.DataTextField = "FullInfo"
DropDownList7.DataValueField = "FullInfo"
DropDownList7.DataBind()
con.Close()

【问题讨论】:

    标签: mysql sql asp.net vb.net


    【解决方案1】:

    您可以填写 DataTable 并将其用作来源。

    Dim dt As DataTable = New DataTable
    
    Dim connection As SqlConnection = New SqlConnection(connStr)
    Dim adapter As SqlDataAdapter = New SqlDataAdapter(SqlQuery & SqlQuery2, connection)
    
    adapter.Fill(dt)
    
    DropDownList1.DataSource = dt
    DropDownList2.DataSource = dt
    

    【讨论】:

    • 谢谢,您的回答让我找到了正确的地方。谢谢!!
    【解决方案2】:
    private sub BindDrops(byRef drop as dropdownlist, byval text as string, byval value as string)
    
        con.Open()
        cmd1 = New SqlCommand(SqlQuery & SqlQuery2, con)
        dr1 = cmd1.ExecuteReader
        con.close()
    
        drop.DataSource = dr1
        drop.DataTextField = text
        drop.DataValueField = value
        drop.databind()
        end sub
    

    查询需要返回一个文本和一个值: IE: query = "SELECT ID_FIELD, NAME_FIELD FROM..."

    BindDrops(DropDownList1, "NAME_FIELD","ID_FIELD")
    BindDrops(DropDownList2, "NAME_FIELD","ID_FIELD")
    ...
    BindDrops(DropDownListN, "NAME_FIELD","ID_FIELD")
    

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 2021-08-21
      相关资源
      最近更新 更多