CompositeControl not saving child controls viewstate

Last post 06-26-2009 6:23 AM by Slipperman. 3 replies.

Sort Posts:

  • CompositeControl not saving child controls viewstate

    06-25-2009, 2:01 AM
    • Member
      77 point Member
    • bhav27
    • Member since 06-07-2006, 4:10 PM
    • Posts 95

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using eBizTakeAway.WebStore.Catalogue; using eBizSuite.Common.Shared; namespace eBizTakeAway.Web.UI.ServerControls { [ToolboxData("<{0}:MenuOptionsConfig runat=server></{0}:MenuOptionsConfig>")] public class MenuOptionsConfig : CompositeControl { #region private members protected MenuOptionCollection _menuOptionList; protected CatalogMenuOptionCollection _catalogMenuOptionList; #endregion #region private methods protected void CreateSingleSelectionControl(MenuOptions menuOption, bool bDataBind) { CheckBoxList chkList = new CheckBoxList(); Controls.Add(chkList); if (bDataBind) { chkList.ID = menuOption.OptionName.Replace(" ", ""); chkList.RepeatDirection = RepeatDirection.Horizontal; chkList.RepeatColumns = 3; chkList.DataSource = menuOption.Options; chkList.DataTextField = "Name"; chkList.DataValueField = "Name"; chkList.DataBind(); } } protected void CreateMultiSelectionControl(MenuOptions menuOption, bool bDataBind) { RadioButtonList rblList = new RadioButtonList(); Controls.Add(rblList); if (bDataBind) { rblList.ID = menuOption.OptionName.Replace(" ", ""); rblList.RepeatDirection = RepeatDirection.Horizontal; rblList.RepeatColumns = 3; rblList.DataSource = menuOption.Options; rblList.DataTextField = "Name"; rblList.DataValueField = "Name"; rblList.DataBind(); } } #endregion #region public properties public MenuOptionCollection MenuOptions { set { _menuOptionList = value; EnsureChildControls(); } } public CatalogMenuOptionCollection CatalogMenuOptions { get { return _catalogMenuOptionList; } } #endregion #region overridable members protected override void CreateChildControls() { try { if (_menuOptionList != null) { _menuOptionList.GetCollection().ForEach(menuOption => { LiteralControl header = new LiteralControl(string.Format("<h2>{0}</h2>", menuOption.OptionName)); LiteralControl desc = new LiteralControl(); if (menuOption.Description != string.Empty) desc = new LiteralControl(string.Format("<h3>{0}</h3>", menuOption.Description)); Controls.Add(header); Controls.Add(desc); switch (menuOption.SelectionType) { case MenuOptionSelectionType.Single: CreateSingleSelectionControl(menuOption, !Page.IsPostBack); break; case MenuOptionSelectionType.Multiple: CreateMultiSelectionControl(menuOption, !Page.IsPostBack); break; } }); } } catch (Exception ex) { throw ex; } finally { base.CreateChildControls(); } } #endregion #region public methods #endregion #region event handlers #endregion } }

    Above is the code I am currently using to develop my custom server control. Basically based on "MenuOptions" property which user sets the control builds by Child controls. The big problem here I have is when user postbacks the control fails to render the child controls. I tried to debug and found out that in the first postback the debugger directly goes to CreateChildControls() rather page_load() of the page.

    On the second postback the debugger behaves normally but never renders checkboxlist and radiobuttonlist in the child controls but just literalcontrols.

    Can someone review the code and let me know what I am doing wrong or missing here?

  • Re: CompositeControl not saving child controls viewstate

    06-25-2009, 7:03 AM
    • Member
      96 point Member
    • Slipperman
    • Member since 05-25-2009, 7:27 AM
    • Posts 39

    you might want to repost your problem description (hard to read). no need to put it in a code segment - just use the regular textbox.

  • Re: CompositeControl not saving child controls viewstate

    06-26-2009, 1:44 AM
    • Member
      77 point Member
    • bhav27
    • Member since 06-07-2006, 4:10 PM
    • Posts 95

    Slipperman:

    you might want to repost your problem description (hard to read). no need to put it in a code segment - just use the regular textbox.

     

    When I tried to insert problem text at the top of code the richtextbox here takes my text as code so I had to put it at the end.

  • Re: CompositeControl not saving child controls viewstate

    06-26-2009, 6:23 AM
    Answer
    • Member
      96 point Member
    • Slipperman
    • Member since 05-25-2009, 7:27 AM
    • Posts 39

    1. have you checked the value of _menuOptionList in debug to make sure it's not null?

    2. if it is, try placing the value of _menuOptionList in the ViewState collection within the property then refer to it via the property name (MenuOptions) in CreateChildControls. also try commenting out EnsureChildControls for now.

    3. you're switching on menuOption.SelectionType for 2 possible values. but where is this actually set to one or the other? there's no reference to it.

Page 1 of 1 (4 items)