【问题标题】:Add item to databound DropDownList将项目添加到数据绑定的 DropDownList
【发布时间】:2009-11-29 20:48:10
【问题描述】:
我有下拉列表控制项目列表来自数据库的位置
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="semester"
DataValueField="semester">
</asp:DropDownList>
但我想在开头添加 1 个列表项更多“全部”.. 我怎样才能添加这个。
谢谢!
【问题讨论】:
标签:
c#
asp.net
drop-down-menu
【解决方案1】:
要将新列表项添加到 DropDownList,请在“属性”窗口中,单击“项”属性中的省略号。
添加一个带有文本“ALL”和值 -1 的新列表项。
或者您可以通过将此标记添加到 DropDownList 来添加列表项:
<asp:DropDownList ID="categories" runat="server" ...>
<asp:ListItem Value="-1">
ALL
</asp:ListItem>
</asp:DropDownList>
设置DropDownList的AppendDataBoundItems=True
【解决方案2】:
使用 Items.Insert 方法,您可以在特定索引处添加项目:
DropDownList1.Items.Insert(0, new ListItem("ALL", "ALL"));
【解决方案3】:
一定要禁用DropDownList1 的viewstate,这样它就不会保留数据库中的每个resultset!