DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 DropDownList binding date using ASP.NET 2.0 and C#

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

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Then create a method  to determine the leap year

private bool CheckLeap(int year)
{
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
return true;
else
return false;
}

And a method  for  binding every month day

private void BindDays(int year, int month)
{
int i;
ArrayList AlDay = new ArrayList();

switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
for (i = 1; i <= 31; i++)
AlDay.Add(i);
break;
case 2:
if (CheckLeap(year))
{
for (i = 1; i <= 29; i++)
AlDay.Add(i);
}
else
{
for (i = 1; i <= 28; i++)
AlDay.Add(i);
}
break;
case 4:
case 6:
case 9:
case 11:
for (i = 1; i <= 30; i++)
AlDay.Add(i);
break;
}
DropDownList3.DataSource = AlDay;
DropDownList3.DataBind();
}

DropDownList1_SelectedIndexChanged (select year)

public void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
year = Int32.Parse(DropDownList1.SelectedValue);
month = Int32.Parse(DropDownList2.SelectedValue);
BindDays(year, month);
}

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

DropDownList2_SelectedIndexChanged (select month)

public void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
{
year = Int32.Parse(DropDownList1.SelectedValue);
month = Int32.Parse(DropDownList2.SelectedValue);
BindDays(year, month);
}

Code of Page_Load

protected void Page_Load(object sender, EventArgs e)
{
DateTime tnow = DateTime.Now;
ArrayList AlYear = new ArrayList();
int i;
for (i = 2002; i <= 2010; i++)
AlYear.Add(i);
ArrayList AlMonth = new ArrayList();
for (i = 1; i <= 12; i++)
AlMonth.Add(i);
if (!this.IsPostBack)
{
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();
}
Label1.Text = "You select date:" + DropDownList1.SelectedValue + "year" + DropDownList2.SelectedValue + "month" + DropDownList3.SelectedValue;
}

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.

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.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DropDownList : System.Web.UI.Page
{
int year, month;
protected void Page_Load(object sender, EventArgs e)
{
DateTime tnow = DateTime.Now;
ArrayList AlYear = new ArrayList();
int i;
for (i = 2002; i <= 2010; i++)
AlYear.Add(i);
ArrayList AlMonth = new ArrayList();
for (i = 1; i <= 12; i++)
AlMonth.Add(i);
if (!this.IsPostBack)
{
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();
}
Label1.Text = "You select date:" + DropDownList1.SelectedValue + "year" + DropDownList2.SelectedValue + "month" + DropDownList3.SelectedValue;
}

//judge leap year
private bool CheckLeap(int year)
{
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
return true;
else return false;
}

//binding every month day
private void BindDays(int year, int month)
{
int i;
ArrayList AlDay = new ArrayList();

switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
for (i = 1; i <= 31; i++)
AlDay.Add(i);
break;
case 2:
if (CheckLeap(year))
{
for (i = 1; i <= 29; i++)
AlDay.Add(i);
}
else
{
for (i = 1; i <= 28; i++)
AlDay.Add(i);
}
break;
case 4:
case 6:
case 9:
case 11:
for (i = 1; i <= 30; i++)
AlDay.Add(i);
break;
}
DropDownList3.DataSource = AlDay;
DropDownList3.DataBind();
}
//select year
public void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
year = Int32.Parse(DropDownList1.SelectedValue);
month = Int32.Parse(DropDownList2.SelectedValue);
BindDays(year, month);
}

//select month
public void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
{
year = Int32.Parse(DropDownList1.SelectedValue);
month = Int32.Parse(DropDownList2.SelectedValue);
BindDays(year, month);
}
}



Looking for the VB.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!