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?