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>

posted @ 2008-06-04 18:01  MicroCoder  阅读(1143)  评论(0编辑  收藏  举报