The Concept of Shopping Cart in ASP.NET with C#


Few of my friends were asking me about the creation of Web cart and use of footer template in GridView. I remember the days of asp 3.0 when we use to make web cart and it use to take many days.

 

I have taken a GridView, Dropdown box and a Button. The name of my page is default.aspx and default.cs. This is just concept. It is not full Web Cart. You can just use the concept and implement it according to your need. I have also highlighted the need of footer template.

 

Copy the following code in ASPX Page.

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Web Cart </title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        &nbsp;<table>

            <tr>

                <td style="width: 100px">

                    <asp:DropDownList ID="DropDownList1" runat="server" Width="198px">

                        <asp:ListItem Value="25.50">LUX</asp:ListItem>

                        <asp:ListItem Value="45.60">MEDIMAX</asp:ListItem>

                        <asp:ListItem Value="34.75">CIBACA</asp:ListItem>

                        <asp:ListItem Value="43.50">CLOSEUP</asp:ListItem>

                    </asp:DropDownList></td>

                <td style="width: 100px">

                    <asp:Button ID="btnAdd" runat="server" Text="Add to Cart" OnClick="btnAdd_Click" /></td>

            </tr>

            <tr>

                <td colspan="2">

                </td>

            </tr>

            <tr>

                <td colspan="2">

        <asp:GridView ID="GridView1"

  ShowFooter="true" DataKeyNames="ProductId"

  AutoGenerateColumns="false" runat="server">

<Columns>

<asp:BoundField DataField="Productid" HeaderText="Product Id" />

<asp:BoundField DataField="ProductName" FooterText="Total" HeaderText="Product Name" />

<asp:TemplateField HeaderText="Unit Price" FooterStyle-Font-Bold="True">

<ItemTemplate>

  <%# GetUnitPrice(decimal.Parse(Eval("UnitPrice").ToString())).ToString("N2") %>

</ItemTemplate>

<FooterTemplate>

  <%# GetTotal().ToString("N2") %>

</FooterTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

                </td>

            </tr>

        </table>

 

    </div>

    </form>

</body>

</html> 

 

And following code in .CS File 

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

       

    }

   

    private void DataTable()

    {

        DataTable MyDT = new DataTable();

        DataRow MyRow;

        if (ViewState["DataTable"] == null)

        {

MyDT.Columns.Add("Productid",System.Type.GetType("System.Int32"));

MyDT.Columns.Add("ProductName");

MyDT.Columns.Add("UnitPrice", System.Type.GetType("System.Decimal"));

MyRow = MyDT.NewRow();

MyRow[0] = MyDT.Rows.Count + 1;

MyRow[1] = DropDownList1.SelectedItem.Text;

MyRow[2] = DropDownList1.SelectedItem.Value;

MyDT.Rows.Add(MyRow);

        }

        else

        {

            MyDT = (DataTable)ViewState["DataTable"];

            MyRow = MyDT.NewRow();

            MyRow[0] = MyDT.Rows.Count + 1;

            MyRow[1] = DropDownList1.SelectedItem.Text;

            MyRow[2] = DropDownList1.SelectedItem.Value;

            MyDT.Rows.Add(MyRow);

        }

        ViewState["DataTable"] = MyDT;

        GridView1.DataSource = MyDT;

        GridView1.DataBind();

    }

   

    protected decimal TotalUnitPrice;

    protected decimal GetUnitPrice(decimal Price)

    {

        TotalUnitPrice += Price;

        return Price;

    }

    protected decimal GetTotal()

    {

        return TotalUnitPrice;

    }

    protected void btnAdd_Click(object sender, EventArgs e)

    {

        DataTable();

    }

}

Up Next
    Ebook Download
    View all
    Learn
    View all