[英]Populate integers to dropdownlist using a loop
Can't figure out why my program wont populate my DropDownList using a loop. 无法弄清楚为什么我的程序不会使用循环填充我的DropDownList 。
So far I've tried these two scenarios: 到目前为止,我已经尝试过这两种情况:
'Fill in the year box from current year to 100 years ago.
'(Safe to assume no one over 100 will be signing up for site)
Dim CurYear As Integer = Date.Now.Year
Dim CurYearMinus100 As Integer = CurYear - 100
Dim Index As Integer = 0
Do While CurYear >= CurYearMinus100
DropDownListYear.Items.Insert(Index, CurYear)
Index += 1
CurYear -= 1
Loop
I've also tried using the .Add()
functionality instead of insert: 我也尝试使用.Add()
功能而不是insert:
Dim CurYear As Integer = Date.Now.Year
Dim CurYearMinus100 As Integer = CurYear - 100
Do While CurYear >= CurYearMinus100
DropDownListYear.Items.Add(Index)
CurYear -= 1
Loop
Not sure why you need Index
, but this code will work: 不确定为什么需要Index
,但这段代码可以正常工作:
Dim CurYear As Integer = Date.Now.Year
Dim CurYearMinus100 As Integer = CurYear - 100
Do While CurYear >= CurYearMinus100
DropDownListYear.Items.Add(New ListItem(CurYear.ToString(), CurYear.ToString()))
CurYear -= 1
Loop
I personally am a fan of the For...Next statement with a decrementing Step argument. 我个人是For ... Next语句的粉丝,带有递减的Step参数。
Optional. 可选的。 Numeric expression. 数字表达式。 The amount by which counter is incremented each time through the loop. 计数器每次循环递增的量。
Your example would be: 你的例子是:
Dim currentYear = Date.Now.Year
For year = currentYear To currentYear - 100 Step -1
DropDownListYear.Items.Add(New ListItem(year.ToString(), year.ToString()))
Next
Replace this: 替换这个:
DropDownListYear.Items.Insert(Index, CurYear)
With: 附:
DropDownListYear.Items.Insert(Index, new ListItem(CurYear.ToString(), CurYear.ToString()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.