locked
State issues with custom WebControl RRS feed

  • Question

  • User260076833 posted

    Hi,

    I am trying to make a DatePicker conrol, with a TextBox and a Button.

    Whenever the button is pressed, it should toggle the visibility of a calendar.

    My class is designed exactly the same way as a working example I found on the net:
    https://msdn.microsoft.com/en-us/library/aa479309.aspx

    It overrides LoadControlState and SaveControlState, and creates all controls dynamically in CreateChildControls().

    However, the click handler does not toggle the calendar as expected:

    • click 1: calendar is shown
    • click 2: calendar is hidden
    • click 3: nothing happens
    • click 4: nothing happens
    • click 5: calendar is shown
    • (and so on as above)

    Below is my code.
    What am I doing wrong?

    Thank you
    Magnus

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI.WebControls;
    
    namespace MyLibDateSelector
    {
    
        public class DateSelector : WebControl
        {
            private TextBox txt;
            private Button btn;
            private System.Web.UI.WebControls.Calendar cal;
            private Boolean calendarVisible;
    
            public DateSelector():base()
            {
            }
    
            public bool ShowCalendar
            {
                get
                {
                    object o = ViewState["ShowCalendar"];
                    if (o == null)
                        return false;
                    return (bool)o;
                }
                set
                {
                    ViewState["ShowCalendar"] = value;
                    if (DesignMode)
                        calendarVisible = value;
                }
            }
    
    
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                Page.RegisterRequiresControlState(this);
                EnsureChildControls();
            }
    
            protected override void OnLoad(EventArgs e)
            {
                if (!Page.IsPostBack)
                    calendarVisible = ShowCalendar;
            }
    
    
            protected override object SaveControlState()
            {
                return (calendarVisible);
            }
    
            protected override void LoadControlState(object savedState)
            {
                if (savedState == null)
                    return;
    
                calendarVisible = (bool)savedState;
            }
    
            protected override void CreateChildControls()
            {
                Controls.Clear();
    
                txt = new TextBox();
                btn = new Button();
                cal = new System.Web.UI.WebControls.Calendar();
    
                //btn.Click += btn_Click;
                btn.Click += new EventHandler(onClick);
                cal.Visible = calendarVisible;
    
                Controls.Add(txt);
                Controls.Add(btn);
                Controls.Add(cal);
            }
    
            private void onClick(object sender, EventArgs e)
            {
                calendarVisible = !calendarVisible;
    
                ChildControlsCreated = false;
            }
    
    
        }
    }

    Thursday, July 23, 2015 12:25 PM

Answers

  • User260076833 posted

    Solved by adding INamingContainer to the inherited classes!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2015 7:45 AM