我想在绑定到List<T>的下拉列表中添加一个"Select One“选项。
查询List<T>后,如何将初始Item (而不是数据源的一部分)添加为该List<T>中的第一个元素?我有:
// populate ti from data
List<MyTypeItem> ti = MyTypeItem.GetTypeItems();
//create initial entry
MyTypeItem initialItem = new MyTypeItem();
initialItem.TypeItem = "Select One";
initialItem.TypeItemID = 0;
ti.Add(initialItem) <!-- want this at the TOP!
// then
DropDownList1.DataSource = ti;发布于 2008-12-24 00:37:59
使用Insert方法:
ti.Insert(0, initialItem);发布于 2008-12-24 00:37:47
更新:更好的方法是将"AppendDataBoundItems“属性设置为true,然后声明"Choose item”。数据绑定操作将添加到静态声明的项中。
<asp:DropDownList ID="ddl" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="Please choose..."></asp:ListItem>
</asp:DropDownList>http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx
-Oisin
发布于 2020-01-08 18:11:26
https://stackoverflow.com/questions/390491
复制相似问题