【发布时间】:2015-06-11 17:56:48
【问题描述】:
我有一个 DropDownList 绑定到 SqlDataSource,如下所示:
<asp:DropDownList ID="ddlist" AppendDataBoundItems="True" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="value" AutoPostBack="True" runat="server">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" SelectCommand="SELECT [name], [value] FROM [myTable]" runat="server"></asp:SqlDataSource>
我想将第三个数据库列绑定到另一个属性(不是值),例如数据库表列 DefaultBit 到 ListItem 属性 data-default 以便 DropDownList 呈现为
<select>
<option></option>
<option value="1" data-default="1">Line 1</option>
<option value="2" data-default="0">Line 2</option>
</select>
我假设我需要在代码隐藏文件中以编程方式执行此操作,但我不清楚我是否仍将 SqlDataSource 绑定到 DropDownList 或者我是否应该打开一个连接并使用 SqlReader 遍历结果并更新DropDownList “手动”或其他更优雅的解决方案。
【问题讨论】: