DotNet Tutorials

Server Intellect

 DropDownList binding date using ASP.NET 2.0 and VB.NET

This tutorial will show you how to bind DropDownList with date in ASP.NET 2.0 and VB.NET. First, you will need to import the System.Collections namespace.
Imports System.Collections

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

Then create a method  to determine the leap year

Private Function CheckLeap(ByVal year As Integer) As Boolean
If (year Mod 4 = 0) And (year Mod 100 <> 0) Or (year Mod 400 = 0) Then
Return True
Else
Return False
End If
End Function

And a method for binding every month day

Private Function BindDays(ByVal year As Integer, ByVal month As Integer)
Dim i As Integer
Dim AlDay As New ArrayList
Select Case month
Case 1, 3, 5, 7, 8, 10, 12
For i = 1 To 31
AlDay.Add(i)
Next
Case 2
If CheckLeap(year) Then
For i = 1 To 29
AlDay.Add(i)
Next
Else
For i = 1 To 28
AlDay.Add(i)
Next
End If
Case 4, 6, 9, 11
For i = 1 To 30
AlDay.Add(i)
Next
End Select
DropDownList3.DataSource = AlDay
DropDownList3.DataBind()
End Function

DropDownList1_SelectedIndexChanged (select year)

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Year = Int32.Parse(DropDownList1.SelectedValue)
Month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(Year, Month)
End Sub

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

DropDownList2_SelectedIndexChanged (select month)

Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
Year = Int32.Parse(DropDownList1.SelectedValue)
Month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(Year, Month)
End Sub

Code of Page_Load

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tnow As DateTime
tnow = DateTime.Now
Dim AlYear As New ArrayList
Dim i As Integer
For i = 2000 To 2010
AlYear.Add(i)
Next
Dim AlMonth As New ArrayList
For i = 1 To 12
AlMonth.Add(i)
Next
If IsPostBack = False Then
DropDownList1.DataSource = AlYear
DropDownList1.DataBind()
DropDownList1.SelectedValue = tnow.Year.ToString()
DropDownList2.DataSource = AlMonth
DropDownList2.DataBind()
DropDownList2.SelectedValue = tnow.Month.ToString()
Year = Int32.Parse(DropDownList1.SelectedValue)
Month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(Year, Month)
DropDownList3.SelectedValue = tnow.Day.ToString()
End If
Label1.Text = "You select date:" + DropDownList1.SelectedValue + "year" + DropDownList2.SelectedValue + "month" + DropDownList3.SelectedValue
End Sub

The front DropDownList.aspx page looks something like this:

<form id="form1" runat="server">
<div>
<fieldset>
<legend>DropDownListBindingDate</legend>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True">
</asp:DropDownList><br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />
 </fieldset>
</div>
</form>

The flow for the code behind page is as follows.

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

Partial Class DropDownListBindingDate
Inherits System.Web.UI.Page
Dim Year, Month As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tnow As DateTime
tnow = DateTime.Now
Dim AlYear As New ArrayList
Dim i As Integer
For i = 2000 To 2010
AlYear.Add(i)
Next
Dim AlMonth As New ArrayList
For i = 1 To 12
AlMonth.Add(i)
Next

If IsPostBack = False Then
DropDownList1.DataSource = AlYear
DropDownList1.DataBind()
DropDownList1.SelectedValue = tnow.Year.ToString()
DropDownList2.DataSource = AlMonth
DropDownList2.DataBind()
DropDownList2.SelectedValue = tnow.Month.ToString()
Year = Int32.Parse(DropDownList1.SelectedValue)
Month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(Year, Month)
DropDownList3.SelectedValue = tnow.Day.ToString()
End If
Label1.Text = "You select date:" + DropDownList1.SelectedValue + "year" + DropDownList2.SelectedValue + "month" + DropDownList3.SelectedValue
End Sub

'judge leap year
Private Function CheckLeap(ByVal year As Integer) As Boolean
If (year Mod 4 = 0) And (year Mod 100 <> 0) Or (year Mod 400 = 0) Then
Return True
Else
Return False
End If
End Function

'binding every month day
Private Function BindDays(ByVal year As Integer, ByVal month As Integer)
Dim i As Integer
Dim AlDay As New ArrayList
Select Case month
Case 1, 3, 5, 7, 8, 10, 12
For i = 1 To 31
AlDay.Add(i)
Next
Case 2
If CheckLeap(year) Then
For i = 1 To 29
AlDay.Add(i)
Next
Else
For i = 1 To 28
AlDay.Add(i)
Next
End If
Case 4, 6, 9, 11
For i = 1 To 30
AlDay.Add(i)
Next
End Select
DropDownList3.DataSource = AlDay
DropDownList3.DataBind()
End Function

'select year
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Year = Int32.Parse(DropDownList1.SelectedValue)
Month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(Year, Month)
End Sub

'select month
Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
Year = Int32.Parse(DropDownList1.SelectedValue)
Month = Int32.Parse(DropDownList2.SelectedValue)
BindDays(Year, Month)
End Sub
End Class



Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect