For a shopping cart page, the list of items is displayed in a html table. I use a ListView for that and it works great.
When the cart is empty, the text 'This cart is empty' appears. But it only renders the code in the EmptyDataTemplate. My goal is to display the table headers ('delete', 'product', 'quantity', etc.) without repeating that html code in the EmptyDataTemplate.
Trying being clever I changed my EmptyDataTemplate into an EditItemTemplate and used the bit of code displayed below.
Can anyone think of a more more elegant solution for this problem??
[C# code]
lvShoppingCart.DataSource = _cart.Items;
lvShoppingCart.DataBind();
if (_cart.ProductCount == 0)
{
lvShoppingCart.DataSource = new List<string>() { "dummy cart item" };
lvShoppingCart.EditIndex = 0;
lvShoppingCart.DataBind();
}
[ASPX code]
<asp:ListView ID="lvShoppingCart" runat="server">
<LayoutTemplate>
<table style="width: 600px;" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">
<strong>Delete</strong>
</td>
<td width="400">
<strong>Product</strong>
</td>
<td width="100">
<strong>Quantity</strong>
</td>
<td width="100">
<strong>Price</strong>
</td>
<td width="100">
<strong>Total</strong>
</td>
</tr>
</table>
<hr />
</td>
</tr>
<tr id="itemPlaceHolder" runat="server">
</tr>
<tr id="trShoppingCartUpdateBtn" runat="server">
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">
</td>
<td width="400">
</td>
<td colspan="3" width="300">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<asp:ImageButton ID="btnImgUpdateQuantities" ImageUrl="../img/refresh.gif" AlternateText="update shopping cart"
OnClick="btnUpdateQuantities_Click" runat="server" />
</td>
<td>
<asp:LinkButton ID="btnUpdateQuantities" Text="update cart" OnClick="btnUpdateQuantities_Click"
runat="server" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr id="trShoppingCartTotals" runat="server">
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="4">
<div align="right">
<strong>Totals: </strong>
</div>
</td>
<td width="100">
<asp:Label ID="lblCartTotal" runat="server" Text="0" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<EditItemTemplate>
<tr>
<td colspan="5" align="center">
<p>
<em>This cart is empty.</em>
</p>
</td>
</tr>
</EditItemTemplate>
<ItemTemplate>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">
<a href='<%# ShoppingCartUrl %>?action=remove&id=<%# Eval("Product.Id") %>'>X</a>
</td>
<td width="400">
<%# Eval("Product.DisplayName") %>
</td>
<td width="100">
<label>
<asp:TextBox ID="txtQuantity" Text='<%# Eval("Quantity") %>' runat="server" size="3" />
</label>
</td>
<td width="100">
<%# Eval("Price", "{0:C}") %>
</td>
<td width="100">
<%# Eval("TotalPrice", "{0:C}") %>
</td>
</tr>
</table>
<hr />
</td>
</tr>
</ItemTemplate>
</asp:ListView>