Custom Web Control

Discussion in 'ASP .Net Web Controls' started by brandonjack007@gmail.com, Jul 22, 2007.

  1. Guest

    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);
    }
    }
    }
    , Jul 22, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonah Olsson
    Replies:
    1
    Views:
    573
    Scott Allen
    Apr 5, 2005
  2. J R M
    Replies:
    2
    Views:
    430
    Charlie@CBFC
    Jun 1, 2006
  3. Matt Sokol
    Replies:
    2
    Views:
    379
    Theo Tillotson
    Aug 7, 2003
  4. Jonah Olsson
    Replies:
    2
    Views:
    201
    Jonah Olsson
    Apr 5, 2005
  5. Leyla

    Web user control - custom web control - java script.

    Leyla, May 1, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    122
    Leyla
    May 1, 2006
Loading...

Share This Page