Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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#代码隐藏填充下拉列表_C#_Asp.net_Code Behind - Fatal编程技术网

用c#代码隐藏填充下拉列表

用c#代码隐藏填充下拉列表,c#,asp.net,code-behind,C#,Asp.net,Code Behind,我正在尝试使用代码隐藏(C#)填充一个下拉列表。我不知道怎样才能得到这个。下面是我的代码,我目前正试图使用,但我得到的错误。我试图填充一个下拉列表,列出每个月(1-12)的门店 受保护的无效页面加载(对象发送方,事件参数e) { 对于(int i=0;i

我正在尝试使用代码隐藏(C#)填充一个下拉列表。我不知道怎样才能得到这个。下面是我的代码,我目前正试图使用,但我得到的错误。我试图填充一个下拉列表,列出每个月(1-12)的门店

受保护的无效页面加载(对象发送方,事件参数e)
{
对于(int i=0;i<12;i++)
{
DropDownListMonth.SelectedValue=i;
DropDownListMonth.DataTextField=i.ToString();
DropDownListMonth.DataValueField=i.ToString();
}
}

听起来您只需要在dropdownlist中添加项目。如何使用循环式

List<int> months = new List<int>(){1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
foreach (string month in months)
{
     DropDownListMonth.Items.Add(month);
}
List months=newlist(){1,2,3,4,5,6,7,8,9,10,11,12};
foreach(以月为单位的字符串月)
{
DropDownListMonth.Items.Add(月);
}

因为for循环在
0
11
之间工作,而不是
1
12
之间工作。而且它没有添加任何项目。它只是设置,而作为
11
,没有做更多的事情

听起来您只需要在dropdownlist中添加项目。如何使用循环式

List<int> months = new List<int>(){1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
foreach (string month in months)
{
     DropDownListMonth.Items.Add(month);
}
List months=newlist(){1,2,3,4,5,6,7,8,9,10,11,12};
foreach(以月为单位的字符串月)
{
DropDownListMonth.Items.Add(月);
}

因为for循环在
0
11
之间工作,而不是
1
12
之间工作。而且它没有添加任何项目。它只是设置,而作为
11
,没有做更多的事情

您希望有一个列表,将值添加到该列表中,并将该列表绑定到下拉列表


另外,看看这篇文章来澄清一些困惑:

您想要一个列表,将值添加到该列表中,并将该列表绑定到下拉列表


另外,看看这篇文章来澄清一些困惑:

这就是你需要做的

for (var i = 1; i < 13; i++)
{
    var item = new ListItem
        {
            Text = i.ToString(),
            Value = i.ToString()
        };
    DropDownListMonth.Items.Add(item);
}
for(变量i=1;i<13;i++)
{
var item=新列表项
{
Text=i.ToString(),
Value=i.ToString()
};
DropDownListMonth.Items.Add(项目);
}

这就是你需要做的

for (var i = 1; i < 13; i++)
{
    var item = new ListItem
        {
            Text = i.ToString(),
            Value = i.ToString()
        };
    DropDownListMonth.Items.Add(item);
}
for(变量i=1;i<13;i++)
{
var item=新列表项
{
Text=i.ToString(),
Value=i.ToString()
};
DropDownListMonth.Items.Add(项目);
}

这段代码有哪些错误?这段代码有哪些错误?