Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.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
Asp.net 使用循环将整数填充到dropdownlist_Asp.net_Vb.net_List_Loops_Populate - Fatal编程技术网

Asp.net 使用循环将整数填充到dropdownlist

Asp.net 使用循环将整数填充到dropdownlist,asp.net,vb.net,list,loops,populate,Asp.net,Vb.net,List,Loops,Populate,无法理解为什么我的程序不会使用循环填充我的DropDownList 到目前为止,我已经尝试了以下两种方案: ”填写从当前年份到100年前的年份框。 "(可以放心地假设没有超过一百岁的人会报名参加该网站) Dim CurYear作为整数=Date.Now.Year Dim CurYearMinus100为整数=CurYear-100 作为整数的Dim索引=0 当CurYear>=curyearminus 100时执行 dropdownlessyear.Items.Insert(索引,CurYear

无法理解为什么我的程序不会使用循环填充我的DropDownList

到目前为止,我已经尝试了以下两种方案:

”填写从当前年份到100年前的年份框。
"(可以放心地假设没有超过一百岁的人会报名参加该网站)
Dim CurYear作为整数=Date.Now.Year
Dim CurYearMinus100为整数=CurYear-100
作为整数的Dim索引=0
当CurYear>=curyearminus 100时执行
dropdownlessyear.Items.Insert(索引,CurYear)
指数+=1
CurYear-=1
环
我还尝试使用
.Add()
功能代替插入:

Dim CurYear As Integer=Date.Now.Year
Dim CurYearMinus100为整数=CurYear-100
当CurYear>=curyearminus 100时执行
DropDownlinear.Items.Add(索引)
CurYear-=1
环

不确定为什么需要
索引,但此代码可以工作:

Dim CurYear As Integer=Date.Now.Year
Dim CurYearMinus100为整数=CurYear-100
当CurYear>=curyearminus 100时执行
dropdownlessyear.Items.Add(新列表项(CurYear.ToString(),CurYear.ToString())
CurYear-=1
环

我个人是语句的粉丝,该语句带有递减步长参数

可选。数值表达式。计数器显示的数量 在循环中每次递增

你的例子是:

Dim currentYear=Date.Now.Year
年份=当前年份到当前年份-100步骤-1
dropdownStyear.Items.Add(新列表项(year.ToString(),year.ToString())
下一个
替换此:

DropDownListYear.Items.Insert(Index, CurYear)
与:

DropDownListYear.Items.Insert(Index, new ListItem(CurYear.ToString(), CurYear.ToString()))