There are three DropDownList controls on the Asp.net| control aspx page.
DropDownList1 said the year, DropDownList2 said the month, DropDownList3 said days;
Note Set the AutoPostBack property of these three DropDownList controls to true.
Users can easily select the year and month, and the monthly date will change with the user selection of different years, months and corresponding changes
The following table CS file code is as follows:
private void Page_Load (object sender, System.EventArgs e)
{
DateTime tnow=datetime.now;//now time
ArrayList alyear=new ArrayList ();
int i;
for (i=2002;i<=2010;i++)
Alyear.add (i);
ArrayList almonth=new ArrayList ();
for (i=1;i<=12;i++)
Almonth.add (i);
if (!this. IsPostBack)
{
Dropdownlist1.datasource=alyear;
Dropdownlist1.databind ()//binding year
Choose to be the year before
Dropdownlist1.selectedvalue=tnow. Year.tostring ();
Dropdownlist2.datasource=almonth;
Dropdownlist2.databind ()/Bind month
Select Current Month
Dropdownlist2.selectedvalue=tnow. Month.tostring ();
int year,month;
Year=int32.parse (Dropdownlist1.selectedvalue);
Month=int32.parse (Dropdownlist2.selectedvalue);
Binddays (year,month);//Bind Day
Select Current Date
Dropdownlist3.selectedvalue=tnow. Day.tostring ();
}
}
Judging leap years
private bool Checkleap (int year)
{
if ((year%4==0) && (year%100!=0) | | (year%400==0))
return true;
else return false;
}
Bind the number of days per month
private void binddays (int year,int month)
{int i;
ArrayList alday=new ArrayList ();
Switch (month)
{
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
for (i=1;i<=31;i++)
Alday.add (i);
Break
Case 2:
if (Checkleap (year))
{for (i=1;i<=29;i++)
Alday.add (i);}
Else
{for (i=1;i<=28;i++)
Alday.add (i);}
Break
Case 4:
Case 6:
Case 9:
Case 11:
for (i=1;i<=30;i++)
Alday.add (i);
Break
}
Dropdownlist3.datasource=alday;
Dropdownlist3.databind ();
}
Select year
private void DropDownList1_SelectedIndexChanged (object sender, System.EventArgs e)
{
int year,month;
Year=int32.parse (Dropdownlist1.selectedvalue);
Month=int32.parse (Dropdownlist2.selectedvalue);
Binddays (Year,month);
}
Select Month
private void Dropdownlist2_selectedindexchanged (object sender, System.EventArgs e)
{
int year,month;
Year=int32.parse (Dropdownlist1.selectedvalue);
Month=int32.parse (Dropdownlist2.selectedvalue);
Binddays (Year,month);
}