// Default.aspx //
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Year:<asp:DropDownList ID="ddlYear" runat="server" onchange = "PopulateDays()" />
Month:<asp:DropDownList ID="ddlMonth" runat="server" onchange = "PopulateDays()" />
Day:<asp:DropDownList ID="ddlDay" runat="server" />
<br />
<asp:CustomValidator ID="Validator" runat="server" ErrorMessage="* Required"
ClientValidationFunction = "Validate" />
</div>
</form>
</body>
</html>
// Default.aspx.cs //
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (this.SelectedDate == DateTime.MinValue)
{
this.PopulateYear();
this.PopulateMonth();
this.PopulateDay();
}
}
else
{
if (Request.Form[ddlDay.UniqueID] != null)
{
this.PopulateDay();
ddlDay.ClearSelection();
ddlDay.Items.FindByValue(Request.Form[ddlDay.UniqueID]).Selected = true;
}
}
}
private int Day
{
get
{
if (Request.Form[ddlDay.UniqueID] != null)
{
return int.Parse(Request.Form[ddlDay.UniqueID]);
}
else
{
return int.Parse(ddlDay.SelectedItem.Value);
}
}
set
{
this.PopulateDay();
ddlDay.ClearSelection();
ddlDay.Items.FindByValue(value.ToString()).Selected = true;
}
}
private int Month
{
get
{
return int.Parse(ddlMonth.SelectedItem.Value);
}
set
{
this.PopulateMonth();
ddlMonth.ClearSelection();
ddlMonth.Items.FindByValue(value.ToString()).Selected = true;
}
}
private int Year
{
get
{
return int.Parse(ddlYear.SelectedItem.Value);
}
set
{
this.PopulateYear();
ddlYear.ClearSelection();
ddlYear.Items.FindByValue(value.ToString()).Selected = true;
}
}
public DateTime SelectedDate
{
get
{
try
{
return DateTime.Parse(this.Month + "/" + this.Day + "/" + this.Year);
}
catch
{
return DateTime.MinValue;
}
}
set
{
if (!value.Equals(DateTime.MinValue))
{
this.Year = value.Year;
this.Month = value.Month;
this.Day = value.Day;
}
}
}
private void PopulateDay()
{
ddlDay.Items.Clear();
ListItem lt = new ListItem();
lt.Text = "DD";
lt.Value = "0";
ddlDay.Items.Add(lt);
int days = DateTime.DaysInMonth(this.Year, this.Month);
for (int i = 1; i <= days; i++)
{
lt = new ListItem();
lt.Text = i.ToString();
lt.Value = i.ToString();
ddlDay.Items.Add(lt);
}
ddlDay.Items.FindByValue(DateTime.Now.Day.ToString()).Selected = true;
}
private void PopulateMonth()
{
ddlMonth.Items.Clear();
ListItem lt = new ListItem();
lt.Text = "MM";
lt.Value = "0";
ddlMonth.Items.Add(lt);
for (int i = 1; i <= 12; i++)
{
lt = new ListItem();
lt.Text = Convert.ToDateTime(i.ToString() + "/1/1900").ToString("MMMM");
lt.Value = i.ToString();
ddlMonth.Items.Add(lt);
}
ddlMonth.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
}
private void PopulateYear()
{
ddlYear.Items.Clear();
ListItem lt = new ListItem();
lt.Text = "YYYY";
lt.Value = "0";
ddlYear.Items.Add(lt);
for (int i = DateTime.Now.Year; i >= 1950; i--)
{
lt = new ListItem();
lt.Text = i.ToString();
lt.Value = i.ToString();
ddlYear.Items.Add(lt);
}
ddlYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Year:<asp:DropDownList ID="ddlYear" runat="server" onchange = "PopulateDays()" />
Month:<asp:DropDownList ID="ddlMonth" runat="server" onchange = "PopulateDays()" />
Day:<asp:DropDownList ID="ddlDay" runat="server" />
<br />
<asp:CustomValidator ID="Validator" runat="server" ErrorMessage="* Required"
ClientValidationFunction = "Validate" />
</div>
</form>
</body>
</html>
// Default.aspx.cs //
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (this.SelectedDate == DateTime.MinValue)
{
this.PopulateYear();
this.PopulateMonth();
this.PopulateDay();
}
}
else
{
if (Request.Form[ddlDay.UniqueID] != null)
{
this.PopulateDay();
ddlDay.ClearSelection();
ddlDay.Items.FindByValue(Request.Form[ddlDay.UniqueID]).Selected = true;
}
}
}
private int Day
{
get
{
if (Request.Form[ddlDay.UniqueID] != null)
{
return int.Parse(Request.Form[ddlDay.UniqueID]);
}
else
{
return int.Parse(ddlDay.SelectedItem.Value);
}
}
set
{
this.PopulateDay();
ddlDay.ClearSelection();
ddlDay.Items.FindByValue(value.ToString()).Selected = true;
}
}
private int Month
{
get
{
return int.Parse(ddlMonth.SelectedItem.Value);
}
set
{
this.PopulateMonth();
ddlMonth.ClearSelection();
ddlMonth.Items.FindByValue(value.ToString()).Selected = true;
}
}
private int Year
{
get
{
return int.Parse(ddlYear.SelectedItem.Value);
}
set
{
this.PopulateYear();
ddlYear.ClearSelection();
ddlYear.Items.FindByValue(value.ToString()).Selected = true;
}
}
public DateTime SelectedDate
{
get
{
try
{
return DateTime.Parse(this.Month + "/" + this.Day + "/" + this.Year);
}
catch
{
return DateTime.MinValue;
}
}
set
{
if (!value.Equals(DateTime.MinValue))
{
this.Year = value.Year;
this.Month = value.Month;
this.Day = value.Day;
}
}
}
private void PopulateDay()
{
ddlDay.Items.Clear();
ListItem lt = new ListItem();
lt.Text = "DD";
lt.Value = "0";
ddlDay.Items.Add(lt);
int days = DateTime.DaysInMonth(this.Year, this.Month);
for (int i = 1; i <= days; i++)
{
lt = new ListItem();
lt.Text = i.ToString();
lt.Value = i.ToString();
ddlDay.Items.Add(lt);
}
ddlDay.Items.FindByValue(DateTime.Now.Day.ToString()).Selected = true;
}
private void PopulateMonth()
{
ddlMonth.Items.Clear();
ListItem lt = new ListItem();
lt.Text = "MM";
lt.Value = "0";
ddlMonth.Items.Add(lt);
for (int i = 1; i <= 12; i++)
{
lt = new ListItem();
lt.Text = Convert.ToDateTime(i.ToString() + "/1/1900").ToString("MMMM");
lt.Value = i.ToString();
ddlMonth.Items.Add(lt);
}
ddlMonth.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
}
private void PopulateYear()
{
ddlYear.Items.Clear();
ListItem lt = new ListItem();
lt.Text = "YYYY";
lt.Value = "0";
ddlYear.Items.Add(lt);
for (int i = DateTime.Now.Year; i >= 1950; i--)
{
lt = new ListItem();
lt.Text = i.ToString();
lt.Value = i.ToString();
ddlYear.Items.Add(lt);
}
ddlYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
}
}
OUTPUT
No comments:
Post a Comment