奇怪问题
我用3个DropDownList分别显示,年,月,日. 假如我用一个按钮让它显示出来,怎么一点击,无论,日,选的是什么数字,最后都显示 1 呢?注:前两个DropDownList的autopostback属性为true.
年和月的DropDownList的item我是自己加上去的,日的是根据年月绑定的.呜呜.
如下:
protected void Page_Load(object sender, EventArgs e)
{
int year = Convert.ToInt32(this.yearlist.SelectedItem.Text);
int month = Convert.ToInt32(this.monthlist.SelectedItem.Text);
int day = dayCount(year, month);
this.daylist.Items.Clear();
for (int i = 1; i <= day; i++)
{
this.daylist.Items.Add(i.ToString());
}
}
// 判断是否是闰年
private bool isLeapYear(int year)
{
return (((year % 4 == 0) && (year % 100 != 0)) || ((year % 100 == 0) && (year % 400 == 0)));
}
// 根据年份和月份计算该月的日数
private int dayCount(int year, int month)
{
int day = 0;
if (isLeapYear(year))
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
day = 29;
break;
default:
day = -1;
break;
}
return day;
}
else
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
day = 28;
break;
default:
day = -1;
break;
}
return day;
}
}