基于Asp.net与Javascript控制的日期控件

控件效果如下:

从左到右:month,day,year

.cs文件初始化这三个下拉列表


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;
}

js代码如下(自己写的,不保证完全正确啊):

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"));
}
}
}

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...