locked
Can't set value in Properties Window of Composite Control RRS feed

  • Question

  • User514230873 posted

    Hi!


     

    I've created an Composite Control with a CheckBox and a RadDatePicker (telerik controls). I've exposed the Checked property from the Checkbox through a public property. It happens that when using my checked property in the properties window in design time in a ASP.Net project my set value disappears on blur, but it is added in my aspx code page as an attribute. I've tried to replace the CheckBox/Checked with a Label/Text property and the same happens.

    Here is my code:

    namespace CheckBoxRadDatePicker
    {
    
        [ToolboxData("<{0}:CheckBoxRadDatePicker runat=\"server\"></{0}:CheckBoxRadDatePicker>")]
        public class CheckBoxRadDatePicker : CompositeControl
        {
            private CheckBox _chk;
            private RadDatePicker _rdp;
    
         
            // EXPOSED PROPERTIES
    
    
            [BrowsableAttribute(true)]
            public DateTime? PickedDate
            {
                get { return Rdp.SelectedDate; }
                set { Rdp.SelectedDate = value; }
            }
    
            [BrowsableAttribute(true)]
            public bool Checked
            {
                get { return Chk.Checked; }
                set { Chk.Checked = value; }
            }
    
    
    
            // ATRIBUTES ENCAPSULATION
    
            protected CheckBox Chk
            {
                get { EnsureChildControls(); return _chk; }
                set { EnsureChildControls(); _chk = value; }
            }
    
            protected RadDatePicker Rdp
            {
                get { EnsureChildControls(); return _rdp; }
                set { EnsureChildControls(); _rdp = value; }
            }
    
    
    
    
            // METHODS
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
    
                // CheckBox
                Chk = new CheckBox();
                Chk.CheckedChanged += new EventHandler(Chk_CheckedChanged);
                Chk.AutoPostBack = true;
    
                // RadDateInput
                Rdp = new RadDatePicker();
                //Rdp.SelectedDate = DateTime.Now;
    
                // Controls add
                Controls.Add(Chk);
                Controls.Add(Rdp);
            }
    
    
            void Chk_CheckedChanged(object sender, EventArgs e)
            {
                Rdp.Enabled = Checked;
                OnCheckedChanged(sender, e);
            }
    
    
        }
    }
    


    When using in the projec, I simplye have:

    <%@ Register Assembly="CheckBoxRadDatePicker" Namespace="CheckBoxRadDatePicker" TagPrefix="cc1" %>

    ...

    <cc1:CheckBoxRadDatePicker ID="CheckBoxRadDatePicker1" runat="server" />



    After changing the value on the Properties Window, the ASPX turns:

    <cc1:CheckBoxRadDatePicker ID="CheckBoxRadDatePicker1" runat="server" cheked="true" />

    but the properties window erases the value on blur.


    :|


    Can anyone tell-me what am I doing wrong?


    Regards,

    Rodrigo S.

    Tuesday, August 4, 2009 10:19 AM

Answers

  • User-2106054853 posted

     Hi,

    If you want to set default value you can try this:

     

     

    private CheckBox _chk = new CheckBox() { Checked = true };

     

    The most common usage of ViewState is, when you change the property value in a postback event handler, the changed value cannot be maintained if you simply use field to store data. Your code works fine because the same value (what you defined in aspx) is assigned to the property on every postback and you don't change it on any postback event handler. (Button's OnClick, etc)

    For more details about ViewState, please refer to:

     

    http://msdn.microsoft.com/en-us/library/ms972976.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 7, 2009 4:12 AM

All replies

  • User-2106054853 posted

    Hi,

    Please do not initialize CheckBox in the CreateChildControls method. The designer will call get accessor of Checked property after the call to the CreateChildControls method. If you initialize the CheckBox actually the value designer got is from a new object, namely the default value false.

     

     You can initialize it in this way instead (remove Chk = new CheckBox();  ):

     

    private CheckBox _chk=new CheckBox();

     

    Wednesday, August 5, 2009 11:24 PM
  • User514230873 posted

    Hi Allen,


    Thank you very much. I've understood the CreateChildControls initialization implications. But I still have some problems. For the same reason you've clarified, I can't set the CheckBox.Checked property on CreateChildControls. I want to set the default initial state of Checked as true and I can't make it with:

    • Visible real initial control default value of Checked on Properties Window on Design Time (for instance, true value);
    • Overridable by design time the control default Checked value, since the component design time attribute value is read previously to my onInit/OnPreInit component value initialization;

    Hope I made myself clear enough. Can anyone give me some lights?


    Regards,

    Rodrigo S.

    Thursday, August 6, 2009 5:44 AM
  • User514230873 posted

    Although I took out from CreateChildControls the child controls initialization, my public property "bounded" to my child control property, which was initialized on CreateChildControls. This way I still can't get to properly set my property value on design time. The solution is, as on related posts (this and this), to don't bound the property directly to the respectively inner control and use the Viewstate["myPropertyX"].

    My simple and humble question is: why use Viewstate and not a new private attribute on my composite server control, like a bool for Checkbox.Checked property or a String for Textbox.Text property? I'm using a private property and until now everything seems ok, even accros postbacks.

    Some sample code:

            private bool _isChecked = true;
    
            public bool Checked
            {
                get { return _isChecked; }
                set { _isChecked = value; }
            }
    
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
    
                // CheckBox
                _chk.CheckedChanged += new EventHandler(Chk_CheckedChanged);
                _chk.AutoPostBack = true;
                _chk.Checked = Checked;
    
                // RadDateInput
                _rdp.Enabled = _chk.Checked;
                _rdp.SelectedDate = PickedDate;
    
                // Controls add
                Controls.Add(_chk);
                Controls.Add(_rdp);
    
            }


    Regards,

    Rodrigo S.

    Thursday, August 6, 2009 10:37 AM
  • User-2106054853 posted

     Hi,

    If you want to set default value you can try this:

     

     

    private CheckBox _chk = new CheckBox() { Checked = true };

     

    The most common usage of ViewState is, when you change the property value in a postback event handler, the changed value cannot be maintained if you simply use field to store data. Your code works fine because the same value (what you defined in aspx) is assigned to the property on every postback and you don't change it on any postback event handler. (Button's OnClick, etc)

    For more details about ViewState, please refer to:

     

    http://msdn.microsoft.com/en-us/library/ms972976.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 7, 2009 4:12 AM
  • User514230873 posted

    Great help, Allen.


    Thanks :-)

    Monday, August 24, 2009 4:40 AM