Custom DatePicker using Combobox in ASP.NET

No.of Views1019
Bookmarked0 times
Downloads 
Votes1
By  jalpesh   On  26 Jun 2010 03:06:08
Tag : ASP.NET , DateTime Controls
ASP.NET provides calendar control to select date but some time it not feasible due to the size of it. It takes more spaces. So we are required to create a new web control which is used less space and also provides date selection mechanism
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

ASP.NET provides calendar control to select date but some time it not feasible due to the size of it. It takes more spaces. So we are required to create a new web control which is used lessspace and also provides date selection mechanism.

Here are the code for that control in C#,

HTML Code

<asp:dropdownlist id="cmbDay" AutoPostBack="True"

runat="server"></asp:dropdownlist><asp:dropdownlist id="cmbMonth" AutoPostBack="True"

runat="server">&</asp:dropdownlist><asp:dropdownlist id="cmbYear" AutoPostBack="True"

runat="server"></asp:dropdownlist>

 C#

namespace Jalpesh.Control
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.VisualBasic;

public class DatePicker : System.Web.UI.UserControl
{
public System.Web.UI.WebControls.DropDownList cmbDay;
public System.Web.UI.WebControls.DropDownList cmbMonth;
public System.Web.UI.WebControls.DropDownList cmbYear;
private static string _SelectedDate;
public string _SetDate;
public static string SelectedDate
{
get{
return _SelectedDate;
}
set{
_SelectedDate=value;

}
}
public string SetDate
{
get{
return _SetDate;
}
set{
cmbYear.Items.FindByValue(cmbYear.SelectedValue).Selected=false;
cmbYear.Items.FindByValue(System.Convert.ToDateTime(value).
Year.ToString()).Selected=true;
cmbMonth.Items.FindByValue(cmbMonth.SelectedValue).Selected=false;
cmbMonth.Items.FindByValue(System.Convert.ToDateTime(value).
Month.ToString() ).Selected=true;
cmbDay.Items.FindByValue(cmbDay.SelectedValue).Selected=false;
cmbDay.Items.FindByValue(System.Convert.ToDateTime(value).Day.
ToString() ).Selected=true;
SelectedDate= cmbMonth.SelectedValue.ToString() + "/" +
cmbDay.SelectedValue.ToString() + "/" +
cmbYear.SelectedValue.ToString();

}
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
FillYear();
FillMonth();
FillDay(int.Parse(cmbMonth.SelectedValue),
int.Parse(cmbYear.SelectedValue));
SelectedDate= cmbMonth.SelectedValue.ToString() + "/"+cmbDay.SelectedValue.ToString() + "/" + cmbYear.SelectedValue.ToString();
}
}
public void FillDay(int Month,int Year)
{
cmbDay.Items.Clear();
int a;
a=Year%4;
int count;
count=0;
switch(Month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count=31;
break;
case 4:
case 6:
case 9:
case 11:
count=30;
break;
case 2:
if(a==0)
{
count=29;
}
else{
count=28;
}
break;
}

for (int i=1;i<=count;i++) { ListItem lstItem=new ListItem(i.ToString(),i.ToString()); if (i==(int.Parse(System.DateTime.Today.Day.ToString() ))) { lstItem.Selected=true; } cmbDay.Items.Add(lstItem); } } public void FillYear() { int i; cmbYear.Items.Clear(); for (i=1900;i<=3000;i++) { ListItem lstItem=new ListItem(i.ToString(),i.ToString()); if (i==(int.Parse(System.DateTime.Today.Year.ToString() ))) { lstItem.Selected=true; } cmbYear.Items.Add(lstItem); lstItem=null; } } public void FillMonth() { int i; cmbMonth.Items.Clear(); for(i=1;i<=12;i++) { ListItem lstItem=new ListItem(GetMonthName(i,false),i.ToString()); if (i==(int.Parse(System.DateTime.Today.Month.ToString() ))) { lstItem.Selected=true; } cmbMonth.Items.Add(lstItem); lstItem=null; } } static string GetMonthName(int monthNum, bool abbreviate) { if(monthNum <> 12)
throw new ArgumentOutOfRangeException("monthNum");
DateTime date = new DateTime(1,monthNum,1);
if(abbreviate)
return date.ToString("MMM");
elsereturn date.ToString("MMMM");
}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//// CODEGEN: This call is required by the ASP.NET Web Form Designer.//InitializeComponent();
base.OnInit(e);
}


private void InitializeComponent()
{
cmbDay.SelectedIndexChanged += new System.EventHandler(this.cmbDay_SelectedIndexChanged);
cmbMonth.SelectedIndexChanged += new System.EventHandler(this.cmbMonth_SelectedIndexChanged);
cmbYear.SelectedIndexChanged += new System.EventHandler(this.cmbYear_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregionprivate void cmbYear_SelectedIndexChanged(object sender, System.EventArgs e)
{
FillDay(int.Parse(cmbMonth.SelectedValue),int.Parse(cmbYear.SelectedValue));
SelectedDate= cmbMonth.SelectedValue.ToString() + "/" +cmbDay.SelectedValue.ToString() + "/" + cmbYear.SelectedValue.ToString();
}

private void cmbMonth_SelectedIndexChanged(object sender, System.EventArgs e)
{
FillDay(int.Parse(cmbMonth.SelectedValue),int.Parse(cmbYear.SelectedValue));
SelectedDate= cmbMonth.SelectedValue.ToString() + "/" +cmbDay.SelectedValue.ToString() + "/" + cmbYear.SelectedValue.ToString();
}

private void cmbDay_SelectedIndexChanged(object sender, System.EventArgs e)
{
SelectedDate= cmbMonth.SelectedValue.ToString() + "/" +cmbDay.SelectedValue.ToString() + "/" + cmbYear.SelectedValue.ToString();
}

}
}

I hope this is help and save you time.happy programming.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Expert
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
    Auto Growing TextBox or TextArea in ASP.NET
    Published Date : 08/May/2010
    In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
    Published Date : 10/Oct/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in asp.net.
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in asp.net and register events for the controls and make it work events perfectly. Last week I have read the forums many readers asking about add controls dynamically in asp.net giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top