如何将下拉列表与ASP.NET中的字符串数组绑定? [英] How to bind drop down list with string array in ASP.NET?
问题描述
通过这样做,我能够将下拉列表与字符串数组绑定在一起(不确定这是否是正确的实现方式):
I am able to bind drop down list with a string array by doing so (not sure whether this is the correct way to implement):
string[] items = { "111", "222", "333" };
ddlSearch.DataSource = items;
ddlSearch.DataBind();
但是,我真正想要的是:当我单击下拉列表时,列表中显示的第一项应为 111
,然后是 222
和333
.
However, what I actually wanted is: When I click the drop down list, the first item showing in the list shall be 111
followed by 222
and 333
.
当我单击下拉列表按钮时,如何添加文本字符串以显示在下拉列表中?
How am I able to add the strings of text to show in the drop down list when I click the drop down list button?
Java有一种简单的方法来添加要显示在列表中的项目,但是我们如何在C#中做到这一点呢?(顺便说一下,我对C#还是很陌生.)
Java has an easy way to add items to be displayed in the list, but how do we do it in C#? (I am very new to C# by the way.)
推荐答案
使用 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();
这篇关于如何将下拉列表与ASP.NET中的字符串数组绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!