Custom Web Control

B

brandonjack007

I am trying to write a custom date control with 3 drop downs one each
for year, month, day. SelectedDate, MaximumDate, MinimumDate are
properties that the user can set on this control. How can I ensure:
1. The year in drop down is always < MinimumDate.Year
2. Year in drop down <MaximumDate.Year
3. Save view state



using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace datecontrol
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[DefaultProperty("SelectedDate"),
ToolboxData("<{0}:YearPicker runat=server></{0}:YearPicker>")]
public class YearPicker : System.Web.UI.WebControls.WebControl
{
private DateTime minimumDate = DateTime.MinValue;
private DateTime maximumDate = DateTime.MaxValue;

[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public YearPicker()
{
month = new DropDownList();
day = new DropDownList();
year = new DropDownList();

month.Items.Add(new ListItem("January", "1"));
month.Items.Add(new ListItem("Febuary", "2"));
month.Items.Add(new ListItem("March", "3"));
month.Items.Add(new ListItem("April", "4"));
month.Items.Add(new ListItem("May", "5"));
month.Items.Add(new ListItem("June", "6"));
month.Items.Add(new ListItem("July", "7"));
month.Items.Add(new ListItem("August", "8"));
month.Items.Add(new ListItem("September", "9"));
month.Items.Add(new ListItem("October", "10"));
month.Items.Add(new ListItem("November", "11"));
month.Items.Add(new ListItem("December", "12"));
month.SelectedValue = DateTime.Now.Month.ToString();

for (int i = 0; i <= 31; i++)
{
day.Items.Add(new ListItem(Convert.ToString(i),
Convert.ToString(i)));
}
day.SelectedValue = DateTime.Now.Day.ToString();

for (int i = minimumDate.Year; i < maximumDate.Year; i++)
{
year.Items.Add(new ListItem(Convert.ToString(i),
Convert.ToString(i)));
}
year.SelectedValue = DateTime.Now.Year.ToString();
}

private DropDownList month;
private DropDownList day;
private DropDownList year;
[Bindable(true),
Category("SelectedDate"),
Description("Gets or sets the date.")]
public DateTime SelectedDate
{
get
{
DateTime selectedDate = new DateTime(00, 01, 01);
if (year.SelectedValue != "" && day.SelectedValue !=
"" && month.SelectedValue != "")
{
selectedDate = new
DateTime(Convert.ToInt32(year.SelectedValue),
Convert.ToInt32(month.SelectedValue),
Convert.ToInt32(day.SelectedValue));
}
return selectedDate;
}

set
{
DateTime selectedDate = value;
month.SelectedValue =
Convert.ToString(selectedDate.Month);
day.SelectedValue =
Convert.ToString(selectedDate.Day);
year.SelectedValue =
Convert.ToString(selectedDate.Year);
}
}

[Description("Sets the minimum date")]
[DefaultValue("")]
public DateTime MinimumDate
{
get
{
return minimumDate;
}
set
{
minimumDate = value;
}
}

[Description("Sets the maximum date")]
[DefaultValue("")]
public DateTime MaximumDate
{
get
{
return maximumDate;
}
set
{
maximumDate = value;
}
}

[Browsable(false)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string GetDay
{
get
{
return this.day.SelectedValue;
}
}

[Browsable(false)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string GetMonth
{
get
{
return this.month.SelectedValue;
}
}

[Browsable(false)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string GetYear
{
get
{
return this.year.SelectedValue;
}
}

public DateTime GetDate()
{
DateTime result = DateTime.MinValue;

try
{
int day = int.Parse(this.day.SelectedValue);
int month = int.Parse(this.month.SelectedValue);
int year = int.Parse(this.year.SelectedValue);

if (year >= minimumDate.Year && year
<=maximumDate.Year)
{
result = new DateTime(year, month, day);
}
}
catch (FormatException)
{
//
// thrown by the int.Parse
//
}
catch (OverflowException)
{
//
// thrown by the int.Parse
//
}
catch (ArgumentOutOfRangeException)
{
//
// thrown by the DateTime ctor
//
}

return result;
}

public void SetDate(DateTime value)
{
this.EnsureChildControls();

if (value == DateTime.MinValue)
{
this.day.SelectedValue = string.Empty;
this.month.SelectedValue = string.Empty;
this.year.SelectedValue = string.Empty;
}
else
{
this.day.SelectedValue = value.Day.ToString("00");
this.month.SelectedValue = value.Month.ToString("00");
this.year.SelectedValue = value.Year.ToString("00");
}
}


/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </
param>
protected override void Render(HtmlTextWriter output)
{
month.RenderControl(output);
output.Write(" ");
day.RenderControl(output);
output.Write(" ");
year.RenderControl(output);
}

protected override object SaveViewState()
{
object selectedDate = base.SaveViewState();
if (year.SelectedValue != "" && day.SelectedValue != "" &&
month.SelectedValue != "")
{
selectedDate = new
DateTime(Convert.ToInt32(year.SelectedValue),
Convert.ToInt32(month.SelectedValue),
Convert.ToInt32(day.SelectedValue));
}
return selectedDate;
}

protected override void LoadViewState(object savedState)
{
if (savedState == null) return;

DateTime state = (DateTime)savedState;
month.SelectedValue = Convert.ToString(state.Month);
day.SelectedValue = Convert.ToString(state.Day);
year.SelectedValue = Convert.ToString(state.Year);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top