The control effect is as follows:
From left to right: month, day, year
. Cs file initialization drop-down lists
Copy codeThe Code is as follows:
Private void BindBirthDay (int day, int month, int year)
{
Int dayNow = day;
Int monNow = month;
Int yearNow = year;
// Binding Month
For (int I = 1; I <= 12; I ++)
{
DdlBirMon. Items. Add (new ListItem (I. ToString (), I. ToString ()));
}
DdlBirMon. Items [monNow-1]. Selected = true;
// Binding Day
Int daysOfMonth = DateTime. DaysInMonth (yearNow, monNow );
For (int I = 1; I <= daysOfMonth; I ++)
{
DdlBirDay. Items. Add (new ListItem (I. ToString (), I. ToString ()));
}
DdlBirDay. Items [dayNow-1]. Selected = true;
// Binding Year
For (int I = 20; I> 0; I --)
{
DdlBirYear. Items. Add (new ListItem (yearNow-I). ToString (), (yearNow-I). ToString ()));
}
For (int I = 0; I <20; I ++)
{
DdlBirYear. Items. Add (new ListItem (yearNow + I). ToString (), (yearNow + I). ToString ()));
}
DdlBirYear. Items. FindByValue (yearNow. ToString (). Selected = true;
}
The js Code is as follows (self-written, not necessarily completely correct ):
Copy codeThe Code is as follows:
Function ChangeDay (){
Var month = document. getElementById ("<% = ddlBirMon. ClientID %> ");
Var year = document. getElementById ("<% = ddlBirYear. ClientID %> ");
Var day = document. getElementById ("<% = ddlBirDay. ClientID %> ");
If (month. selectedIndex = 3 | month. selectedIndex = 5 | month. selectedIndex = 8 | month. selectedIndex = 10 ){
If (day. length = 31 ){
If (day. options [30]. selected = true ){
Day. options [29]. selected = true;
}
Day. remove (30 );
}
}
Else {
While (day. length <31 ){
Day. add (new Option (day. length + 1, day. length + 1 ));
}
}
If (month. selectedIndex = 1 ){
If (day. length> 28 ){
If (day. selectedIndex = 28 ){
Day. options [27]. selected = true;
}
While (day. length> 28 ){
Day. remove (day. length-1 );
}
}
Var sy = year. options [year. selectedIndex]. value;
If (sy % 4 = 0 & sy % 100! = 0) | (sy % 400 = 0 )){
Day. add (new Option ("29", "29 "));
}
}
}