如何在中继器中找到总价

How Can I Find Total Price in the Repeater

我想查找购物车中的总价。我的"shop-cart.aspx"代码如下,如何在"shop-cart.aspx.cs"页面找到总价?

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>
        <tr>
            <td class="text-left"><%#Eval("productname") %></td>
            <td class="text-left"><%#Eval("price") %></td>
            <td class="product-subtotal text-left"><%#Eval("piece") %></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aytasarimConnectionString %>" SelectCommand="SELECT * FROM [cart] WHERE ([userid] = @userid)">
    <SelectParameters>
        <asp:SessionParameter Name="userid" SessionField="userid" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

我已经试过了,但出现空错误。

string totalprice;
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        totalprice += Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "price")) * Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "piece"));
        LblTotalPrice.Text = totalprice;
    }
}

在 ItemDataBound 方法之外创建一个变量。然后将每件商品的价格添加到变量中并显示结果。

decimal total = 0;

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DataRowView item = e.Item.DataItem as DataRowView;
    total += Convert.ToDecimal(item["itemid"]);

    Label1.Text = string.Format("{0:C}", total);
}