Eric的日期选择下拉列表小控件

利用JS实现日期选择功能,包括润年2月的日数变更, 简单实用!

主要属性: Date

设计时图示:  

源代码:

using  System;
using  System.ComponentModel;
using  System.Text;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;

[assembly: TagPrefix(
" EFPlatform.WebControls " " efp " )]
namespace  EFPlatform.WebControls
{
    [DefaultProperty(
" Date " )]
    [ToolboxData(
" <{0}:DateDropDownList runat=server></{0}:DateDropDownList> " )]
    
public   class  DateDropDownList : WebControl, INamingContainer
    {
        
#region  Properties

        
#region  Data(数据)
        
#region  Date
        [Bindable(
true )]
        [Category(
" Data " )]
        [DefaultValue(
" 1900-1-1 " )]
        
public  DateTime Date
        {
            
get
            {
                
this .GetDate();
                
return  (ViewState[ " Date " ==   null ?  ( new  DateTime( 1900 1 1 )) : (DateTime)ViewState[ " Date " ];
            }

            
set
            {
                ViewState[
" Date " =  value;
            }
        }
        
#endregion

        
#region  FirstYear
        [Category(
" Data " )]
        [DefaultValue(
1900 )]
        
public   int  FirstYear
        {
            
get
            {
                
return  (ViewState[ " FirstYear " ==   null ?   1900  : ( int )ViewState[ " FirstYear " ];
            }

            
set
            {
                ViewState[
" FirstYear " =  value;
            }
        }
        
#endregion

        
#region  LastYear
        [Category(
" Data " )]
        
public   int  LastYear
        {
            
get
            {
                
return  (ViewState[ " LastYear " ==   null ?  DateTime.Now.Year : ( int )ViewState[ " LastYear " ];
            }

            
set
            {
                ViewState[
" LastYear " =  value;
            }
        }
        
#endregion
        
#endregion

        
#region  Appearance(外观)
        
#region  SelectCssClass
        [Category(
" Appearance " )]
        
public   string  SelectCssClass
        {
            
get
            {
                
return  (ViewState[ " SelectCssClass " ==   null ?   string .Empty : ( string )ViewState[ " SelectCssClass " ];
            }

            
set
            {
                ViewState[
" SelectCssClass " =  value;
            }
        }
        
#endregion

        
#region  YearTitle
        [Category(
" Appearance " )]
        [DefaultValue(
" " )]
        
public   string  YearTitle
        {
            
get
            {
                
return  (ViewState[ " YearTitle " ==   null ?   " "  : ( string )ViewState[ " YearTitle " ];
            }

            
set
            {
                ViewState[
" YearTitle " =  value;
            }
        }
        
#endregion

        
#region  MonthTitle
        [Category(
" Appearance " )]
        [DefaultValue(
" " )]
        
public   string  MonthTitle
        {
            
get
            {
                
return  (ViewState[ " MonthTitle " ==   null ?   " "  : ( string )ViewState[ " MonthTitle " ];
            }

            
set
            {
                ViewState[
" MonthTitle " =  value;
            }
        }
        
#endregion

        
#region  DayTitle
        [Category(
" Appearance " )]
        [DefaultValue(
" " )]
        
public   string  DayTitle
        {
            
get
            {
                
return  (ViewState[ " DayTitle " ==   null ?   " "  : ( string )ViewState[ " DayTitle " ];
            }

            
set
            {
                ViewState[
" DayTitle " =  value;
            }
        }
        
#endregion
        
#endregion

        
#endregion

        
#region  GetDate
        
private   void  GetDate()
        {
            
if (Page.IsPostBack)
            {
                
string  strYear  =  Page.Request.Form[ this .ClientID  +   " _Year " ];
                
string  strMonth  =  Page.Request.Form[ this .ClientID  +   " _Month " ];
                
string  strDay  =  Page.Request.Form[ this .ClientID  +   " _Day " ];
                ViewState[
" Date " =  DateTime.Parse( string .Format( " {0}-{1}-{2} " , strYear, strMonth, strDay));
            }
        }
        
#endregion

        
#region  BuildJScript
        
private   string  BuildJScript()
        {
            StringBuilder sb 
=   new  StringBuilder();
            sb.Append(
" <script language="jscript"> <!-- " );
            sb.Append(
" function {0}_Day_Check() {{ " );
            sb.Append(
" var {0}_obj_Year = document.getElementById("{0}_Year"); " );
            sb.Append(
" var {0}_obj_Month = document.getElementById("{0}_Month"); " );
            sb.Append(
" if(parseInt({0}_obj_Month.value) == 2) {{ " );
            sb.Append(
" if(parseInt({0}_obj_Year.value) % 4 == 0) {{ " );
            sb.Append(
" {0}_Day_SetOptions(29); }} " );
            sb.Append(
" else {{ " );
            sb.Append(
" {0}_Day_SetOptions(28); }} }} " );
            sb.Append(
" else {{ " );
            sb.Append(
" switch(parseInt({0}_obj_Month.value)) {{ " );
            sb.Append(
" case 1: {0}_Day_SetOptions(31); break; " );
            sb.Append(
" case 3: {0}_Day_SetOptions(31); break; " );
            sb.Append(
" case 5: {0}_Day_SetOptions(31); break; " );
            sb.Append(
" case 7: {0}_Day_SetOptions(31); break; " );
            sb.Append(
" case 8: {0}_Day_SetOptions(31); break; " );
            sb.Append(
" case 10: {0}_Day_SetOptions(31); break; " );
            sb.Append(
" case 12: {0}_Day_SetOptions(31); break; " );
            sb.Append(
" case 4: {0}_Day_SetOptions(30); break; " );
            sb.Append(
" case 6: {0}_Day_SetOptions(30); break; " );
            sb.Append(
" case 9: {0}_Day_SetOptions(30); break; " );
            sb.Append(
" case 11: {0}_Day_SetOptions(30); break; " );
            sb.Append(
" default: break; }} }} }} " );
            sb.Append(
" " );
            sb.Append(
" function {0}_Day_SetOptions({0}_Day_Count) {{ " );
            sb.Append(
" var {0}_obj_Day = document.getElementById("{0}_Day"); " );
            sb.Append(
" var {0}_Day_SelectedDay; " );
            sb.Append(
" if({0}_obj_Day.value == "") {{ " );
            sb.Append(
" {0}_Day_SelectedDay = 1; }} " );
            sb.Append(
" else {{ " );
            sb.Append(
" {0}_Day_SelectedDay = parseInt({0}_obj_Day.value); }} " );
            sb.Append(
" if({0}_Day_SelectedDay > {0}_Day_Count) {{ " );
            sb.Append(
" {0}_Day_SelectedDay = {0}_Day_Count; }} " );
            sb.Append(
" {0}_obj_Day.length = 0; " );
            sb.Append(
" for(var i = 1; i <= {0}_Day_Count; i++) {{ " );
            sb.Append(
" {0}_obj_Day.options.add(new Option(i, i)); }} " );
            sb.Append(
" {0}_obj_Day.value = {0}_Day_SelectedDay; }} " );
            sb.Append(
" --> </script> " );
            
return   string .Format(sb.ToString(),  this .ClientID);
        }
        
#endregion

        
#region  BuildHtmlElement
        
private   string  BuildHtmlElement()
        {
            StringBuilder sb 
=   new  StringBuilder();
            sb.AppendFormat(
" <select id="{0}_Year" name="{0}_Year" onchange="{0}_Day_Check();" class="{1}"> " this .ClientID,  this .SelectCssClass);

            
for ( int  i  =   this .FirstYear; i  <=   this .LastYear; i ++ )
            {
                
if (i  ==   this .Date.Year)
                {
                    sb.AppendFormat(
" <option value="{0}" selected>{0}</option> " , i);
                }
                
else
                {
                    sb.AppendFormat(
" <option value="{0}">{0}</option> " , i);
                }
            }

            sb.AppendFormat(
" </select> {1}<select id="{0}_Month" name="{0}_Month" onchange="{0}_Day_Check();" class="{2}"> " this .ClientID,  this .YearTitle,  this .SelectCssClass);

            
for ( int  i  =   1 ; i  <=   12 ; i ++ )
            {
                
if (i  ==   this .Date.Month)
                {
                    sb.AppendFormat(
" <option value="{0}" selected>{0}</option> " , i);
                }
                
else
                {
                    sb.AppendFormat(
" <option value="{0}">{0}</option> " , i);
                }
            }

            sb.AppendFormat(
" </select>{1}<select id="{0}_Day" name="{0}_Day" onmouseover="{0}_Day_Check();" class="{2}"> " this .ClientID,  this .MonthTitle,  this .SelectCssClass);

            
for ( int  i  =   1 ; i  <=  DateTime.DaysInMonth( this .Date.Year,  this .Date.Month); i ++ )
            {
                
if (i  ==   this .Date.Day)
                {
                    sb.AppendFormat(
" <option value="{0}" selected>{0}</option> " , i);
                }
                
else
                {
                    sb.AppendFormat(
" <option value="{0}">{0}</option> " , i);
                }
            }

            sb.AppendFormat(
" </select>{0} " this .DayTitle);
            
return  sb.ToString();
        }
        
#endregion

        
#region  override RenderChildren
        
protected   override   void  RenderChildren(HtmlTextWriter writer)
        {
            writer.Write(
this .BuildJScript());
            writer.Write(
this .BuildHtmlElement());
            
base .RenderChildren(writer);
        }
        
#endregion
    }
}

你可能感兴趣的:(下拉列表)