我一直在探讨如何解决某个特定问题。我需要创建一个购物车页面,用户可以在其中设置数量或从购物车中删除该特定商品。我决定使用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?
答案 0 :(得分:1)
ItemDataBound
,因此您需要将计算限制为该项目,而不是所有项目,就像您现在所做的那样。
其次,您似乎从呈现的数据元素而不是数据项本身获取值。如果可以,请在绑定到Repeater
之前计算服务器上的总计(或其他人建议的GridView
或ListView
)。
答案 1 :(得分:0)
disculpa pero mi ingles no es muy bueno pero puedes hacer esto
Dim按钮As Button = CType(发件人,按钮)
'Get the command argument
Dim commandArgument As String = button.CommandArgument
'Get the Repeater Item reference
Dim item As RepeaterItem = CType(button.NamingContainer, RepeaterItem)
'Get the repeater item index
Dim index As Integer = item.ItemIndex
Dim cantidad As TextBox = (CType(MyList.Items(index).FindControl("Quantity"), TextBox))
Dim contador As Integer = 0
contador = Convert.ToInt32(cantidad.Text)
contador += 1
' 'Dim countryName As String = CountryLabel.Text
cantidad.Text = contador.ToString()