Dropdownlist无刷新年月日选择

其实用UpdataPanel很简单就可以实现的,这里试试用javascript的方法
 DropDownList1代表年, DropDownList2代表月, DropDownList3代表日。
由于用到的是 Dropdownlist服务器控件所以要在 Page_Load添假相关代码:
  protected   void  Page_Load( object  sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{   
            DropDownList2.Attributes.Add(
"onchange""return change()");
            DropDownList1.Attributes.Add(
"onchange""return change()");
            
for (int i = 1950; i <= 2010; i++)
            
{
                DropDownList1.Items.Add(i.ToString());
            }

            
for (int i = 1; i <= 12; i++)
            
{
                DropDownList2.Items.Add(i.ToString());
            }

        }

    }

主要是javascript代码:
function  change()
    
{
     
var year=document.getElementById("DropDownList1");
     
var month=document.getElementById("DropDownList2");
     
var yearValue=year.options[year.selectedIndex].text;
     
var monthValue=month.options[month.selectedIndex].text;
     
var x=document.getElementById("DropDownList3");
     
while(x.length>0)
     
{      
       x.remove(
0);
     }

     BindDays(yearValue,monthValue);
    }

function  BindDays(year,month)
{
  
switch(month)
  
{
     
case "1":
     
case "3":
     
case "5":
     
case "7":
     
case "8":
     
case "10":
     
case "12":
           
for(var i=1;i<=31;i++)
           
{
            
var y=document.createElement('option');
             y.text
=i.toString();
            
var x=document.getElementById("DropDownList3");
              
try
                
{
                x.add(y,
null); // standards compliant
                }

              
catch(ex)
                
{
                x.add(y); 
// IE only
                }

            }

       
break;
       
case "2":
           
if(CheckLeap(year)=="true")
           
{
              
for(var i=1;i<=29;i++)
               
{
                
var y=document.createElement('option');
                 y.text
=i.toString();
                
var x=document.getElementById("DropDownList3");
                  
try
                    
{
                    x.add(y,
null); // standards compliant
                    }

                  
catch(ex)
                    
{
                    x.add(y); 
// IE only
                    }

                }

           }

      
if(CheckLeap(year)=="false")
      
{
           
for(var i=1;i<=28;i++)
           
{
            
var y=document.createElement('option');
             y.text
=i.toString();
            
var x=document.getElementById("DropDownList3");
              
try
                
{
                x.add(y,
null); // standards compliant
                }

              
catch(ex)
                
{
                x.add(y); 
// IE only
                }

            }

      }

      
break;
    
case "4":
    
case "6":
    
case "9":
    
case "11":
          
for(var i=1;i<=30;i++)
               
{
                
var y=document.createElement('option');
                 y.text
=i.toString();
                
var x=document.getElementById("DropDownList3");
                  
try
                    
{
                    x.add(y,
null); // standards compliant
                    }

                  
catch(ex)
                    
{
                    x.add(y); 
// IE only
                    }

                }

  }

}
//判断是否为闰年
function  CheckLeap(year)
{
      
if ((year % 4 == 0 && year % 100 != 0|| (year % 100 == 0 && year % 400 == 0))
        
{
            
return "true";
        }

        
else
        
{
            
return "false";
        }

}
前台页面就是三个DropDownList控件:
< div  style ="text-align: center" >
        
< asp:DropDownList  ID ="DropDownList1"  runat ="server" >
        
</ asp:DropDownList >< span > </ span >
        
< asp:DropDownList  ID ="DropDownList2"  runat ="server"   >
        
</ asp:DropDownList >< span > </ span >
        
< asp:DropDownList  ID ="DropDownList3"  runat ="server" >
        
</ asp:DropDownList >< span > </ span >
    
</ div >

转载于:https://www.cnblogs.com/dushouke/archive/2008/06/04/1213808.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值