ASP.NET 中,给 ListBox 绑定数据库数据,要比其它绑定多两个步骤。需要指定两个值:option 的 text 和 value 分别对应哪个字段。
list.DataSource = ds.Tables[0].DefaultView; //ds 为 DataSet 对象
list.DataTextField = "textFld"; //textFld 为字段名称
list.DataValueField = "valueFld"; //valueFld 为字段名称
list.DataBind();
同样实用于 DropDownList。
也可以利用声明式数据绑定:
<asp:DropDownList ID="_categoryId" runat="server"
DataSourceID="SqlDataSource2" DataTextField="categoryName" DataValueField="categoryId" SelectedValue="<%# Bind('categoryId') %>">
详情请参见:
http://www.itpow.com/c/2008/08/FZEAUHUITSG6MC5D.asp。
相关阅读