我一直在探索如何解決特定問題。我需要創建一個購物車頁面,用戶可以在其中設置其數量或從購物車中移除該特定物品。我決定使用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?
您是否嘗試過使用一個GridView呢? – madatanic
...或一個ListView。 – BeemerGuy
他的轉發器沒有什麼問題..我不明白爲什麼他應該使用gridview或listview – Thousand