Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Custom Web Control

Reply
Thread Tools

Custom Web Control

 
 
brandonjack007@gmail.com
Guest
Posts: n/a
 
      07-22-2007
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(DesignerSerializat ionVisibility.Hidden)]
public string GetDay
{
get
{
return this.day.SelectedValue;
}
}

[Browsable(false)]

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

[Browsable(false)]

[DesignerSerializationVisibility(DesignerSerializat ionVisibility.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);
}
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom control within a custom control J R M ASP .Net 2 06-01-2006 06:04 PM
Web user control - custom web control - java script. Leyla ASP .Net Web Controls 0 05-01-2006 10:05 PM
Capturing event from other custom control within another custom control Jonah Olsson ASP .Net 1 04-05-2005 01:39 PM
Capturing event from other custom control within another custom control Jonah Olsson ASP .Net Web Controls 2 04-05-2005 12:56 PM
ControlDesigner not invoked on custom control when control is rendered within another custom control Matt Sokol ASP .Net Building Controls 2 08-07-2003 07:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57