dropdownlist1代表年 autopostback=true
dropdownlist2代表月 autopostback=true
dropdownlist3代表日
protected
void
Page_Load(
object
sender, EventArgs e)
...
{
string s = Request.QueryString["id"];
Response.Write(Server.UrlDecode(s));
DateTime tnow = DateTime.Now;
ArrayList years = new ArrayList();
ArrayList months = new ArrayList();
int i;
for (i = 1991; i >= 1958; i--)
...{
years.Add(i);
}
for (i = 1; i <= 12; i++)
...{
months.Add(i);
}
if (!IsPostBack)
...{
DropDownList1.DataSource = years;
DropDownList1.SelectedValue =years[8].ToString();
DropDownList1.DataBind();
DropDownList2.DataSource = months;
DropDownList2.SelectedValue = tnow.Month.ToString();
DropDownList2.DataBind();
int year, month;
Int32.TryParse(tnow.Year.ToString(), out year);
Int32.TryParse(tnow.Month.ToString(), out month);
binddropdownlist3(year, month);
DropDownList3.SelectedValue = tnow.Day.ToString();
}
}

protected
bool
checkLeap(
int
year)
...
{
if ((year % 4 != 0) && (year % 100 != 0) || (year % 400 == 0))
...{
return true;
}
else
...{
return false;
}
}

protected
void
binddropdownlist3(
int
year,
int
month)
...
{
int i;
ArrayList days = new ArrayList();
switch (month)
...{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
for (i = 1; i <= 31; i++)
...{
days.Add(i);
}
break;
case 2:
if (checkLeap(year))
...{
for (i = 1; i <= 29; i++)
...{
days.Add(i);
}
}
else
...{
for (i = 1; i <= 28; i++)
...{
days.Add(i);
}
}
break;
case 4:
case 6:
case 9:
case 11:
for (i = 1; i <= 30; i++)
...{
days.Add(i);
}
break;
}
DropDownList3.DataSource = days;
DropDownList3.DataBind();
}
protected
void
DropDownList1_SelectedIndexChanged(
object
sender, EventArgs e)
...
{
int year, month;
Int32.TryParse(DropDownList1.SelectedValue.ToString(), out year);
Int32.TryParse(DropDownList2.SelectedValue.ToString(),out month);
binddropdownlist3(year, month);
}
protected
void
DropDownList2_SelectedIndexChanged(
object
sender, EventArgs e)
...
{
int year, month;
Int32.TryParse(DropDownList1.SelectedValue.ToString(),out year);
Int32.TryParse(DropDownList2.SelectedValue.ToString(),out month);
binddropdownlist3(year, month);
}
但是这种做法会刷新页面,不是很好!
本文介绍了一种使用ASP.NET的DropDownList控件实现日期选择的方法。通过三个下拉列表分别选择年、月、日,并根据所选年份和月份动态更新天数选项。文章还讨论了处理闰年的细节。
1777

被折叠的 条评论
为什么被折叠?



