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
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>
Server Side Code in 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");
else
return 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);
}
#endregion
private 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();
}
}
}
Saturday, August 26, 2006
0 comments:
Post a Comment
Your feedback is very important to me. Please provide your feedback via putting comments.