用户注册表中日期输入的解决方案
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
for (int i = 1900; i <= 2100; i++)
{
this.DropDownList1.Items.Add(i.ToString());
}
for (int j = 1; j <= 12; j++)
{
this.DropDownList2.Items.Add(j.ToString());
}
FillDay();
}
}
protected int getDays()
{
int year = Convert.ToInt32(this.DropDownList1.SelectedValue);
int month = Convert.ToInt32(this.DropDownList2.SelectedValue);
int days = 0;
//下面是得到当月的天数。。。
switch (month)
{
case 1:
days = 31; break;
case 2:
if (DateTime.IsLeapYear(year))
{
//闰年二月为29天
days = 29;
}
else
{
//非闰年是28天哟。。
days = 28;
}
break;
case 3:
days = 31; break;
case 4:
days = 30; break;
case 5:
days = 31; break;
case 6:
days = 30; break;
case 7:
days = 31; break;
case 8:
days = 31; break;
case 9:
days = 30; break;
case 10:
days = 31; break;
case 11:
days = 30; break;
case 12:
days = 31; break;
}
return days;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
FillDay();
}
protected void FillDay()
{
int days;
DropDownList3.Items.Clear();
days = getDays();
for (int day = 1; day <= days; day++)
{
this.DropDownList3.Items.Add(day.ToString());
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
FillDay();
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = "你选择的日期是:" + DropDownList1.SelectedValue+"年"+ DropDownList2.SelectedValue+"月"+ DropDownList3.SelectedValue+"日";
}
}