Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 20th, 2006, 07:55 AM
Authorized User
 
Join Date: Jan 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Maintaining the viewstate in a custom web control

Hi there,

I am trying to make a simple month/year web user control. It consists of 2 drop down boxes, one for the month and one for the year. However, I can't get it to maintain the viewstate. I've never made a custom control before so I'm not entirely sure if I'm doing it right. Heres my code:

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

namespace Smartbeyond.WebControls
{
    /// <summary>
    /// Summary description for WebCustomControl1.
    /// </summary>
    [DefaultProperty("SelectedDate"),
        ToolboxData("<{0}:MonthYearPicker runat=server></{0}:MonthYearPicker>")]
    public class MonthYearPicker : System.Web.UI.WebControls.WebControl
    {

        [Bindable(true),
        Category("Appearance"),
        DefaultValue("")]
        public MonthYearPicker()
        {
            month = new DropDownList();
            year = new DropDownList();
            month.Items.Add(new ListItem("Select Month",""));
            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"));

            year.Items.Add(new ListItem("Select Year",""));
            for (int i = 1980; i <2010; i++)
            {
                year.Items.Add(new ListItem(Convert.ToString(i),Convert.ToString(i)));
            }

        }
        private DropDownList month;
        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 != "" && month.SelectedValue != "") 
                {
                    selectedDate = new DateTime(Convert.ToInt32(year.SelectedValue), Convert.ToInt32(month.SelectedValue),01);
                }
                return selectedDate;
            }

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

        /// <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(" ");
            year.RenderControl(output);
        }

        protected override object SaveViewState()
        {

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

            DateTime state = (DateTime) savedState;
            month.SelectedValue = Convert.ToString(state.Month);
            year.SelectedValue = Convert.ToString(state.Year);
        }
    }
}
Can anyone help?

Thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
Web Service, Custom Control, Custom Return Type robzyc ASP.NET 2.0 Basics 6 June 10th, 2008 08:03 AM
Maintaining custom colors when exporting to Excel jocchino Reporting Services 0 May 28th, 2007 05:09 PM
Web Custom Control Events junshi ASP.NET 1.0 and 1.1 Professional 1 May 3rd, 2007 01:19 AM
Web Custom Control prakashpatil_1978 ASP.NET 1.x and 2.0 Application Design 11 August 30th, 2004 07:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.