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 C#
DropDownList binding date using ASP.NET 2.0 and C#


ASP.NET Controls Tutorial

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;

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;
}

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.

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);
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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;
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

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>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The flow for the code behind page is as follows.

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);
}
}

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

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks