The ASPX page has three dropdownlist controls,
Dropdownlist1 indicates the year, dropdownlist2 indicates the month, and dropdownlist3 indicates the day;
Set the autopostback attribute of the three dropdownlist controls to true.
Users can easily select the year, month, and month dates change accordingly as users select different years and months.
Its Background CS FileCodeAs follows:
private void page_load (Object sender, system. eventargs e)
{< br> datetime tnow = datetime. now; // current 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)
{< br> dropdownlist1.datasource = alyear;
dropdownlist1.databind (); // bind the year
// select the current year
dropdownlist1.selectedvalue = tnow. year. tostring ();
dropdownlist2.datasource = almonth;
dropdownlist2.databind (); // bind a month
// select the current month
dropdownlist2.selectedvalue = tnow. month. tostring ();
int year, month;
year = int32.parse (dropdownlist1.selectedvalue);
month = int32.parse (dropdownlist2.selectedvalue);
binddays (year, month); // bind the day
// select the current date
dropdownlist3.selectedvalue = tnow. day. tostring ();
}< BR >}
// Determine the leap year
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 dropdownlistpolicselectedindexchanged (Object sender, system. eventargs E)
{
Int year, month;
Year = int32.parse (dropdownlist1.selectedvalue );
Month = int32.parse (dropdownlist2.selectedvalue );
Binddays (year, month );
}
// Select a 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 );
}