Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//this is my aspx page


XML
<%@ 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 file
C#
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;
                }
            }
        }
    }
}

Now i want to retain my all item on my session ["CART"]
how to do this ...
Thanks in Advance
Posted
Updated 5-Oct-13 2:19am
v2

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.
 
Share this answer
 
shopping-cart[^]

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900