I am working on a simple user control composed of 3 DropDownLists that will be used to select Dates. The purpose of the control is to all the user to choose a date using dropdown lists but not need to worry about choosing a non-existing date (due to different months having different numbers of days). They all have AutoPostBack="T rue", but when the eventhandler attempts to use the SelectedIndex property, it is always the same. Here is the code:
Public Class DatePicker
Inherits System.Web.UI.U serControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
End Sub
Private selectdate As Date = Date.Today
Private startyear As Integer = 1900
Private stopyear As Integer = 2100
Protected WithEvents ddlMonth As System.Web.UI.W ebControls.Drop DownList
Protected WithEvents ddlDate As System.Web.UI.W ebControls.Drop DownList
Protected WithEvents ddlYear As System.Web.UI.W ebControls.Drop DownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Public Property SelectedDate() As Date
Get
Return Me.selectdate
End Get
Set(ByVal Value As Date)
Me.selectdate = Value
Me.startyear = Math.Min(Me.sel ectdate.Year, Me.startyear)
Me.stopyear = Math.Max(Me.sel ectdate.Year, Me.stopyear)
Me.CreateLists( )
End Set
End Property
Public Property FirstYear() As Integer
Get
Return Me.startyear
End Get
Set(ByVal Value As Integer)
If Value <= Me.selectdate.Y ear AndAlso Value >= 1 Then
Me.startyear = Value
Me.CreateLists( )
End If
End Set
End Property
Public Property LastYear() As Integer
Get
Return Me.stopyear
End Get
Set(ByVal Value As Integer)
If Value >= Me.selectdate.Y ear AndAlso Value <= 9999 Then
Me.stopyear = Value
Me.CreateLists( )
End If
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
If Not IsPostBack() Then Me.CreateLists( )
End Sub
Private Sub CreateLists()
ddlMonth.Items. Clear()
ddlYear.Items.C lear()
ddlDate.Items.C lear()
For i As Integer = 1 To 12
ddlMonth.Items. Add(New ListItem(System .Globalization. DateTimeFormatI nfo.CurrentInfo .GetMonthName(i ), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.A dd(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMont h(Me.selectdate .Year, Me.selectdate.M onth)
ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(Me.selectd ate.Year, Me.selectdate.M onth, i).DayOfWeek), CStr(i)))
Next
ddlMonth.Select edIndex = Me.selectdate.M onth - 1
ddlYear.Selecte dIndex = Me.selectdate.Y ear - Me.startyear
ddlDate.Selecte dIndex = Me.selectdate.D ay - 1
End Sub
Private Sub DateChanged(ByV al sender As Object, ByVal e As System.EventArg s) Handles ddlMonth.Select edIndexChanged, ddlYear.Selecte dIndexChanged, ddlDate.Selecte dIndexChanged
Dim selectedday As Integer = ddlDate.Selecte dIndex + 1
ddlDate.Items.C lear()
For i As Integer = 1 To Date.DaysInMont h(ddlYear.Selec tedIndex + Me.startyear, ddlMonth.Select edIndex + 1)
ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(ddlYear.Se lectedIndex + Me.startyear, ddlMonth.Select edIndex + 1, i).DayOfWeek), CStr(i)))
Next
If Date.DaysInMont h(ddlYear.Selec tedIndex + Me.startyear, ddlMonth.Select edIndex + 1) >= selectedday Then
ddlDate.Selecte dIndex = selectedday - 1
Else
ddlDate.Selecte dIndex = Date.DaysInMont h(ddlYear.Selec tedIndex + Me.startyear, ddlMonth.Select edIndex + 1) - 1
End If
Me.selectdate = New Date(ddlYear.Se lectedIndex + Me.startyear, ddlMonth.Select edIndex + 1, ddlDate.Selecte dIndex + 1)
End Sub
End Class
<%@ Control Language="vb" AutoEventWireup ="false" Codebehind="Dat ePicker.ascx.vb " Inherits="WebAp plication1.Date Picker" TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5" %>
<asp:dropdownli st id="ddlMonth" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
<asp:dropdownli st id="ddlDate" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
<asp:dropdownli st id="ddlYear" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
--
Nathan Sokalski
nj********@hotm ail.com
http://www.nathansokalski.com/