CreateChildControls() always has default values for properties

Discussion in 'ASP .Net Building Controls' started by Christophe Peillet, Jan 14, 2006.

  1. I am trying to develop a composite control that renders in a specific layout,
    depending on the value of the custom LabelPosition property. The 'layout' of
    the rendered control will change depending on whether the value of
    LabelPosition is set to Left, Right, Top, or Bottom, as can be seen in the
    CreateChildControls() method.

    This particular class (included below) actually acts as a base class for
    many other controls (such as a textbox, checkbox, datepicker, etc.), so not
    all of the logic is implemented here. Because I don't know what objects will
    need to be added by the child controls (textbox, etc.), I have inserted a
    PlaceHolder control in the appropriate section of the table, and child
    controls insert their own objects here. (This allows me to keep the code for
    rendering the table in the parent class, making code maintenance much easier
    for me if changes are ever needed.)

    The problem I'm having is that no matter which value I assign to the
    LabelPosition property, it always returns the default value inside the
    CreateChildControls() method. I explicitly set it to Top, and this appears
    in the Html code behind, but it is always the default (Left) when I set and
    reach a breakpoint in CreateChildControls().

    I'm sure my problem is that I have not properly understood the composite
    control page model, and am setting default values in the wrong place (look at
    the SetDefaultValues() method reference in the constructor), or am creating
    the table in the wrong place (based on the 'switch' in
    CreateChildControls()). Or, the problem could be something with ViewState,
    that I am not properly storing/refreshing the value of the LabelPosition
    property.

    If anyone can help me understand why this value is not being restored, and
    always reads as default, I would enormously appreciate it, since I've spend
    many hours on this to no avail, including a lot of time on Google.

    The base class is included below. If you need the Designer class (which
    contains nothing exotic) or a child class (that implements this base class),
    let me know any I'll post it.

    PS: Because of message length restrictions, I have removed some uneccessary
    properties from this class.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Diagnostics;
    using System.Drawing.Design;
    using Telerik.WebControls;

    namespace CompanyName.EEE.Web.UI.FormControls
    {
    /// <summary>
    /// The BaseClass used in all of CompanyName's form custom form controls
    which require a descriptive label.
    /// </summary>
    // [Designer(typeof(SupportFormLabelledControlDesigner))]
    [DefaultProperty("LabelText")]
    [ToolboxData("<{0}:SupportFormLabelledControl
    runat=\"server\"></{0}:SupportFormLabelledControl>")]
    public abstract class SupportFormLabelledControl : SupportFormControl,
    INamingContainer, ICallback, IRequired
    {

    #region Protected Fields

    // Any third-party or external controls that you wish to add to the
    this
    // control, such as buttons, labels, etc., should be declared here.
    By
    // declaring them 'protected' they will be visible in any control that
    // inherits from SupportFormLabelledControl

    /// <summary>The Table used to hold child controls on the
    form</summary>
    protected Table m_tbl;
    /// <summary>The CallbackLabel rendered with the control</summary>
    protected CallbackLabel m_lbl;
    /// <summary>The Warning Icon rendered with the control when <see
    cref="P:Required"/> is set to <c>true</c></summary>
    protected IconPopupControl m_icn;
    /// <summary>The content PlaceHolder used to insert any child
    controls that need to be added (Textboxes, Checkboxes, etc).</summary>
    protected PlaceHolder m_plc;

    #endregion

    #region Constructor

    // Default values for properties should ONLY be defined in the the
    // class constructor. If you set properties elsewhere, such as in the
    // OnLoad event, you will make the control insensitive to external,
    // tag-level settings.

    /// <summary>
    /// Initializes a new instance of the <see
    cref="SupportFormLabelledControl"/> class.
    /// </summary>
    public SupportFormLabelledControl()
    {
    // Set control default values
    this.SetDefaultValues();
    }

    #endregion

    #region Abstract Members

    /// <summary>
    /// If the control's Required property is set to True, this method
    can be
    /// used to validate whether the Required conditions are met or not.
    /// </summary>
    /// <returns>
    /// <c>true</c> if the Required conditions are met, <c>false</c> if
    they are not.
    /// </returns>
    public abstract bool ValidateControl();

    #endregion

    #region Protected Methods

    /// <summary>
    /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
    /// </summary>
    /// <param name="e">An <see cref="T:System.EventArgs"></see> object
    that contains the event data.</param>
    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    }

    /// <summary>
    /// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
    /// </summary>
    /// <param name="e">The <see cref="T:System.EventArgs"></see> object
    that contains the event data.</param>
    protected override void OnLoad(EventArgs e)
    {
    // This method captures the controls Load event (declared and
    bound in the
    // class constructor). RunTime rendering takes place in this
    method (as
    // opposed to DesignMode rendering, which takes place elsewhere).

    // Call base Load method
    base.OnLoad(e);

    EnsureChildControls();
    }

    /// <summary>
    /// Called by the ASP.NET page framework to notify server controls
    that use
    /// composition-based implementation to create any child controls
    they contain
    /// in preparation for posting back or rendering.
    /// </summary>
    protected override void CreateChildControls()
    {
    // Clear the control collection
    Controls.Clear();

    // Any dependant controls used in this custom control must
    // be added to the control 'hierarchy'. If they are not added
    // to the control collection, they will not be visible to other
    // controls on the page.

    // Instantiate any dependant controls
    m_tbl = new Table();
    m_lbl = new CallbackLabel();
    m_icn = new IconPopupControl();
    m_plc = new PlaceHolder();

    // Create table object and format it through relevant method
    m_tbl =
    SharedFunctions.CreateLabelledControlTable(this.LabelPosition);

    // Add table to the control collection
    Controls.Add(m_tbl);

    // Add controls to the table control collection
    switch (this.LabelPosition)
    {
    case Position.Left:
    m_tbl.Rows[0].Cells[0].Controls.Add(m_lbl);
    m_tbl.Rows[0].Cells[1].Controls.Add(m_plc);
    m_tbl.Rows[0].Cells[1].Controls.Add(m_icn);
    // Set relevant design properties
    m_tbl.Rows[0].Cells[0].Width = new
    Unit(this.LabelWidth.ToString());
    break;
    case Position.Top:
    m_tbl.Rows[0].Cells[0].Controls.Add(m_lbl);
    m_tbl.Rows[1].Cells[0].Controls.Add(m_plc);
    m_tbl.Rows[1].Cells[0].Controls.Add(m_icn);
    // Set relevant design properties
    m_tbl.Rows[0].Cells[0].Width = new
    Unit(this.LabelWidth.ToString());
    break;
    case Position.Right:
    m_tbl.Rows[0].Cells[0].Controls.Add(m_plc);
    m_tbl.Rows[0].Cells[0].Controls.Add(m_icn);
    m_tbl.Rows[0].Cells[1].Controls.Add(m_lbl);
    // Set relevant design properties
    m_tbl.Rows[0].Cells[1].Width = new
    Unit(this.LabelWidth.ToString());
    break;
    case Position.Bottom:
    m_tbl.Rows[0].Cells[0].Controls.Add(m_plc);
    m_tbl.Rows[0].Cells[0].Controls.Add(m_icn);
    m_tbl.Rows[1].Cells[0].Controls.Add(m_lbl);
    // Set relevant design properties
    m_tbl.Rows[1].Cells[0].Width = new
    Unit(this.LabelWidth.ToString());
    break;
    default:
    Debug.Assert(false);
    break;
    }

    // Call base method
    base.CreateChildControls();
    }

    protected override void OnPreRender(EventArgs e)
    {
    // Call base method
    base.OnPreRender(e);

    // Associate dependent control properties with this control's
    properties
    m_lbl.CssClass = this.LabelCssClass;
    m_lbl.Text = this.LabelText;
    m_lbl.Visible = this.LabelVisible;
    m_lbl.RadControlsDir = this.ScriptsPath;
    m_lbl.CallbackEnabled = this.CallbackEnabled;
    m_lbl.DisableAtCallback = this.DisableAtCallback;
    m_lbl.Enabled = this.Enabled;
    m_icn.WarningImageUrl = this.WarningImageUrl;
    m_icn.ImageAlign = this.ImageAlign;
    m_icn.EmptyImageUrl = this.EmptyImageUrl;
    m_icn.MessageStyle = this.MessageStyle;
    m_icn.PopupText = this.PopupText;
    m_icn.PopupTextResourceKey = this.PopupTextResourceKey;
    m_icn.PopupTitle = this.PopupTitle;
    m_icn.PopupTitleResourceKey = this.PopupTitleResourceKey;
    m_icn.LinkUrl = this.LinkUrl;
    m_icn.Enabled = this.Enabled;
    m_icn.CssClass = this.WarningIconCssStyle;

    // Enable or disable warning icon as appropriate
    m_icn.Visible = this.Required ? true : false;
    }

    #endregion

    #region Private/Internal Methods

    /// <summary>
    /// Sets the control's default values.
    /// </summary>
    private void SetDefaultValues()
    {
    // Set default properties
    this.LabelPosition = Position.Left;
    this.LabelText = string.Empty;
    this.LabelCssClass = SharedConstants.SupportForm_LabelStyle;
    this.LabelVisible = true;
    this.LabelWidth = new
    Unit(SharedConstants.SupportForm_DefaultLabelWidth);
    this.Required = false;
    this.CallbackEnabled =
    SharedConstants.SupportForm_CallbackEnabledByDefault;
    this.DisableAtCallback =
    SharedConstants.SupportForm_DisableAtCallback;
    this.ScriptsPath = SharedConstants.SupportForm_DefaultScriptsPath;
    this.AutoPostback = false;
    this.ImageAlign = ImageAlign.AbsMiddle;
    this.LinkUrl = string.Empty;
    this.MessageStyle = MessageStyle.NoMessage;
    this.PopupText = string.Empty;
    this.PopupTextResourceKey = string.Empty;
    this.PopupTitle = string.Empty;
    this.PopupTitleResourceKey = string.Empty;
    this.WarningIconVisible = false;
    this.WarningIconCssStyle =
    SharedConstants.SupportForm_WarningIconStyle;
    this.WarningImageUrl =
    SharedConstants.SupportForm_IconPopupControlDefaultWarningImageUrl;
    this.EmptyImageUrl =
    SharedConstants.SupportForm_IconPopupControlDefaultEmptyImageUrl;
    }

    /// <summary>
    /// Sets the design mode values.
    /// </summary>
    internal virtual void SetDesignModeValues()
    {
    // Ensure child controls are created
    EnsureChildControls();

    // Associate dependent control properties with this control's
    properties
    m_lbl.CssClass = this.LabelCssClass;
    m_lbl.Text = this.LabelText == string.Empty ? "[LabelText]" :
    this.LabelText;
    m_lbl.Visible = this.LabelVisible;
    m_lbl.RadControlsDir = this.ScriptsPath;
    m_lbl.CallbackEnabled = this.CallbackEnabled;
    m_lbl.DisableAtCallback = this.DisableAtCallback;
    m_lbl.Enabled = this.Enabled;
    m_icn.WarningImageUrl = this.WarningImageUrl;
    m_icn.ImageAlign = this.ImageAlign;
    m_icn.EmptyImageUrl = this.EmptyImageUrl;
    m_icn.MessageStyle = this.MessageStyle;
    m_icn.PopupText = this.PopupText;
    m_icn.PopupTextResourceKey = this.PopupTextResourceKey;
    m_icn.PopupTitle = this.PopupTitle;
    m_icn.PopupTitleResourceKey = this.PopupTitleResourceKey;
    m_icn.LinkUrl = this.LinkUrl;
    m_icn.WarningIconVisible = this.WarningIconVisible;
    m_icn.Enabled = this.Enabled;
    m_icn.CssClass = this.WarningIconCssStyle;
    if (this.Required)
    {
    m_icn.Visible = true;
    }
    else
    {
    m_icn.Visible = false;
    }
    // For DesignMode, always display the warning icon
    m_icn.WarningIconVisible = true;
    }

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets or sets a value indicating whether this control uses AJAX
    callback functionality or not.
    /// </summary>
    /// <value><c>true</c> if callback enabled; otherwise,
    <c>false</c>.</value>
    [Bindable(true)]
    [Category("Callback")]
    [Description("Whether this control uses callback functionality or
    not.")]
    public bool CallbackEnabled
    {
    get
    {
    bool b = (bool)ViewState["IsCallback"];
    return b;
    }
    set
    {
    ViewState["IsCallback"] = value;
    }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this control is disabled
    during callback.
    /// </summary>
    /// <value><c>true</c> if disabled during callback; otherwise,
    <c>false</c>.</value>
    [Bindable(true)]
    [Category("Callback")]
    [Description("Whether this control is disabled while a background
    callback is in effect.")]
    public bool DisableAtCallback
    {
    get
    {
    bool b = (bool)ViewState["DisableAtCallback"];
    return b;
    }
    set
    {
    ViewState["DisableAtCallback"] = value;
    }
    }

    /// <summary>
    /// The location of the RadControls folder, containing the required
    JavaScript support files.
    /// </summary>
    /// <value>The scripts path.</value>
    [Bindable(true)]
    [Category("Callback")]
    [Description("The location of the RadControls folder, containing the
    required JavaScript support files.")]
    public string ScriptsPath
    {
    get
    {
    String s = (String)ViewState["ScriptsPath"];
    return ((s == null) ? String.Empty : s);
    }
    set
    {
    Debug.Assert(value != null, "Warning: ScriptsPath property
    is null!");
    if (value != null)
    {
    ViewState["ScriptsPath"] = value;
    }
    else
    {
    throw new NullReferenceException("ScriptsPath can not be
    assigned a null value.");
    }
    }
    }

    /// <summary>
    /// Gets or sets the position of the descriptive label relative to
    the rest of the control.
    /// </summary>
    /// <value>The label position.</value>
    [Bindable(true)]
    [Category("Label")]
    [DefaultValue(Position.Left)]
    [Localizable(false)]
    [Description("The position of the descriptive label relative to the
    rest of the control.")]
    public Position LabelPosition
    {
    get
    {
    Position p = (Position)ViewState["LabelPosition"];
    return p;
    }
    set
    {
    ViewState["LabelPosition"] = value;
    }
    }

    /// <summary>
    /// Gets or sets the text displayed in the descriptive label.
    /// </summary>
    /// <value>The label text.</value>
    [Bindable(true)]
    [Category("Label")]
    [DefaultValue("")]
    [Localizable(true)]
    [Description("The text to display in the descriptive label.")]
    public string LabelText
    {
    get
    {
    EnsureChildControls();
    String s = (String)ViewState["LabelText"];
    return ((s == null) ? String.Empty : s);
    }
    set
    {
    Debug.Assert(value != null, "Warning: LabelText property is
    null!");
    if (value != null)
    {
    ViewState["LabelText"] = value;
    }
    else
    {
    throw new NullReferenceException("LabelText can not be
    assigned a null value.");
    }
    }
    }

    /// <summary>
    /// Gets or sets the label text resource key.
    /// </summary>
    /// <value>The label text resource key.</value>
    [Bindable(true)]
    [Category("Label")]
    [DefaultValue("")]
    [Localizable(false)]
    public string LabelTextResourceKey
    {
    get
    {
    String s = (String)ViewState["LabelTextResourceKey"];
    return ((s == null) ? String.Empty : s);
    }
    set
    {
    Debug.Assert(value != null, "Warning: LabelTextResourceKey
    property is null!");
    if (value != null)
    {
    ViewState["LabelTextResourceKey"] = value;
    }
    else
    {
    throw new NullReferenceException("LabelTextResourceKey
    can not be assigned a null value.");
    }
    }
    }

    /// <summary>
    /// Gets or sets the label CSS class.
    /// </summary>
    /// <value>The label CSS class.</value>
    [Bindable(true)]
    [Category("Label")]
    [DefaultValue("")]
    [Localizable(false)]
    public string LabelCssClass
    {
    get
    {
    string s = (string)ViewState["LabelTextCssClass"];
    return (s == null) ? String.Empty : s;
    }
    set
    {
    Debug.Assert(value != null, "Warning: LabelCssClass property
    is null!");
    if (value != null)
    {
    ViewState["LabelTextCssClass"] = value;
    }
    else
    {
    throw new NullReferenceException("LabelTextCssClass can
    not be assigned a null value.");
    }
    }
    }

    /// <summary>
    /// Gets or sets a value indicating whether label is visible.
    /// </summary>
    /// <value><c>true</c> if label is visible; otherwise,
    <c>false</c>.</value>
    [Bindable(true)]
    [Category("Label")]
    [DefaultValue(true)]
    [Localizable(false)]
    public bool LabelVisible
    {
    get
    {
    bool b = (bool)ViewState["LabelVisible"];
    return (b);
    }
    set
    {
    ViewState["LabelVisible"] = value;
    }
    }

    /// <summary>
    /// Gets or sets the width of the label.
    /// </summary>
    /// <value>The width of the label.</value>
    [Bindable(true)]
    [Category("Label")]
    [Localizable(false)]
    [Description("The width of the descriptive label.")]
    public Unit LabelWidth
    {
    get
    {
    Unit u = (Unit)ViewState["LabelWidth"];
    return (u);
    }
    set
    {
    Debug.Assert(value != null, "Warning: LabelWidth property is
    null!");
    ViewState["LabelWidth"] = value;
    }
    }

    /// <summary>
    /// Gets or sets a value indicating whether the <see
    cref="SupportFormLabelledControl"/> is required.
    /// If required is set to true, than a validating <see
    cref="IconPopupControl"/> will be added to this control.
    /// </summary>
    /// <value><c>true</c> if required; otherwise, <c>false</c>.</value>
    [Bindable(true)]
    [Category("Required")]
    [DefaultValue(false)]
    [Localizable(false)]
    [Description("Determines whether the Required icon validator is
    present on this control or not.")]
    public bool Required
    {
    get
    {
    bool b = (bool)ViewState["Required"];
    return (b);
    }
    set
    {
    ViewState["Required"] = value;
    }
    }

    /// <summary>
    /// Gets or sets a value indicating whether to automatically post
    back when the control is clicked.
    /// </summary>
    /// <value><c>true</c> if controls posts back when clicked;
    otherwise, <c>false</c>.</value>
    [Bindable(true)]
    [Category("Behavior")]
    [DefaultValue(typeof(bool), "False")]
    [Description("Automatically posts back to the server when the
    control is clicked.")]
    [Localizable(false)]
    public bool AutoPostback
    {
    get
    {
    bool b = (bool)ViewState["AutoPostBack"];
    return b;
    }
    set
    {
    ViewState["AutoPostBack"] = value;
    }
    }

    /// <summary>
    /// Gets or sets the URL of the image to display when
    WarningIconVisible is set to true.
    /// </summary>
    /// <value>The warning image URL.</value>
    [Bindable(false)]
    [Category("Required")]
    [Localizable(false)]
    [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design,
    Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    typeof(UITypeEditor))]
    [Description("The image to display when WarningIconVisible is set to
    true.")]
    public string WarningImageUrl
    {
    get
    {
    string s = (string)ViewState["WarningImageUrl"];
    return (s == null) ? String.Empty : s;
    }
    set
    {
    Debug.Assert(value != null, "Warning: WarningImageUrl
    property is null!");
    if (value != null)
    {
    ViewState["WarningImageUrl"] = value;
    }
    else
    {
    throw new NullReferenceException("WarningImageUrl can
    not be assigned a null value.");
    }
    }
    }

    /// <summary>
    /// Gets or sets the URL of the page to open when the required
    icon's MessageStyle is set to UrlLink.
    /// </summary>
    /// <value>The link URL.</value>
    [Bindable(false)]
    [Category("Required")]
    [Localizable(false)]
    [Editor("System.Web.UI.Design.UrlEditor, System.Design,
    Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    typeof(UITypeEditor))]
    [Description("The URL of the page to open when the Required icon's
    MessageStyle is set to UrlLink.")]
    public string LinkUrl
    {
    get
    {
    string s = (string)ViewState["LinkUrl"];
    return (s == null) ? String.Empty : s;
    }
    set
    {
    Debug.Assert(value != null, "Warning: LinkUrl property is
    null!");
    if (value != null)
    {
    ViewState["LinkUrl"] = value;
    }
    else
    {
    throw new NullReferenceException("LinkUrl can not be
    assigned a null value.");
    }
    }
    }

    /// <summary>
    /// Gets or sets the type of message to display when the user clicks
    or moves their mouse over the required icon.
    /// </summary>
    /// <value>The message style.</value>
    [Bindable(false)]
    [Category("Required")]
    [Localizable(false)]
    [DefaultValue(MessageStyle.NoMessage)]
    [Description("The type of message to display when the user clicks or
    moves their mouse over the Required icon.")]
    public MessageStyle MessageStyle
    {
    get
    {
    MessageStyle m = (MessageStyle)ViewState["MessageStyle"];
    return m;
    }
    set
    {
    ViewState["MessageStyle"] = value;
    }
    }


    // ...
    // Several properties removed here for message length sake
    // ...

    #endregion
    }
    }
     
    Christophe Peillet, Jan 14, 2006
    #1
    1. Advertising

  2. Hi Christophe,

    Welcome.
    Regarding on this issue, I've also found your another duplicated one in the

    Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols

    I've posted my response there and if you feel it convenient that we
    continue to discuss in that thread, please feel free to post there.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)




    --------------------
    | Thread-Topic: CreateChildControls() always has default values for
    properties
    | thread-index: AcYZBNfNsoMjkZsyQLuG49CPfaU85w==
    | X-WBNR-Posting-Host: 82.236.3.78
    | From: =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=
    <>
    | Subject: CreateChildControls() always has default values for properties
    | Date: Sat, 14 Jan 2006 04:20:02 -0800
    | Lines: 740
    | Message-ID: <>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 7bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.buildingcontrols:14286
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    |
    | I am trying to develop a composite control that renders in a specific
    layout,
    | depending on the value of the custom LabelPosition property. The
    'layout' of
    | the rendered control will change depending on whether the value of
    | LabelPosition is set to Left, Right, Top, or Bottom, as can be seen in
    the
    | CreateChildControls() method.
    |
    | This particular class (included below) actually acts as a base class for
    | many other controls (such as a textbox, checkbox, datepicker, etc.), so
    not
    | all of the logic is implemented here. Because I don't know what objects
    will
    | need to be added by the child controls (textbox, etc.), I have inserted a
    | PlaceHolder control in the appropriate section of the table, and child
    | controls insert their own objects here. (This allows me to keep the code
    for
    | rendering the table in the parent class, making code maintenance much
    easier
    | for me if changes are ever needed.)
    |
    | The problem I'm having is that no matter which value I assign to the
    | LabelPosition property, it always returns the default value inside the
    | CreateChildControls() method. I explicitly set it to Top, and this
    appears
    | in the Html code behind, but it is always the default (Left) when I set
    and
    | reach a breakpoint in CreateChildControls().
    |
    | I'm sure my problem is that I have not properly understood the composite
    | control page model, and am setting default values in the wrong place
    (look at
    | the SetDefaultValues() method reference in the constructor), or am
    creating
    | the table in the wrong place (based on the 'switch' in
    | CreateChildControls()). Or, the problem could be something with
    ViewState,
    | that I am not properly storing/refreshing the value of the LabelPosition
    | property.
    |
    | If anyone can help me understand why this value is not being restored,
    and
    | always reads as default, I would enormously appreciate it, since I've
    spend
    | many hours on this to no avail, including a lot of time on Google.
    |
    | The base class is included below. If you need the Designer class (which
    | contains nothing exotic) or a child class (that implements this base
    class),
    | let me know any I'll post it.
    |
    | PS: Because of message length restrictions, I have removed some
    uneccessary
    | properties from this class.
    |
    | using System;
    | using System.Collections.Generic;
    | using System.Text;
    | using System.Web;
    | using System.Web.UI;
    | using System.Web.UI.WebControls;
    | using System.ComponentModel;
    | using System.ComponentModel.Design;
    | using System.Diagnostics;
    | using System.Drawing.Design;
    | using Telerik.WebControls;
    |
    | namespace CompanyName.EEE.Web.UI.FormControls
    | {
    | /// <summary>
    | /// The BaseClass used in all of CompanyName's form custom form
    controls
    | which require a descriptive label.
    | /// </summary>
    | // [Designer(typeof(SupportFormLabelledControlDesigner))]
    | [DefaultProperty("LabelText")]
    | [ToolboxData("<{0}:SupportFormLabelledControl
    | runat=\"server\"></{0}:SupportFormLabelledControl>")]
    | public abstract class SupportFormLabelledControl :
    SupportFormControl,
    | INamingContainer, ICallback, IRequired
    | {
    |
    | #region Protected Fields
    |
    | // Any third-party or external controls that you wish to add to
    the
    | this
    | // control, such as buttons, labels, etc., should be declared
    here.
    | By
    | // declaring them 'protected' they will be visible in any control
    that
    | // inherits from SupportFormLabelledControl
    |
    | /// <summary>The Table used to hold child controls on the
    | form</summary>
    | protected Table m_tbl;
    | /// <summary>The CallbackLabel rendered with the control</summary>
    | protected CallbackLabel m_lbl;
    | /// <summary>The Warning Icon rendered with the control when <see
    | cref="P:Required"/> is set to <c>true</c></summary>
    | protected IconPopupControl m_icn;
    | /// <summary>The content PlaceHolder used to insert any child
    | controls that need to be added (Textboxes, Checkboxes, etc).</summary>
    | protected PlaceHolder m_plc;
    |
    | #endregion
    |
    | #region Constructor
    |
    | // Default values for properties should ONLY be defined in the the
    | // class constructor. If you set properties elsewhere, such as
    in the
    | // OnLoad event, you will make the control insensitive to
    external,
    | // tag-level settings.
    |
    | /// <summary>
    | /// Initializes a new instance of the <see
    | cref="SupportFormLabelledControl"/> class.
    | /// </summary>
    | public SupportFormLabelledControl()
    | {
    | // Set control default values
    | this.SetDefaultValues();
    | }
    |
    | #endregion
    |
    | #region Abstract Members
    |
    | /// <summary>
    | /// If the control's Required property is set to True, this
    method
    | can be
    | /// used to validate whether the Required conditions are met or
    not.
    | /// </summary>
    | /// <returns>
    | /// <c>true</c> if the Required conditions are met, <c>false</c>
    if
    | they are not.
    | /// </returns>
    | public abstract bool ValidateControl();
    |
    | #endregion
    |
    | #region Protected Methods
    |
    | /// <summary>
    | /// Raises the <see cref="E:System.Web.UI.Control.Init"></see>
    event.
    | /// </summary>
    | /// <param name="e">An <see cref="T:System.EventArgs"></see>
    object
    | that contains the event data.</param>
    | protected override void OnInit(EventArgs e)
    | {
    | base.OnInit(e);
    | }
    |
    | /// <summary>
    | /// Raises the <see cref="E:System.Web.UI.Control.Load"></see>
    event.
    | /// </summary>
    | /// <param name="e">The <see cref="T:System.EventArgs"></see>
    object
    | that contains the event data.</param>
    | protected override void OnLoad(EventArgs e)
    | {
    | // This method captures the controls Load event (declared and
    | bound in the
    | // class constructor). RunTime rendering takes place in this
    | method (as
    | // opposed to DesignMode rendering, which takes place
    elsewhere).
    |
    | // Call base Load method
    | base.OnLoad(e);
    |
    | EnsureChildControls();
    | }
    |
    | /// <summary>
    | /// Called by the ASP.NET page framework to notify server
    controls
    | that use
    | /// composition-based implementation to create any child controls
    | they contain
    | /// in preparation for posting back or rendering.
    | /// </summary>
    | protected override void CreateChildControls()
    | {
    | // Clear the control collection
    | Controls.Clear();
    |
    | // Any dependant controls used in this custom control must
    | // be added to the control 'hierarchy'. If they are not added
    | // to the control collection, they will not be visible to
    other
    | // controls on the page.
    |
    | // Instantiate any dependant controls
    | m_tbl = new Table();
    | m_lbl = new CallbackLabel();
    | m_icn = new IconPopupControl();
    | m_plc = new PlaceHolder();
    |
    | // Create table object and format it through relevant method
    | m_tbl =
    | SharedFunctions.CreateLabelledControlTable(this.LabelPosition);
    |
    | // Add table to the control collection
    | Controls.Add(m_tbl);
    |
    | // Add controls to the table control collection
    | switch (this.LabelPosition)
    | {
    | case Position.Left:
    | m_tbl.Rows[0].Cells[0].Controls.Add(m_lbl);
    | m_tbl.Rows[0].Cells[1].Controls.Add(m_plc);
    | m_tbl.Rows[0].Cells[1].Controls.Add(m_icn);
    | // Set relevant design properties
    | m_tbl.Rows[0].Cells[0].Width = new
    | Unit(this.LabelWidth.ToString());
    | break;
    | case Position.Top:
    | m_tbl.Rows[0].Cells[0].Controls.Add(m_lbl);
    | m_tbl.Rows[1].Cells[0].Controls.Add(m_plc);
    | m_tbl.Rows[1].Cells[0].Controls.Add(m_icn);
    | // Set relevant design properties
    | m_tbl.Rows[0].Cells[0].Width = new
    | Unit(this.LabelWidth.ToString());
    | break;
    | case Position.Right:
    | m_tbl.Rows[0].Cells[0].Controls.Add(m_plc);
    | m_tbl.Rows[0].Cells[0].Controls.Add(m_icn);
    | m_tbl.Rows[0].Cells[1].Controls.Add(m_lbl);
    | // Set relevant design properties
    | m_tbl.Rows[0].Cells[1].Width = new
    | Unit(this.LabelWidth.ToString());
    | break;
    | case Position.Bottom:
    | m_tbl.Rows[0].Cells[0].Controls.Add(m_plc);
    | m_tbl.Rows[0].Cells[0].Controls.Add(m_icn);
    | m_tbl.Rows[1].Cells[0].Controls.Add(m_lbl);
    | // Set relevant design properties
    | m_tbl.Rows[1].Cells[0].Width = new
    | Unit(this.LabelWidth.ToString());
    | break;
    | default:
    | Debug.Assert(false);
    | break;
    | }
    |
    | // Call base method
    | base.CreateChildControls();
    | }
    |
    | protected override void OnPreRender(EventArgs e)
    | {
    | // Call base method
    | base.OnPreRender(e);
    |
    | // Associate dependent control properties with this control's
    | properties
    | m_lbl.CssClass = this.LabelCssClass;
    | m_lbl.Text = this.LabelText;
    | m_lbl.Visible = this.LabelVisible;
    | m_lbl.RadControlsDir = this.ScriptsPath;
    | m_lbl.CallbackEnabled = this.CallbackEnabled;
    | m_lbl.DisableAtCallback = this.DisableAtCallback;
    | m_lbl.Enabled = this.Enabled;
    | m_icn.WarningImageUrl = this.WarningImageUrl;
    | m_icn.ImageAlign = this.ImageAlign;
    | m_icn.EmptyImageUrl = this.EmptyImageUrl;
    | m_icn.MessageStyle = this.MessageStyle;
    | m_icn.PopupText = this.PopupText;
    | m_icn.PopupTextResourceKey = this.PopupTextResourceKey;
    | m_icn.PopupTitle = this.PopupTitle;
    | m_icn.PopupTitleResourceKey = this.PopupTitleResourceKey;
    | m_icn.LinkUrl = this.LinkUrl;
    | m_icn.Enabled = this.Enabled;
    | m_icn.CssClass = this.WarningIconCssStyle;
    |
    | // Enable or disable warning icon as appropriate
    | m_icn.Visible = this.Required ? true : false;
    | }
    |
    | #endregion
    |
    | #region Private/Internal Methods
    |
    | /// <summary>
    | /// Sets the control's default values.
    | /// </summary>
    | private void SetDefaultValues()
    | {
    | // Set default properties
    | this.LabelPosition = Position.Left;
    | this.LabelText = string.Empty;
    | this.LabelCssClass = SharedConstants.SupportForm_LabelStyle;
    | this.LabelVisible = true;
    | this.LabelWidth = new
    | Unit(SharedConstants.SupportForm_DefaultLabelWidth);
    | this.Required = false;
    | this.CallbackEnabled =
    | SharedConstants.SupportForm_CallbackEnabledByDefault;
    | this.DisableAtCallback =
    | SharedConstants.SupportForm_DisableAtCallback;
    | this.ScriptsPath =
    SharedConstants.SupportForm_DefaultScriptsPath;
    | this.AutoPostback = false;
    | this.ImageAlign = ImageAlign.AbsMiddle;
    | this.LinkUrl = string.Empty;
    | this.MessageStyle = MessageStyle.NoMessage;
    | this.PopupText = string.Empty;
    | this.PopupTextResourceKey = string.Empty;
    | this.PopupTitle = string.Empty;
    | this.PopupTitleResourceKey = string.Empty;
    | this.WarningIconVisible = false;
    | this.WarningIconCssStyle =
    | SharedConstants.SupportForm_WarningIconStyle;
    | this.WarningImageUrl =
    | SharedConstants.SupportForm_IconPopupControlDefaultWarningImageUrl;
    | this.EmptyImageUrl =
    | SharedConstants.SupportForm_IconPopupControlDefaultEmptyImageUrl;
    | }
    |
    | /// <summary>
    | /// Sets the design mode values.
    | /// </summary>
    | internal virtual void SetDesignModeValues()
    | {
    | // Ensure child controls are created
    | EnsureChildControls();
    |
    | // Associate dependent control properties with this control's
    | properties
    | m_lbl.CssClass = this.LabelCssClass;
    | m_lbl.Text = this.LabelText == string.Empty ? "[LabelText]" :
    | this.LabelText;
    | m_lbl.Visible = this.LabelVisible;
    | m_lbl.RadControlsDir = this.ScriptsPath;
    | m_lbl.CallbackEnabled = this.CallbackEnabled;
    | m_lbl.DisableAtCallback = this.DisableAtCallback;
    | m_lbl.Enabled = this.Enabled;
    | m_icn.WarningImageUrl = this.WarningImageUrl;
    | m_icn.ImageAlign = this.ImageAlign;
    | m_icn.EmptyImageUrl = this.EmptyImageUrl;
    | m_icn.MessageStyle = this.MessageStyle;
    | m_icn.PopupText = this.PopupText;
    | m_icn.PopupTextResourceKey = this.PopupTextResourceKey;
    | m_icn.PopupTitle = this.PopupTitle;
    | m_icn.PopupTitleResourceKey = this.PopupTitleResourceKey;
    | m_icn.LinkUrl = this.LinkUrl;
    | m_icn.WarningIconVisible = this.WarningIconVisible;
    | m_icn.Enabled = this.Enabled;
    | m_icn.CssClass = this.WarningIconCssStyle;
    | if (this.Required)
    | {
    | m_icn.Visible = true;
    | }
    | else
    | {
    | m_icn.Visible = false;
    | }
    | // For DesignMode, always display the warning icon
    | m_icn.WarningIconVisible = true;
    | }
    |
    | #endregion
    |
    | #region Public Properties
    |
    | /// <summary>
    | /// Gets or sets a value indicating whether this control uses
    AJAX
    | callback functionality or not.
    | /// </summary>
    | /// <value><c>true</c> if callback enabled; otherwise,
    | <c>false</c>.</value>
    | [Bindable(true)]
    | [Category("Callback")]
    | [Description("Whether this control uses callback functionality or
    | not.")]
    | public bool CallbackEnabled
    | {
    | get
    | {
    | bool b = (bool)ViewState["IsCallback"];
    | return b;
    | }
    | set
    | {
    | ViewState["IsCallback"] = value;
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets a value indicating whether this control is
    disabled
    | during callback.
    | /// </summary>
    | /// <value><c>true</c> if disabled during callback; otherwise,
    | <c>false</c>.</value>
    | [Bindable(true)]
    | [Category("Callback")]
    | [Description("Whether this control is disabled while a background
    | callback is in effect.")]
    | public bool DisableAtCallback
    | {
    | get
    | {
    | bool b = (bool)ViewState["DisableAtCallback"];
    | return b;
    | }
    | set
    | {
    | ViewState["DisableAtCallback"] = value;
    | }
    | }
    |
    | /// <summary>
    | /// The location of the RadControls folder, containing the
    required
    | JavaScript support files.
    | /// </summary>
    | /// <value>The scripts path.</value>
    | [Bindable(true)]
    | [Category("Callback")]
    | [Description("The location of the RadControls folder, containing
    the
    | required JavaScript support files.")]
    | public string ScriptsPath
    | {
    | get
    | {
    | String s = (String)ViewState["ScriptsPath"];
    | return ((s == null) ? String.Empty : s);
    | }
    | set
    | {
    | Debug.Assert(value != null, "Warning: ScriptsPath
    property
    | is null!");
    | if (value != null)
    | {
    | ViewState["ScriptsPath"] = value;
    | }
    | else
    | {
    | throw new NullReferenceException("ScriptsPath can not
    be
    | assigned a null value.");
    | }
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets the position of the descriptive label relative
    to
    | the rest of the control.
    | /// </summary>
    | /// <value>The label position.</value>
    | [Bindable(true)]
    | [Category("Label")]
    | [DefaultValue(Position.Left)]
    | [Localizable(false)]
    | [Description("The position of the descriptive label relative to
    the
    | rest of the control.")]
    | public Position LabelPosition
    | {
    | get
    | {
    | Position p = (Position)ViewState["LabelPosition"];
    | return p;
    | }
    | set
    | {
    | ViewState["LabelPosition"] = value;
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets the text displayed in the descriptive label.
    | /// </summary>
    | /// <value>The label text.</value>
    | [Bindable(true)]
    | [Category("Label")]
    | [DefaultValue("")]
    | [Localizable(true)]
    | [Description("The text to display in the descriptive label.")]
    | public string LabelText
    | {
    | get
    | {
    | EnsureChildControls();
    | String s = (String)ViewState["LabelText"];
    | return ((s == null) ? String.Empty : s);
    | }
    | set
    | {
    | Debug.Assert(value != null, "Warning: LabelText property
    is
    | null!");
    | if (value != null)
    | {
    | ViewState["LabelText"] = value;
    | }
    | else
    | {
    | throw new NullReferenceException("LabelText can not
    be
    | assigned a null value.");
    | }
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets the label text resource key.
    | /// </summary>
    | /// <value>The label text resource key.</value>
    | [Bindable(true)]
    | [Category("Label")]
    | [DefaultValue("")]
    | [Localizable(false)]
    | public string LabelTextResourceKey
    | {
    | get
    | {
    | String s = (String)ViewState["LabelTextResourceKey"];
    | return ((s == null) ? String.Empty : s);
    | }
    | set
    | {
    | Debug.Assert(value != null, "Warning:
    LabelTextResourceKey
    | property is null!");
    | if (value != null)
    | {
    | ViewState["LabelTextResourceKey"] = value;
    | }
    | else
    | {
    | throw new
    NullReferenceException("LabelTextResourceKey
    | can not be assigned a null value.");
    | }
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets the label CSS class.
    | /// </summary>
    | /// <value>The label CSS class.</value>
    | [Bindable(true)]
    | [Category("Label")]
    | [DefaultValue("")]
    | [Localizable(false)]
    | public string LabelCssClass
    | {
    | get
    | {
    | string s = (string)ViewState["LabelTextCssClass"];
    | return (s == null) ? String.Empty : s;
    | }
    | set
    | {
    | Debug.Assert(value != null, "Warning: LabelCssClass
    property
    | is null!");
    | if (value != null)
    | {
    | ViewState["LabelTextCssClass"] = value;
    | }
    | else
    | {
    | throw new NullReferenceException("LabelTextCssClass
    can
    | not be assigned a null value.");
    | }
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets a value indicating whether label is visible.
    | /// </summary>
    | /// <value><c>true</c> if label is visible; otherwise,
    | <c>false</c>.</value>
    | [Bindable(true)]
    | [Category("Label")]
    | [DefaultValue(true)]
    | [Localizable(false)]
    | public bool LabelVisible
    | {
    | get
    | {
    | bool b = (bool)ViewState["LabelVisible"];
    | return (b);
    | }
    | set
    | {
    | ViewState["LabelVisible"] = value;
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets the width of the label.
    | /// </summary>
    | /// <value>The width of the label.</value>
    | [Bindable(true)]
    | [Category("Label")]
    | [Localizable(false)]
    | [Description("The width of the descriptive label.")]
    | public Unit LabelWidth
    | {
    | get
    | {
    | Unit u = (Unit)ViewState["LabelWidth"];
    | return (u);
    | }
    | set
    | {
    | Debug.Assert(value != null, "Warning: LabelWidth property
    is
    | null!");
    | ViewState["LabelWidth"] = value;
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets a value indicating whether the <see
    | cref="SupportFormLabelledControl"/> is required.
    | /// If required is set to true, than a validating <see
    | cref="IconPopupControl"/> will be added to this control.
    | /// </summary>
    | /// <value><c>true</c> if required; otherwise,
    <c>false</c>.</value>
    | [Bindable(true)]
    | [Category("Required")]
    | [DefaultValue(false)]
    | [Localizable(false)]
    | [Description("Determines whether the Required icon validator is
    | present on this control or not.")]
    | public bool Required
    | {
    | get
    | {
    | bool b = (bool)ViewState["Required"];
    | return (b);
    | }
    | set
    | {
    | ViewState["Required"] = value;
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets a value indicating whether to automatically post
    | back when the control is clicked.
    | /// </summary>
    | /// <value><c>true</c> if controls posts back when clicked;
    | otherwise, <c>false</c>.</value>
    | [Bindable(true)]
    | [Category("Behavior")]
    | [DefaultValue(typeof(bool), "False")]
    | [Description("Automatically posts back to the server when the
    | control is clicked.")]
    | [Localizable(false)]
    | public bool AutoPostback
    | {
    | get
    | {
    | bool b = (bool)ViewState["AutoPostBack"];
    | return b;
    | }
    | set
    | {
    | ViewState["AutoPostBack"] = value;
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets the URL of the image to display when
    | WarningIconVisible is set to true.
    | /// </summary>
    | /// <value>The warning image URL.</value>
    | [Bindable(false)]
    | [Category("Required")]
    | [Localizable(false)]
    | [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design,
    | Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    | typeof(UITypeEditor))]
    | [Description("The image to display when WarningIconVisible is set
    to
    | true.")]
    | public string WarningImageUrl
    | {
    | get
    | {
    | string s = (string)ViewState["WarningImageUrl"];
    | return (s == null) ? String.Empty : s;
    | }
    | set
    | {
    | Debug.Assert(value != null, "Warning: WarningImageUrl
    | property is null!");
    | if (value != null)
    | {
    | ViewState["WarningImageUrl"] = value;
    | }
    | else
    | {
    | throw new NullReferenceException("WarningImageUrl can
    | not be assigned a null value.");
    | }
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets the URL of the page to open when the required
    | icon's MessageStyle is set to UrlLink.
    | /// </summary>
    | /// <value>The link URL.</value>
    | [Bindable(false)]
    | [Category("Required")]
    | [Localizable(false)]
    | [Editor("System.Web.UI.Design.UrlEditor, System.Design,
    | Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    | typeof(UITypeEditor))]
    | [Description("The URL of the page to open when the Required
    icon's
    | MessageStyle is set to UrlLink.")]
    | public string LinkUrl
    | {
    | get
    | {
    | string s = (string)ViewState["LinkUrl"];
    | return (s == null) ? String.Empty : s;
    | }
    | set
    | {
    | Debug.Assert(value != null, "Warning: LinkUrl property is
    | null!");
    | if (value != null)
    | {
    | ViewState["LinkUrl"] = value;
    | }
    | else
    | {
    | throw new NullReferenceException("LinkUrl can not be
    | assigned a null value.");
    | }
    | }
    | }
    |
    | /// <summary>
    | /// Gets or sets the type of message to display when the user
    clicks
    | or moves their mouse over the required icon.
    | /// </summary>
    | /// <value>The message style.</value>
    | [Bindable(false)]
    | [Category("Required")]
    | [Localizable(false)]
    | [DefaultValue(MessageStyle.NoMessage)]
    | [Description("The type of message to display when the user clicks
    or
    | moves their mouse over the Required icon.")]
    | public MessageStyle MessageStyle
    | {
    | get
    | {
    | MessageStyle m = (MessageStyle)ViewState["MessageStyle"];
    | return m;
    | }
    | set
    | {
    | ViewState["MessageStyle"] = value;
    | }
    | }
    |
    |
    | // ...
    | // Several properties removed here for message length sake
    | // ...
    |
    | #endregion
    | }
    | }
    |
     
    Steven Cheng[MSFT], Jan 16, 2006
    #2
    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. Jonathan Wood
    Replies:
    2
    Views:
    4,323
    Jonathan Wood
    Feb 8, 2008
  2. Ben Schwehn

    how to set default values for non-basic type properties?

    Ben Schwehn, Sep 21, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    230
    Ben Schwehn
    Sep 22, 2003
  3. Praveen Ramesh

    Specifying dynamic default values for properties in custom controls:

    Praveen Ramesh, Sep 2, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    137
    Praveen Ramesh
    Sep 2, 2004
  4. ~~~ .NET Ed ~~~
    Replies:
    1
    Views:
    237
    ~~~ .NET Ed ~~~
    Mar 25, 2005
  5. Christophe Peillet

    CreateChildControls() always returns default property values

    Christophe Peillet, Jan 14, 2006, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    359
    Steven Cheng[MSFT]
    Jan 19, 2006
Loading...

Share This Page