绑定来自一个源的多个下拉列表

时间:2017-04-19 17:36:48

标签: mysql sql asp.net vb.net

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

以下代码将仅填充代码中的第一个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()

2 个答案:

答案 0 :(得分:1)

您可以填写 PhoneNumber phoneNum = new Mobile(123); if (phoneNum.getClass().isAssignableFrom(Mobile.class)) { Mobile mobile = (Mobile) phoneNum; } else { HousePhone housePhone = (HousePhone) phoneNum; } 并将其重新用作来源。

DataTable

答案 1 :(得分:0)

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

查询需要返回文本和值: 即: 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")