Home > Web Front-end > JS Tutorial > body text

Date control based on Asp.net and Javascript control_javascript skills

WBOY
Release: 2016-05-16 18:26:37
Original
1137 people have browsed it

The effect of the control is as follows:

Date control based on Asp.net and Javascript control_javascript skills

From left to right: month, day, year

.cs file initializes these three drop-down lists

Copy the code The 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 (written by myself, not guaranteed to be completely correct):
Copy code The 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"));
}
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template