一个简单的日期控件

这个日期控件就是三个下拉框的简单组合,但用起来还是挺顺手的,呵呵。
控件类:

Imports System.ComponentModel
Imports System.Web.UI

<ToolboxData("<{0}:DateControl2 runat=server></{0}:DateControl2>")> Public Class DateControl2
    
Inherits System.Web.UI.WebControls.WebControl

    
Private _class As String
    
Private _initDate As DateTime

    
Private listYear As New System.Web.UI.HtmlControls.HtmlSelect
    
Private listMonth As New System.Web.UI.HtmlControls.HtmlSelect
    
Private listDay As New System.Web.UI.HtmlControls.HtmlSelect

    
Private Sub DateControl2_Init(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Init
        initControl()
    
End Sub


    
'设置列表的css样式
    <Category("Appearance"), DefaultValue("")> Shadows Property CssClass() As String
        
Get
            
Return _class
        
End Get
        
Set(ByVal Value As String)
            _class 
= Value
        
End Set
    
End Property


    
'初始日期,不设置的话,初始为当天
    <Category("Appearance"), DefaultValue("")> Shadows Property InitDate() As DateTime
        
Get
            
Return _initDate
        
End Get
        
Set(ByVal Value As DateTime)
            _initDate 
= Value
        
End Set
    
End Property


    
Private Sub initControl()
        Me.Controls.Clear()
        
Dim lt As WebControls.Literal
        Me.Controls.Add(listYear)
        lt 
= New WebControls.Literal
        lt.Text 
= "年"
        Me.Controls.Add(lt)
        Me.Controls.Add(listMonth)
        lt 
= New WebControls.Literal
        lt.Text 
= "月"
        Me.Controls.Add(lt)
        Me.Controls.Add(listDay)
        lt 
= New WebControls.Literal
        lt.Text 
= "日"
        Me.Controls.Add(lt)
        initYear()
        initMonth()
        initDay()
    
End Sub


    
Private Sub initYear()
        
Dim i As Integer
        listYear.Items.Clear()
        
For i = Now.Year To 1900 Step -1
            listYear.Items.Add(i)
        
Next
        listYear.EnableViewState 
= False
        listYear.ID 
= Me.ClientID & "_Year"
        If (_initDate.Year <> 1Then
            listYear.Value 
= _initDate.Year
        
Else
            listYear.Value 
= Now.Year
        
End If
        listYear.Attributes.Add(
"class", _class)
        listYear.Attributes.Add(
"onchange""SetWellDate('" & Me.ClientID & "')")
    
End Sub


    
Private Sub initMonth()
        
Dim i As Integer
        listMonth.Items.Clear()
        
For i = 1 To 12
            listMonth.Items.Add(i)
        
Next
        
If (_initDate.Year <> 1Then
            listMonth.Value 
= _initDate.Month
        
Else
            listMonth.Value 
= Now.Month
        
End If
        listMonth.EnableViewState 
= False
        listMonth.ID 
= Me.ClientID & "_Month"
        listMonth.Attributes.Add("onchange""SetWellDate('" & Me.ClientID & "')")
    
End Sub


    
Private Sub initDay()
        
Dim i As Integer
        
Dim j As Integer
        listDay.Items.Clear()
        
If (_initDate.Year <> 1Then
            j 
= DateTime.DaysInMonth(_initDate.Year, _initDate.Month)
        
Else
            j 
= DateTime.DaysInMonth(Now.Year, Now.Month)
        
End If
        
For i = 1 To j
            listDay.Items.Add(i)
        
Next
        
If (_initDate.Year <> 1Then
            listDay.Value 
= _initDate.Day
        
Else
            listDay.Value 
= Now.Day
        
End If
        listDay.ID 
= Me.ClientID & "_Day"
        listDay.EnableViewState = False
    
End Sub


    
Private Sub DateControl2_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Load
        Page.RegisterClientScriptBlock(
"DateControl2""<script language='javascript' src='/js/DateControl2.js'></script>")
        Page.RegisterStartupScript(Me.ClientID, 
"<script language='javascript'>SetWellDate('" & Me.ClientID & "')</script>")
    
End Sub


    
Public Property SelectedDate() As Date
        
Get
            
Return New DateTime(listYear.Value, listMonth.Value, listDay.Value)
        
End Get
        
Set(ByVal Value As Date)
            _initDate 
= Value
            initControl()
        
End Set
    
End Property

End Class


DateControl2.js文件内容:

function SetWellDate(sName)
{
    dtYear 
= eval("document.all." + sName + "_Year");
    dtMonth 
= eval("document.all." + sName + "_Month");
    dtDay 
= eval("document.all." + sName + "_Day");
    dt1 
= new Date(dtYear.value, dtMonth.value-11);
    dt2 
= new Date(dtYear.value, dtMonth.value, 1);
    i 
= (dt2 - dt1) / 24 / 60 / 60 / 1000;
    
if(i != dtDay.length)
    {
        
if(i > dtDay.length)
        {
            i 
= i - dtDay.length;
            dtDay.length 
= dtDay.length + i;
            i 
= dtDay.length - i;
            
for(i++; i<=dtDay.length; i++)
            {
                dtDay.options[i
-1].value = i;
                dtDay.options[i
-1].text = i;
            }
        }
        
else
        {
            dtDay.length 
= i;
        }
    }
    dtDay.value;
}


还有一些功能可以加入,比如:设置日期段,设置是否可以选择今天以前等等,慢慢往里加,如果有哪位朋友作了改进,别忘了告知一下。

posted @ 2004-10-08 21:56  squirrel_sc  阅读(3747)  评论(4编辑  收藏  举报