如何维护购物车 [英] How To Maintain Shopping Cart

查看:102
本文介绍了如何维护购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//这是我的aspx页面





//this is my aspx page


<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/Main.master" AutoEventWireup="true"

    CodeFile="ShowProduct.aspx.cs" Inherits="Guest_ShowProduct" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <center>
        <table width="100%">
            <asp:DataList ID="dtlist" runat="server" RepeatColumns="4" RepeatDirection="Horizontal"

                OnItemCommand="dtlist_ItemCommand" OnItemDataBound="dtlist_ItemDataBound">
                <ItemTemplate>
                    <table>
                        <tr>
                            <td>
                                <asp:ImageButton ID="imgPic" runat="server" Height="150px" Width="150px" CommandName="Cart"

                                    ImageUrl='<%# Eval("image") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblid" runat="server" Text='<%# Eval("uniqueid")%>'></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("productName")%>'></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="lblPrice" runat="server" Text='<%#  Eval("Amount") %>'></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:LinkButton ID="lnkAdd" runat="server" PostBackUrl="~/Guest/AddToCart.aspx">Add To Cart</asp:LinkButton>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
        </table>
    </center>
</asp:Content>





.CS文件



.CS file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Guest_ShowProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showproduct();
           
        }

        

    }
    public void showproduct()
    {
        d_ProductDetails d = new d_ProductDetails();
        DataTable dt = new DataTable();
        dt = d.getProduct();
        if (dt != null && dt.Rows.Count > 0)
        {
            dtlist.DataSource = dt;
            dtlist.DataBind();
        }
    }
    public void collectItem()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ProductID", typeof(string));
        dt.Columns.Add("ProductName", typeof(string));
        dt.Columns.Add("ProductCost", typeof(string));
        dt.Columns.Add("ProductImage", typeof(string));
        Session["collect"] = dt;

    }

    protected void dtlist_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        
    }
    public void cart()
    { 
    
    }
    protected void dtlist_ItemCommand(object source, DataListCommandEventArgs e)
    {
         DataTable dt = (DataTable)Session["collect"];
        if (e.CommandName == "Cart")
        {
            if (Session["collect"] == null)
            {
                DataRow dr = dt.NewRow();
                DataList rowSelect = (DataList)(((ImageButton)e.CommandSource).NamingContainer);
                Label Product = (Label)rowSelect.FindControl("lblProductName");
                Label price = (Label)rowSelect.FindControl("lblPrice");
                Label id = (Label)rowSelect.FindControl("lblid");
            
                dr["ProductID"] = id.Text;
                dr["ProductCost"] = price.Text;
                dr["ProductName"] = Product.Text;
                
                dt.Rows.Add(dr);

                Session["collect"] = dt;
            }
            else
            {
                var cart = (List<string>)Session["Cart"];
                foreach (var product in cart)
                {
                    cart.Add(product);
                    Session["Cart"] = cart;
                }
            }
        }
    }
}



现在我想保留我的所有项目session [CART]

怎么做...

先谢谢


Now i want to retain my all item on my session ["CART"]
how to do this ...
Thanks in Advance

推荐答案

你好,

您可以通过使用System.Data.DataTable类使用临时表来完成此操作意味着您可以创建一个包含所有项目的数据表。现在如果你想保留这些项目直到你登录,那么只需将这个数据表分配到会话变量中,每当你想使用这个数据表时,只需将Session变量转换为数据表并再次使用这个数据表。

try它会起作用。
Hi,
You can do this by using temporary table by using System.Data.DataTable class means you can create a datatable which holds your all items. now if you want to retain these items till you are logged in then just assign this datatable into session variable and whenever you want to use this datatable just cast Session variable into datatable and use this datatable again.
try this it will worked.


shopping-cart [ ^ ]



查看链接..这将让您了解如何维护购物车..
shopping-cart[^]

Check the link..this will give you the idea how to maintain the cart..


这篇关于如何维护购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆