我一直在思考如何解决特定问题。我需要创建一个购物车页面,用户可以在其中设置数量或从购物车中删除该特定商品。我决定使用Repeater。并且值是从数据库中获取的
我的代码如下
<asp:Repeater ID="RepeatStatus" runat="server" OnItemDataBound="RepeatStatus_ItemDataBound" onitemcommand="Button_ItemCommand" EnableViewState="False">
<HeaderTemplate><tr style="color: #FFFFFF; background-color: #3D7169">
<td>Product Name</td>
<td>Quantity</td>
<td>Price</td>
<td>Total</td>
</tr></HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container,"DataItem.ProductName")%></td>
<td><asp:TextBox ID="txtQty" runat="server" Height="23px" Width="50px" Text=<%#DataBinder.Eval(Container,"DataItem.Quantity")%>></asp:TextBox><br />
<asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("CDetailsID") %>' style="font-size:12px;"></asp:LinkButton>
</td>
<td><asp:Label ID="lblPrice" runat="server" Text=<%#DataBinder.Eval(Container,"DataItem.Price")%>></asp:Label></td>
<td><asp:Label ID="lblTotal" runat="server"></asp:Label></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<br />
<tr style="color: #FFFFFF; background-color: #6C6B66">
<td colspan="4" class="cartTotal">Final Price: </td>
</tr>
<tr><td colspan="4">
<asp:Label ID="lblEmptyData" Text="No Data Found" runat="server" Visible="false" Font-Bold="True">
</asp:Label></td></tr>
<tr><td colspan="4">
<asp:Button ID="btnUpdate" runat="server" Text="Update" />
</td></tr>
</FooterTemplate>
</asp:Repeater>
我背后的代码是这个
protected void RepeatStatus_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
int q;
decimal p = 0, t = 0;
int j = RepeatStatus.Items.Count;
if (RepeatStatus.Items.Count > 0)
{
foreach (RepeaterItem ri in RepeatStatus.Items)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label price = (Label)e.Item.FindControl("lblPrice");
TextBox qty = (TextBox)e.Item.FindControl("txtQty");
Label total = (Label)e.Item.FindControl("lblTotal");
q = Convert.ToInt32(qty.Text);
p = Convert.ToDecimal(price.Text.Substring(1));
t = (p * q);
total.Text = "$" + t.ToString();
}
}
}
if (RepeatStatus.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblFooter = (Label)e.Item.FindControl("lblEmptyData");
lblFooter.Visible = true;
Button btnU = (Button)e.Item.FindControl("btnUpdate");
btnU.Visible = false;
}
}
}
出于某种我一直在努力寻找的原因,第一条记录永远不会计算产品的子定价。但第二条及以后的记录将正确显示该值。我做错了什么还是应该将其更改为使用 Datalist?