首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将复发器中的数据保存到单行

如何将复发器中的数据保存到单行
EN

Stack Overflow用户
提问于 2017-09-21 13:35:26
回答 1查看 207关注 0票数 0
  1. 我正在将tbl_ShopCart中的产品保存到tbl_Order中
  2. 有可能有多个产品,因为我使用的商店购物车中继器。
  3. 例如:我的购物车有两个产品,我按下按钮完成购物,我已经为tbl_Order保存了数据。但这两种产品的总价格是分开记录的。我想用一条线显示两种产品的总价。

我的aspx.cs代码:

代码语言:javascript
复制
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();
}

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

代码语言:javascript
复制
<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>

我怎么能这么做,有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2017-09-21 14:30:03

试着做这样的事情:

代码语言:javascript
复制
    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") + "')");
            }

        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46345426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档