如何将中继器中的数据保存到单行

时间:2017-09-21 13:35:26

标签: c# sql asp.net repeater

  1. 我将tbl_ShopCart中的产品保存到tbl_Order
  2. 可以有多个产品,因为我使用中继器购物车。
  3. 例如:我的购物车有2个产品,我按下按钮完成购物,我保存了tbl_Order的数据。但是这两种产品的总价格是分开记录的。我想在两行产品的一行中显示总价格。
  4. My Database

    我的aspx.cs代码:

    protected void BtnCompleteOrder_Click(object sender, EventArgs e)
    {
        foreach(RepeaterItem item in Repeater1.Items)
        {
            if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                Label lblproductid = (Label)item.FindControl("LblProductID");
                Image imgproductimage = (Image)item.FindControl("ImgProductImage");
                Label lblproductname = (Label)item.FindControl("LblProductName");
                Label lblprice = (Label)item.FindControl("LblPrice");
                Label lblpiece = (Label)item.FindControl("LblPiece");
                function.cmd("INSERT INTO tbl_Order(userid, productid, name, surname, email, identificationnumber, phone, productimage, productname, piece, cargo, totalprice, paymenttype, orderdate) VALUES('" + Session["userid"] + "', '" + lblproductid.Text + "', '" + Session["name"] + "', '" + Session["surname"] + "', '" + Session["email"] + "', '" + Session["identificationnumber"] + "', '" + Session["phone"] + "', '" + imgproductimage.ImageUrl + "', '" + lblproductname.Text + "', '" + lblpiece.Text + "', '" + Session["cargo"] + "', '" + LblTotalPrice.Text + "', '" + DrpDwnPaymentType.Text + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')");
            }
        }
    }
    
    // I add the cargo price on top of the total price, which gives us the lasttotal.
    decimal total = 0;
    decimal lasttotal = 0;
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView item = e.Item.DataItem as DataRowView;
        total += Convert.ToDecimal(item["price"]) * Convert.ToDecimal(item["piece"]);
        LblShopCartTotal.Text = total.ToString();
    
        lasttotal = total + Convert.ToDecimal(LblCargo.Text);
        LblTotalPrice.Text = lasttotal.ToString();
    }
    

    这是我的设计代码和设计窗口:

    <table class="table shop-cart text-center">
        <thead>
            <tr>
                <th class="first"></th>
                <th class="text-left text-uppercase font-weight-600 letter-spacing-2 text-small black-text">Product Name</th>
                <th class="text-left text-uppercase font-weight-600 letter-spacing-2 text-small black-text">Price</th>
                <th class="text-left text-uppercase font-weight-600 letter-spacing-2 text-small black-text">Piece</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">
                <ItemTemplate>
                    <tr>
                        <asp:Label ID="LblShopCartID" runat="server" Text='<%#Eval("shopcartid") %>' Visible="false"></asp:Label>
                        <asp:Label ID="LblProductID" runat="server" Text='<%#Eval("productid") %>' Visible="false"></asp:Label>
                        <td class="product-thumbnail text-left"><asp:Image ID="ImgProductImage" runat="server" Height="150px" Width="150px" ImageUrl='<%#Eval("productimage") %>' /></td>
                        <td class="text-left"><asp:Label ID="LblProductName" runat="server" Text='<%#Eval("productname") %>'></asp:Label></td>
                        <td class="text-left"><asp:Label ID="LblPrice" runat="server" Text='<%#Eval("price") %>'></asp:Label> TL</td>
                        <td class="product-subtotal text-left"><asp:Label ID="LblPiece" runat="server" Text='<%#Eval("piece") %>'></asp:Label></td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aytasarimConnectionString %>" SelectCommand="SELECT * FROM [tbl_ShopCart] WHERE ([userid] = @userid)">
                <SelectParameters>
                    <asp:SessionParameter Name="userid" SessionField="userid" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>
        </tbody>
    </table>
    <table class="table cart-total">
        <tbody>
            <tr>
                <th class="padding-two text-right no-padding-right text-uppercase font-weight-600 letter-spacing-2 text-small xs-no-padding">Shop Cart Total: </th>
                <td class="padding-two text-uppercase text-right no-padding-right font-weight-600 black-text xs-no-padding"><asp:Label ID="LblShopCartTotal" Text="" runat="server"></asp:Label> ₺</td>
            </tr>
            <tr>
                <th class="padding-two text-right no-padding-right text-uppercase font-weight-600 letter-spacing-2 text-small xs-no-padding">Cargo Price: </th>
                <td class="padding-two text-uppercase text-right no-padding-right font-weight-600 black-text text-small xs-no-padding"><asp:Label ID="LblCargoPrice" runat="server" Text=""></asp:Label> ₺</td>
            </tr>
            <tr class="total">
                <th class="padding-two text-uppercase text-right no-padding-right font-weight-600 text-large xs-no-padding">Last Total: </th>
                <td class="padding-two text-uppercase text-right no-padding-right font-weight-600 black-text text-large no-letter-spacing xs-no-padding"><asp:Label ID="LblTotalPrice" runat="server" Text=""></asp:Label> ₺</td>
            </tr>
        </tbody>
    </table>
    

    enter image description here 我怎么能这样做,任何想法?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

    private class Product
    {
        public string Productid { get; set; }
        public decimal Piece { get; set; }
        public decimal Price { get; set; }

        // TODO: Add name etc...
    }

    protected void BtnCompleteOrder_Click(object sender, EventArgs e)
    {
        var products = new Dictionary<string, Product>();
        foreach (RepeaterItem item in Repeater1.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                Label lblproductid = (Label)item.FindControl("LblProductID");
                Image imgproductimage = (Image)item.FindControl("ImgProductImage");
                Label lblproductname = (Label)item.FindControl("LblProductName");
                Label lblprice = (Label)item.FindControl("LblPrice");
                Label lblpiece = (Label)item.FindControl("LblPiece");

                var currentProductid = lblproductid.Text;
                var currentPiece = Convert.ToDecimal(lblpiece);

                if (products.ContainsKey(currentProductid))
                {
                    products[currentProductid].Piece += currentPiece;
                }
                else
                {
                    products.Add(currentProductid, new Product
                    {
                        Piece=currentPiece,
                    }
                    );
                }

            }

            foreach (var currentProduct in products.Values)
            {
                function.cmd("INSERT INTO tbl_Order(userid, productid, name, surname, email, identificationnumber, phone, productimage, productname, piece, cargo, totalprice, paymenttype, orderdate) VALUES('" + Session["userid"] + "', '" 
                    + currentProduct.Productid


                    //TODO change all below etc....
                    + "', '" + Session["name"] + "', '" + Session["surname"] + "', '" + Session["email"] + "', '" + Session["identificationnumber"] + "', '" + Session["phone"] + "', '" + imgproductimage.ImageUrl + "', '" + lblproductname.Text + "', '" + lblpiece.Text + "', '" + Session["cargo"] + "', '" + LblTotalPrice.Text + "', '" + DrpDwnPaymentType.Text + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')");
            }

        }
    }