dropdownlist 显示日期

dropdownlist1代表年 autopostback=true

dropdownlist2代表月  autopostback=true

dropdownlist3代表日


    
protected   void  Page_Load( object  sender, EventArgs e)
    
{
        
string s = Request.QueryString["id"];
        Response.Write(Server.UrlDecode(s));

        DateTime tnow 
= DateTime.Now;
        ArrayList years 
= new ArrayList();
        ArrayList months 
= new ArrayList();
        
int i;
        
for (i = 1991; i >= 1958; i--)
        
{
            years.Add(i);
        }

        
for (i = 1; i <= 12; i++)
        
{
            months.Add(i);
        }


        
if (!IsPostBack)
        
{
            DropDownList1.DataSource 
= years;
            DropDownList1.SelectedValue 
=years[8].ToString();
            DropDownList1.DataBind();

            DropDownList2.DataSource 
= months;
            DropDownList2.SelectedValue 
= tnow.Month.ToString();
            DropDownList2.DataBind();

            
int year, month;
            Int32.TryParse(tnow.Year.ToString(), 
out year);
            Int32.TryParse(tnow.Month.ToString(), 
out month);

            binddropdownlist3(year, month);
            DropDownList3.SelectedValue 
= tnow.Day.ToString();
        }

    }


    
protected   bool  checkLeap( int  year)
    
{
        
if ((year % 4 != 0&& (year % 100 != 0|| (year % 400 == 0))
        

            
return true
        }

        
else
        
{
            
return false;
        }

    }


    
protected   void  binddropdownlist3( int  year,  int  month)
    
{
        
int i;
        ArrayList days 
= new ArrayList();
        
switch (month)
        

            
case 1:
            
case 3:
            
case 5:
            
case 7:
            
case 8:
            
case 10:
            
case 12:
                
for (i = 1; i <= 31; i++)
                
{
                    days.Add(i);
                }

                
break;
            
case 2:
                
if (checkLeap(year))
                
{
                    
for (i = 1; i <= 29; i++)
                    
{
                        days.Add(i);
                    }

                }

                
else
                
{
                    
for (i = 1; i <= 28; i++)
                    
{
                        days.Add(i);
                    }
                    
                }

                
break;
            
case 4:
            
case 6:
            
case 9:
            
case 11:
                
for (i = 1; i <= 30; i++)
                
{
                    days.Add(i);
                }

                
break;
        }

        DropDownList3.DataSource 
= days;
        DropDownList3.DataBind();

    }

    
protected   void  DropDownList1_SelectedIndexChanged( object  sender, EventArgs e)
    
{
        
int year, month;
        Int32.TryParse(DropDownList1.SelectedValue.ToString(), 
out year);
        Int32.TryParse(DropDownList2.SelectedValue.ToString(),
out month);
        binddropdownlist3(year, month);
    }

    
protected   void  DropDownList2_SelectedIndexChanged( object  sender, EventArgs e)
    
{
        
int year, month;
        Int32.TryParse(DropDownList1.SelectedValue.ToString(),
out year);
        Int32.TryParse(DropDownList2.SelectedValue.ToString(),
out month);
        binddropdownlist3(year, month);
    }

但是这种做法会刷新页面,不是很好!

http://www.fayland.org/journal/Date_Selector.html 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值