这段代码是从petshop4.0里面 拷贝下来,我觉得非常好。以后也会经常用到:
<asp:Repeater ID="repWishList" runat="server">
<HeaderTemplate>
<table cellspacing="0" cellpadding="3" rules="all" border="0" class="cart" align="center" width="387">
<tr class="labelLists">
<th scope="col"> </th>
<th scope="col">Name</th>
<th align="right" scope="col">Price</th>
<th scope="col"> </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="listItem">
<td>
<asp:ImageButton ID="btnDelete" runat="server" AlternateText="Delete" CausesValidation="false"
CommandArgument='<%# Eval("ItemId") %>' CommandName="Del" ImageUrl="~/Comm_Images/button-delete.gif"
OnCommand="CartItem_Command" ToolTip="Delete" />
</td>
<td style="width:100%;">
<a runat="server" href='<%# string.Format("~/Items.aspx?itemId={0}&productId={1}&categoryId={2}", Eval("ItemId"), Eval("ProductId"), Eval("CategoryId")) %>'><%# string.Format("{0} {1}", Eval("Name"), Eval("Type")) %></a>
</td>
<td align="right"><%# Eval("Price", "{0:c}")%></td><td>
<asp:ImageButton ID="btnToWishList" runat="server" AlternateText="Move to cart" CausesValidation="false"
CommandArgument='<%# Eval("ItemId") %>' CommandName="Move" ImageUrl="~/Comm_Images/button-cart.gif"
OnCommand="CartItem_Command" ToolTip="Move to cart" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
cs部分代码:
/// <summary>
/// Bind repeater to Cart object in Profile
/// </summary>
private void BindCart() {
ICollection<CartItemInfo> wishList = Profile.WishList.CartItems;
if (wishList.Count > 0) {
repWishList.DataSource = wishList;
repWishList.DataBind();
}
else {
repWishList.Visible = false;
lblMsg.Text = "Your wish list is empty.";
}
}
/// <summary>
/// Handler for Delete/Move buttons
/// </summary>
protected void CartItem_Command(object sender, CommandEventArgs e) {
switch(e.CommandName.ToString()) {
case "Del":
Profile.WishList.Remove(e.CommandArgument.ToString());
break;
case "Move":
Profile.WishList.Remove(e.CommandArgument.ToString());
Profile.ShoppingCart.Add(e.CommandArgument.ToString());
break;
}
Profile.Save();
BindCart();
}