Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET中将下拉列表与字符串数组绑定?_C#_Asp.net_Drop Down Menu - Fatal编程技术网

C# 如何在ASP.NET中将下拉列表与字符串数组绑定?

C# 如何在ASP.NET中将下拉列表与字符串数组绑定?,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,通过这样做,我可以将下拉列表与字符串数组绑定(不确定这是否是正确的实现方式): 然而,我真正想要的是:当我单击下拉列表时,列表中显示的第一项应该是111,然后是222和333 单击下拉列表按钮时,如何添加要显示在下拉列表中的文本字符串 Java有一种简单的方法来添加要显示在列表中的项目,但是我们如何在C#中做到这一点呢?(顺便说一句,我对C很陌生。)使用List 标记可以是 <asp:DropDownList ID="DropDownList1" runat="server"><

通过这样做,我可以将下拉列表与字符串数组绑定(不确定这是否是正确的实现方式):

然而,我真正想要的是:当我单击下拉列表时,列表中显示的第一项应该是
111
,然后是
222
333

单击下拉列表按钮时,如何添加要显示在下拉列表中的文本字符串


Java有一种简单的方法来添加要显示在列表中的项目,但是我们如何在C#中做到这一点呢?(顺便说一句,我对C很陌生。)

使用
List

标记可以是

<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>

后端代码如下所示

var items = new List<string> {
"111",
"222",
"333"
};
items.Sort(); 

DropDownList1.DataSource = items;
DropDownList1.DataBind();
var items=新列表{
"111",
"222",
"333"
};
items.Sort();
DropDownList1.DataSource=项目;
DropDownList1.DataBind();
来源:

公共级电影图书馆
{
字符串[]可用长度={“喜剧”、“戏剧”、“浪漫”};
公共电影图书馆
{
}
公共字符串[]可用项
{
得到
{
返回可用项;
}
}
}

用这种方法。项目在下拉列表中的显示顺序是什么?您是否希望对列表进行重新排序?或者您是否希望向列表中添加其他字符串?如果您想添加项目,使用
List
Dictionary
可能更容易。我看到了几个使用List的示例,是否可以使用字符串数组填充下拉列表?顺序无关紧要。例如,我只想要下拉列表中的“111”、“222”、“333”。所以问题是,如何添加到数组?问题是“如何用字符串填充下拉列表”-最好是字符串数组
var items = new List<string> {
"111",
"222",
"333"
};
items.Sort(); 

DropDownList1.DataSource = items;
DropDownList1.DataBind();
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>