Use three dropdownlist controls in ASP. NET to conveniently select a date

Source: Internet
Author: User
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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.