.NET 2.0 Composite Control with DataList won't render

Joined
Sep 14, 2006
Messages
5
Reaction score
0
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top