一个简单的日期控件
这个日期控件就是三个下拉框的简单组合,但用起来还是挺顺手的,呵呵。
控件类:
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 Object, ByVal 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 <> 1) Then
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 <> 1) Then
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 <> 1) Then
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 <> 1) Then
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 Object, ByVal 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
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 Object, ByVal 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 <> 1) Then
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 <> 1) Then
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 <> 1) Then
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 <> 1) Then
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 Object, ByVal 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-1, 1);
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;
}
{
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-1, 1);
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;
}
还有一些功能可以加入,比如:设置日期段,设置是否可以选择今天以前等等,慢慢往里加,如果有哪位朋友作了改进,别忘了告知一下。
---------------------------
http://www.cnblogs.com/squirrel_sc
1. 如有引用,请包含本段文字及原始出处。以表示对作者的尊重,且能将有相似想法的人联系起来。
2. 如无特殊说明,本文的文字代表作者的观点和想法。由于人类的记忆和连通性的限制,可能会将别人的观点和想法当成自己的(会尽量避免,但我读书少,别蒙我:D);或有人会有同样的想法(那就太好了)。若有此类情况,请联系我。我会很高兴更新内容。
3. 每篇文章会根据反馈或新的想法,随时更新。我会尽量记得更新版本号。