在 ASP.Net DropDownList 中加载年份
Load years in ASP.Net DropDownList
我需要用几年来填充我的 DropDownList,但我正在为此苦苦挣扎。我尝试了几个月并成功了。问题是我在为 DateTime 对象调用正确的方法时遇到问题。有人可以帮我吗?
这是我在 DropDownList 中加载月份的方式:
Sub LoadMonth()
Try
ddlMonth.Items.Clear()
If ddlMonth.Items.Count = 0 Then
For month As Integer = 1 To 12
Dim monthName As String = DateTimeFormatInfo.CurrentInfo.GetMonthName(month)
ddlMonth.Items.Add(New ListItem(monthName, month.ToString("00")))
Next
End If
ddlMonth.Items.Insert(0, New ListItem("--Select--", 1))
Catch ex As Exception
End Try
End Sub
您可以简单地使用如下循环实现它:
Dim i as integer
for i = 2008 to 2100
ddlYear.Items.Add(i.ToString());
next i
ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true; //set current year as selected
希望这对您有所帮助。
看看这个,可能对你有帮助:
Dim myList As New List(Of Integer)
myList.AddRange(Enumerable.Range(1950, 100).Reverse())
ddlYear.DataSource = myList
ddlYear.DataBind()
ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true;
很好用,谢谢。
private void LoadYears()
{
int i;
for (i = 2019; i <= 2125; i++)
{
cb_Years.Items.Add(i.ToString());
cb_Years.Items.FindByValue(System.DateTime.Now.Year.ToString());
}
string strYear = System.DateTime.Now.Year.ToString();
cb_Years.Items.Insert(0, new ListEditItem(strYear, Convert.ToString(0)));
cb_Years.SelectedIndex = 0;
}
我需要用几年来填充我的 DropDownList,但我正在为此苦苦挣扎。我尝试了几个月并成功了。问题是我在为 DateTime 对象调用正确的方法时遇到问题。有人可以帮我吗?
这是我在 DropDownList 中加载月份的方式:
Sub LoadMonth()
Try
ddlMonth.Items.Clear()
If ddlMonth.Items.Count = 0 Then
For month As Integer = 1 To 12
Dim monthName As String = DateTimeFormatInfo.CurrentInfo.GetMonthName(month)
ddlMonth.Items.Add(New ListItem(monthName, month.ToString("00")))
Next
End If
ddlMonth.Items.Insert(0, New ListItem("--Select--", 1))
Catch ex As Exception
End Try
End Sub
您可以简单地使用如下循环实现它:
Dim i as integer
for i = 2008 to 2100
ddlYear.Items.Add(i.ToString());
next i
ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true; //set current year as selected
希望这对您有所帮助。
看看这个,可能对你有帮助:
Dim myList As New List(Of Integer)
myList.AddRange(Enumerable.Range(1950, 100).Reverse())
ddlYear.DataSource = myList
ddlYear.DataBind()
ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected = true;
很好用,谢谢。
private void LoadYears()
{
int i;
for (i = 2019; i <= 2125; i++)
{
cb_Years.Items.Add(i.ToString());
cb_Years.Items.FindByValue(System.DateTime.Now.Year.ToString());
}
string strYear = System.DateTime.Now.Year.ToString();
cb_Years.Items.Insert(0, new ListEditItem(strYear, Convert.ToString(0)));
cb_Years.SelectedIndex = 0;
}