|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThere are many Date controls in the Internet including many here at CodeProject. However few don't tap into .NET's BackgroundThe I have used the David Truxall control found on CodeProject as a template for the control, along with the following added features: Properties
Examples for use: <%@ Register TagPrefix="cc1"
Namespace="i386.UI" Assembly="i386.UI" %>
German with short month text, 10 years into future and two years into the past from the current year: <cc1:dropdowndatetime id="DropDownDateTime1" runat="server" YearsForward="2"
YearsBack="10" Culture="de-DE" SelectCurrentDate="False" MonthFormat="MMM">
</cc1:dropdowndatetime>
US date formatting with the <cc1:dropdowndatetime id="Dropdowndatetime4" runat="server"
dateFormat="MM/dd/yyyy" Value="11/02/2005">
</cc1:dropdowndatetime>
The
How the control worksFirstly, the private string _DateFormat=
System.Threading.Thread.CurrentThread.
CurrentCulture.DateTimeFormat.ShortDatePattern;
public string Value
{
get
{
string s = (string)ViewState["Value"];
if(s == null) return String.Empty;
else return s;
}
set
{
this.SetSelected(value);
ViewState["Value"] = value;
}
}
private void SetSelected(string StoredValue)
{
DateTimeFormatInfo formatInfo =
new DateTimeFormatInfo();
formatInfo.FullDateTimePattern = DateFormat;
DateTime dt = DateTime.ParseExact(StoredValue,
DateFormat,formatInfo);
SelectedDay = dt.Day;
SelectedMonth = dt.Month;
SelectedYear = dt.Year;
}
The control implements bool IPostBackDataHandler.LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
{
....
....
if(this.SelectedDay == -1 && this.SelectedMonth == -1 &&
this.SelectedYear == -1)
this.Value = "";
else
{
// Build a DateTime string based on the DateFormat
DateTimeFormatInfo formatInfo = new DateTimeFormatInfo();
formatInfo.FullDateTimePattern = DateFormat;
DateTime dt = new DateTime(this.SelectedYear,
this.SelectedMonth, this.SelectedDay);
this.Value = dt.ToString(DateFormat,formatInfo);
}
}
Building the child controlsWhen protected override void CreateChildControls()
{
DateTime dt = DateTime.Today;
// Use CurrentDate if Value is empty and
// SelectCurrentDate is true.
if (SelectCurrentDate && this.Value=="")
{
// Use Current Date
SelectedDay = dt.Day;
SelectedMonth = dt.Month;
SelectedYear = dt.Year;
DateTimeFormatInfo formatInfo =
new DateTimeFormatInfo();
formatInfo.FullDateTimePattern = DateFormat;
this.Value = dt.ToString(DateFormat,formatInfo);
}
// Days
DropDownList ddlboxDay = new DropDownList();
ddlboxDay.ID ="Day";
ddlboxDay.Attributes.Add("onchange",
"ChangeOptionDays('" + this.ClientID +"',
'" + DateFormat + "');");
for (int nDay = 1; nDay<32; nDay++)
ddlboxDay.Items.Add(nDay.ToString());
// So we can restore the Culture later
CultureInfo ExistingCulture =
System.Threading.Thread.CurrentThread.CurrentCulture;
// Culture - changes language of months
if (Culture!=null)
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo(Culture);
// Months
DropDownList ddlboxMonth = new DropDownList();
ddlboxMonth.ID ="Month";
ddlboxMonth.Attributes.Add("onchange",
"ChangeOptionDays('" + this.ClientID +"',
'" + DateFormat + "');");
for ( int nMonth=1; nMonth<=12; nMonth++)
{
DateTime MonthDate = new DateTime(2000,nMonth,1);
ddlboxMonth.Items.Add(new
ListItem(MonthDate.ToString(this.MonthFormat),
nMonth.ToString()));
}
// Restore
System.Threading.Thread.CurrentThread.CurrentCulture =
ExistingCulture;
// Years (Forward and Back properties)
DropDownList ddlboxYear = new DropDownList();
ddlboxYear.ID ="Year";
ddlboxYear.Attributes.Add("onchange",
"ChangeOptionDays('" + this.ClientID +"',
'" + DateFormat + "');");
ddlboxYear.ID ="Year";
if (YearsBack>=0)
{
for (int nYear = -YearsBack; nYear<=YearsForward; nYear++)
{
int ddlYear =dt.Year+nYear;
ddlboxYear.Items.Add(ddlYear.ToString());
}
}
else
{
for (int nYear = YearsForward; nYear<=-YearsBack; nYear++)
{
int ddlYear =dt.Year-nYear;
ddlboxYear.Items.Add(ddlYear.ToString());
}
}
// Select the DropDownList for the year
if (SelectedYear>0)
{
// See if the year is in the list.
if (ddlboxYear.Items.FindByValue(SelectedYear.ToString())!=null)
{
ddlboxYear.Items.FindByValue(SelectedYear.ToString()).Selected =
true;
}
}
if (SelectedMonth>0)
ddlboxMonth.Items.FindByValue(SelectedMonth.ToString()).Selected = true;
if (SelectedDay>0)
ddlboxDay.Items.FindByValue(SelectedDay.ToString()).Selected = true;
// Add Child Control
Controls.Add(ddlboxDay);
Controls.Add(ddlboxMonth);
Controls.Add(ddlboxYear);
Debugging modeIt is used to check what is happening with the control. The <cc1:dropdowndatetime DebugMode=true id="DropDownDateTime1" runat="server">
</cc1:dropdowndatetime>
Browser compatibilityThis is only a short list of browsers I've tested with. I don't have Netscape installed on my PC.
Internet Explorer 3.0 will need to use server validation. Please let me know if a certain browser doesn't function correctly. JavaScript supportFrom the debugging mode you may notice the hidden input box. This Input box can be used in JavaScript before postback is made to the server. Updates, versions and bug fixes
Future enhancements
Feel free to comment or add features yourself. If you rate this page lower please leave a comment in the forum below.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||