N
Nathan Sokalski
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="True", 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.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
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.WebControls.DropDownList
Protected WithEvents ddlDate As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlYear As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
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.selectdate.Year, Me.startyear)
Me.stopyear = Math.Max(Me.selectdate.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.Year 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.Year 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.EventArgs) Handles MyBase.Load
If Not IsPostBack() Then Me.CreateLists()
End Sub
Private Sub CreateLists()
ddlMonth.Items.Clear()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For i As Integer = 1 To 12
ddlMonth.Items.Add(New ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.Add(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMonth(Me.selectdate.Year, Me.selectdate.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(Me.selectdate.Year, Me.selectdate.Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = Me.selectdate.Month - 1
ddlYear.SelectedIndex = Me.selectdate.Year - Me.startyear
ddlDate.SelectedIndex = Me.selectdate.Day - 1
End Sub
Private Sub DateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged, ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
Dim selectedday As Integer = ddlDate.SelectedIndex + 1
ddlDate.Items.Clear()
For i As Integer = 1 To Date.DaysInMonth(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1, i).DayOfWeek), CStr(i)))
Next
If Date.DaysInMonth(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1) >= selectedday Then
ddlDate.SelectedIndex = selectedday - 1
Else
ddlDate.SelectedIndex = Date.DaysInMonth(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1) - 1
End If
Me.selectdate = New Date(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1, ddlDate.SelectedIndex + 1)
End Sub
End Class
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="DatePicker.ascx.vb" Inherits="WebApplication1.DatePicker" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="ddlMonth" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" runat="server" AutoPostBack="True"></asp:dropdownlist>
Public Class DatePicker
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
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.WebControls.DropDownList
Protected WithEvents ddlDate As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlYear As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
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.selectdate.Year, Me.startyear)
Me.stopyear = Math.Max(Me.selectdate.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.Year 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.Year 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.EventArgs) Handles MyBase.Load
If Not IsPostBack() Then Me.CreateLists()
End Sub
Private Sub CreateLists()
ddlMonth.Items.Clear()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For i As Integer = 1 To 12
ddlMonth.Items.Add(New ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.Add(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMonth(Me.selectdate.Year, Me.selectdate.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(Me.selectdate.Year, Me.selectdate.Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = Me.selectdate.Month - 1
ddlYear.SelectedIndex = Me.selectdate.Year - Me.startyear
ddlDate.SelectedIndex = Me.selectdate.Day - 1
End Sub
Private Sub DateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged, ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
Dim selectedday As Integer = ddlDate.SelectedIndex + 1
ddlDate.Items.Clear()
For i As Integer = 1 To Date.DaysInMonth(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1, i).DayOfWeek), CStr(i)))
Next
If Date.DaysInMonth(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1) >= selectedday Then
ddlDate.SelectedIndex = selectedday - 1
Else
ddlDate.SelectedIndex = Date.DaysInMonth(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1) - 1
End If
Me.selectdate = New Date(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1, ddlDate.SelectedIndex + 1)
End Sub
End Class
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="DatePicker.ascx.vb" Inherits="WebApplication1.DatePicker" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="ddlMonth" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" runat="server" AutoPostBack="True"></asp:dropdownlist>