[英]How to bind drop down list with string array in ASP.NET?
我能够通过这样做将下拉列表与字符串数组绑定(不确定这是否是正确的实现方式):
string[] items = { "111", "222", "333" };
ddlSearch.DataSource = items;
ddlSearch.DataBind();
但是,我真正想要的是:当我点击下拉列表时,列表中显示的第一个项目应为111
然后是222
和333
。
当我单击下拉列表按钮时,如何添加要在下拉列表中显示的文本字符串?
Java有一种简单的方法来添加要在列表中显示的项目,但是我们如何在C#中实现呢? (顺便说一句,我对C#很新。)
使用List<string>
会更容易
标记可以
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
后端代码看起来像
var items = new List<string> {
"111",
"222",
"333"
};
items.Sort();
DropDownList1.DataSource = items;
DropDownList1.DataBind();
来源: https : //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.contexttypename(v=vs.110).aspx
public class MovieLibrary
{
string[] _availableGenres = { "Comedy", "Drama", "Romance" };
public MovieLibrary()
{
}
public string[] AvailableGenres
{
get
{
return _availableGenres;
}
}
}
<asp:LinqDataSource
ContextTypeName="MovieLibrary"
TableName="AvailableGenres"
ID="LinqDataSource1"
runat="server">
</asp:LinqDataSource>
<asp:DropDownList
DataSourceID="LinqDataSource1"
runat="server"
ID="DropDownList1">
</asp:DropDownList>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.