I have a shopping cart user control that I want to turn into a composite control. The shopping cart uses a DataList which contains an ItemTemplate. Within the ItemTemplate is a TextBox, 2 Labels, an Image, and a LinkButton. I have been unsuccessful in getting the DataList to render. Following are relevant code snippets. Note: I've only included the code to generate the TextBox within the ItemTemplate for brevity sake.
1) Define Shopping Cart composite control:
[DefaultProperty("Text")]
[ToolboxData("<{0}:Shopping_Cart runat=server></{0}:Shopping_Cart>")]
class Shopping_Cart: CompositeControl
{
#region DECLARATIONS
private DataList CartDL;
#endregion
2) Define a property for the DataList:
#region PROPERTIES
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
public DataList CartDataList
{
get
{
EnsureChildControls();
return CartDL;
}
}
#endregion
3) Override CreateChildControls function to build DataList:
protected override void CreateChildControls()
{
Controls.Clear();
//Add Cart DataList
CartDL = new DataList();
CartDL.CssClass = "CartDataList";
CartDL.DataKeyField = "CERT_ID";
CartDL.ItemCommand += new DataListCommandEventHandler
(this.CartEvent);
CartDL.ID = "Custom_CartDL";
TemplateColumn objTC = new TemplateColumn();
objTC.ItemTemplate = new ShoppingCartDataListItem();
objTC.ItemTemplate.InstantiateIn(CartDL);
Controls.Add(CartDL);
base.CreateChildControls();
}
4) Define the Template class from which the Template controls will be built:
public class ShoppingCartDataListItem : ITemplate
{
public void InstantiateIn(Control container)
{
//Add CartItemQuantity Textbox
TextBox tbCartItemQuantity = new TextBox();
tbCartItemQuantity.CssClass = "CartItemQty";
tbCartItemQuantity.MaxLength = 2;
tbCartItemQuantity.ID = "Custom_CartItemQty";
tbCartItemQuantity.DataBinding += new EventHandler
(this.BindCartItemQuantity);
container.Controls.Add(tbCartItemQuantity);
}
// Handler of the OnDataBinding event for the TextBox element
private void BindCartItemQuantity(Object sender, EventArgs e)
{
TextBox tbCIQ = (TextBox)sender;
DataListItem container = (DataListItem)tbCIQ.NamingContainer;
tbCIQ.Text = Convert.ToString(DataBinder.Eval(((DataListItem)
container).DataItem, "CERT_QTY"));
}
}
As stated, the DataList never renders--any ideas?
1) Define Shopping Cart composite control:
[DefaultProperty("Text")]
[ToolboxData("<{0}:Shopping_Cart runat=server></{0}:Shopping_Cart>")]
class Shopping_Cart: CompositeControl
{
#region DECLARATIONS
private DataList CartDL;
#endregion
2) Define a property for the DataList:
#region PROPERTIES
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
public DataList CartDataList
{
get
{
EnsureChildControls();
return CartDL;
}
}
#endregion
3) Override CreateChildControls function to build DataList:
protected override void CreateChildControls()
{
Controls.Clear();
//Add Cart DataList
CartDL = new DataList();
CartDL.CssClass = "CartDataList";
CartDL.DataKeyField = "CERT_ID";
CartDL.ItemCommand += new DataListCommandEventHandler
(this.CartEvent);
CartDL.ID = "Custom_CartDL";
TemplateColumn objTC = new TemplateColumn();
objTC.ItemTemplate = new ShoppingCartDataListItem();
objTC.ItemTemplate.InstantiateIn(CartDL);
Controls.Add(CartDL);
base.CreateChildControls();
}
4) Define the Template class from which the Template controls will be built:
public class ShoppingCartDataListItem : ITemplate
{
public void InstantiateIn(Control container)
{
//Add CartItemQuantity Textbox
TextBox tbCartItemQuantity = new TextBox();
tbCartItemQuantity.CssClass = "CartItemQty";
tbCartItemQuantity.MaxLength = 2;
tbCartItemQuantity.ID = "Custom_CartItemQty";
tbCartItemQuantity.DataBinding += new EventHandler
(this.BindCartItemQuantity);
container.Controls.Add(tbCartItemQuantity);
}
// Handler of the OnDataBinding event for the TextBox element
private void BindCartItemQuantity(Object sender, EventArgs e)
{
TextBox tbCIQ = (TextBox)sender;
DataListItem container = (DataListItem)tbCIQ.NamingContainer;
tbCIQ.Text = Convert.ToString(DataBinder.Eval(((DataListItem)
container).DataItem, "CERT_QTY"));
}
}
As stated, the DataList never renders--any ideas?