DropDownList 中来自一个 SqlDataSource 的不同值

本文关键字:SqlDataSource 一个 DropDownList | 更新日期: 2023-09-27 18:30:38

我在一页上有十个DropDownList。当我对所有数据源使用相同的数据源时,所有十个数据源中都有重复的值。有没有办法对所有十个 DropDownList 使用一个 SqlDataSource,并且只为每个 DropDownList 获取不同的值。

下面是一个示例下拉列表和 SqlDataSource:

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="County" DataValueField="County"AppendDataBoundItems="true"></asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings %>" 
    SelectCommand="SELECT Distinct * FROM [Personal]"></asp:SqlDataSource>

提前致谢

DropDownList 中来自一个 SqlDataSource 的不同值

我怀疑您的 sql 查询正在返回重复,因为您使用的是 select *。 如果表中有任何记录的字段与其他记录不同,例如自动增量主键,则也会返回这些记录。 "非重复"确保整个记录集是唯一的。

尝试将查询限制为仅所需的字段。

SELECT Distinct County FROM [Personal]

检查此链接: http://www.w3schools.com/sql/sql_distinct.asp

相关文章: