mysql - 从一个源绑定(bind)多个下拉列表

标签 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()

最佳答案

您可以填充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/

相关文章:

c# - 找不到 Entity Framework 6 的兼容提供者

c# - 静态与动态控制 + 基于情境

php - 如何从 SQL 中获取不同的行

php - 如何使用位域存储星期几?

来自多个表的 MySQL 最喜欢的项目

java - Java 中的 SQL 优化选项

java - 安全:090759 in weblogic managed logs

php - CakePHP 3.0 中 add 方法中将附加数据保存到 Join 表

php - 使用 PHP 将 mySQL 转换为 XLS?

asp.net - .NET 4 上的 "A potentially dangerous Request.Path found...",requestValidationMode= "2.0"和 validateRequest= "false"设置