无法弄清楚为什么我的程序不会使用循环填充我的 DropDownList

到目前为止,我已经尝试过这两种情况:

'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

我也试过使用 .Add() 功能而不是插入:

Dim CurYear As Integer = Date.Now.Year
Dim CurYearMinus100 As Integer = CurYear - 100

Do While CurYear >= CurYearMinus100
    DropDownListYear.Items.Add(Index)
    CurYear -= 1
Loop

最佳答案

不知道为什么你需要 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

关于asp.net - 使用循环将整数填充到下拉列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21463187/

10-11 16:45