Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - DropDownList binding date using ASP.NET 2.0 and VB.NET
DropDownList binding date using ASP.NET 2.0 and VB.NET


ASP.NET Controls Tutorial

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

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

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

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

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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

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

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

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>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

The flow for the code behind page is as follows.

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

Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!

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

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks