2

特定の問題を解決する方法について頭を悩ませています。ユーザーが数量を設定したり、カートから特定のアイテムを削除したりできるショッピング カート ページを作成する必要があります。リピーターを利用することにしました。そして値はデータベースから取得されます

私のコードは次のとおりです

<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;
            }
        }
    }

私が一生懸命見つけようとしてきた何らかの理由で、最初のレコードは常に製品のサブ価格を計算しないということです. ただし、2 レコード目以降は正しく値が表示されます。何か間違っているのでしょうか、それとも Datalist を使用するように変更する必要がありますか?

4

1 に答える 1

1

ItemDataBound is called for each item in the list, so you'll want to limit your calculation to just that item, rather than all of the items, as you are doing now. Secondly, you appear to be fetching the values from the rendered data elements, rather than the data items themselves. If you can, calculate the totals on the server before binding to the Repeater (or GridView or ListView as others suggested).

于 2012-05-14T19:24:01.730 に答える