将转发器中的数据保存到单行SQL中的数据库中

时间:2017-09-22 11:19:11

标签: c# sql asp.net repeater

我在ASP.NET中面临一个小问题。我将在下面详细解释。

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

enter image description here 我的设计代码:



<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>
&#13;
&#13;
&#13;

在浏览器上查看: enter image description here 我的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();
}

如何显示两个或更多产品的总价格?有什么想法吗?

0 个答案:

没有答案