我有多个下拉列表,我想用相同的项目填充它们。
以下代码将仅填充代码中的第一个 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()
最佳答案
您可以填充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
关于mysql - 从一个源绑定(bind)多个下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43502541/