Shopping Cart Project Using ASP.Net in C#

In this project, you will learn how to make a shopping cart project using ASP.Net in C#. Make a shopping cart project using C# to check how the add cart button work.



Shopping Cart Project using C#
Shopping Cart Project



Add.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Add.aspx.cs" Inherits="Shopping_Cart.Add" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
         a {text-decoration: none;

         }
         body {
            background-size: cover;
        }

     </style>
</head>
<body background="pics/bg.jpg">
    <form id="form1" runat="server">
    <div>
    <table align="center" style="margin-top:60px" cellpadding="10">
      <tr>
       <td>
           <asp:HyperLink ID="HyperLink1" runat="server" Font-Size="Large"
               NavigateUrl="~/Default.aspx">Home</asp:HyperLink>
       </td>
       <td>
           <asp:HyperLink ID="HyperLink2" runat="server" Font-Size="Large"
               NavigateUrl="~/AddProduct.aspx">Add Product</asp:HyperLink>
       </td>
       <td>
           <asp:HyperLink ID="HyperLink3" runat="server" Font-Size="Large"
               NavigateUrl="~/ShowProduct.aspx">Show Product</asp:HyperLink>
       </td>
      </tr>
     </table>
     <hr />
     <h2 style='text-align:center;margin-top:100px'>Product added to the cart</h2>
    </div>
    </form>
</body>
</html>


Add.aspx.cs

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

namespace Shopping_Cart
{
    public partial class Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}




AddProduct.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddProduct.aspx.cs" Inherits="Shopping_Cart.AddProduct" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">    <title> JACH Shopping Product</title>
    <style>
         a {text-decoration: none;

         }
        body {
            background-size: cover;
        }

     </style>
</head>
<body background="pics/bg.jpg">
    <form id="form1" runat="server">
    <div>
    <!---Menu code here--->
    <table align="center" Style= " margin-top:60px" cellpadding="19">
    <tr>
    <td>
        <asp:HyperLink ID="HyperLink1" runat="server" Font-Size ="Large" NavigateUrl="~/Default.aspx">HOME</asp:HyperLink>
    </td>
    <td>
    <asp:HyperLink ID="HyperLink2" runat="server" Font-Size="Large" NavigateUrl="~/AddProduct.aspx" >ADD PRODUCT</asp:HyperLink>
    </td>
    <td>
    <asp:HyperLink ID="HyperLink3" runat="server" Font-Size ="Large" NavigateUrl="~/ShowProduct.aspx">SHOW PRODUCT</asp:HyperLink>
 
    </td>
    </tr>
    </table>
 <hr /><!---Menu code end here--->

 <table align="center" style="margin-top :40px; font-size :18px;" cellpadding="10">
 <tr>
 <td>
 Select Product
 </td>

 <td>
     <asp:DropDownList ID="DropDownList1" runat="server" Font-Size ="Large"
         AutoPostBack="True" Width="139px"
         onselectedindexchanged="DropDownList1_SelectedIndexChanged">
         <asp:ListItem>---Select---</asp:ListItem>
         <asp:ListItem>Laptop</asp:ListItem>
         <asp:ListItem>Desktop</asp:ListItem>
         <asp:ListItem>Monitor</asp:ListItem>
         <asp:ListItem>Keyboard</asp:ListItem>
         <asp:ListItem>Mouse</asp:ListItem>
     </asp:DropDownList>
  </td>
 </tr>

 <tr>
 <td colspan="2" align="center">
     <asp:Image ID="Image1" runat="server" Width="150px" />
 </td>
 </tr>

 <tr>
 <td colspan="2" align="center">
     <asp:Label ID="Label1" runat="server"  Font-Bold ="True"></asp:Label>
 </td>
  </tr>
 <tr>
  <td colspan="2" align="center">
     <asp:Button ID="Button1" runat="server" Text="ADD TO CART" onclick="Button1_Click" Visible="False" />
 </td>
 </tr>
 </table>
 
 </div>
  </form>
</body>
</html>



AddProduct.aspx.cs

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

namespace Shopping_Cart
{
    public partial class AddProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] price = { "", "28000", "18000", "7000", "400", "250", };
            Image1.ImageUrl = "pics/" + DropDownList1.Text + ".jpg";
            int i = DropDownList1.SelectedIndex;
            Label1.Text = "Rs." + price[i];
            Button1.Visible = true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ItemAdded(DropDownList1.Text))
            {
                Response.Write("<script>alert('selected item is already added in cart')</script>");
                return;
            }
            Session["ctr"] = (int)Session["ctr"] + 1;
            Product proj = new Product(DropDownList1.Text, Image1.ImageUrl, Label1.Text);
            Session["p" + Session["ctr"]] = proj;
            Server.Transfer("Add.aspx");
        }
        bool ItemAdded(string name)
        {
            bool found = false;
            for (int c = 1; c <= (int)Session["ctr"]; c++)
            {
                Product p = (Product)Session["p" + c];
                if (name == p.name)
                {
                    found = true;
                    break;
                }
            }
            return found;

        }

    }
}




Default.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
 <head id="Head1" runat="server">    <title> JACH Shopping</title>
     <style>
         a {text-decoration: none;

         }
   

     </style>
 </head>
    <body background="pics/bggg.jpg">

    <form id="form1" runat="server">
    <div>
 <table align="center" Style= " margin-top:60px" cellpadding="19">
    <tr>
    <td>
 
        <asp:HyperLink ID="HyperLink1" runat="server" Font-Size ="Large" NavigateUrl="~/Default.aspx">HOME</asp:HyperLink>
    </td>
    <td>
    <asp:HyperLink ID="HyperLink2" runat="server" Font-Size="Large" NavigateUrl="~/AddProduct.aspx" >ADD PRODUCT</asp:HyperLink>
    </td>
    <td>
    <asp:HyperLink ID="HyperLink3" runat="server" Font-Size ="Large" NavigateUrl="~/ShowProduct.aspx">SHOW PRODUCT</asp:HyperLink>
    </td>
    </tr>
 </table>
     
 <hr />
     

 <h1 style="text-align :center; margin-top:70px;"> Welcome to Online Shopping Cart</h1>
        <h3 style="text-align :center; margin-top:70px;">
        <p>We Provide Excellent Quality Product</p>
            <br />
            <br />
            <br />

<h2 style="text-align :center; margin-top:70px;">
<p><font color="black"> We always maintain a good relationship with the customer.</font></p>
            </h2>
    </div>
    </form>
</body>
</html>



Default.aspx.cs

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

namespace Shopping_Cart
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}


Global.asax

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace Shopping_Cart
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Session["ctr"] = 0; 
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}


ShowProduct.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowProduct.aspx.cs" Inherits="Shopping_Cart.ShowProduct" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> JACH Shopping ShowProduct</title>

    <style>
         a {text-decoration: none;

         } 
         body {
            background-size: cover;
        }

     </style>
</head>
   <body background="pics/bg.jpg">
 <form id="form1" runat="server">
    <div>
    <table align="center" Style= " margin-top:60px" cellpadding="19">
    <tr>
    <td>
        <asp:HyperLink ID="HyperLink1" runat="server" Font-Size ="Large" NavigateUrl="~/Default.aspx">HOME</asp:HyperLink>
    </td>
    <td>
    <asp:HyperLink ID="HyperLink2" runat="server" Font-Size="Large" NavigateUrl="~/AddProduct.aspx" >ADD PRODUCT</asp:HyperLink>
    </td>
    <td>
    <asp:HyperLink ID="HyperLink3" runat="server" Font-Size ="Large" NavigateUrl="~/ShowProduct.aspx">SHOW PRODUCT</asp:HyperLink>
    
    </td>
    </tr>
    </table>
 <hr />

  <table align="center" cellpadding="10">
       <tr>
         <td>
             <asp:Button ID="Button1" runat="server" Text="&lt;&lt;" Width="60px" 
                 onclick="Button1_Click" Visible="False" />
         </td>
         <td>
             <asp:Button ID="Button2" runat="server" Text="&gt;&gt;" Width="60px" 
                 onclick="Button2_Click" Visible="False" />
         </td>
          <td>
              <asp:Label ID="Label4" runat="server"></asp:Label>
          </td>
       </tr>
     </table>

     <center style="margin-top:60px">
         <asp:Label ID="Label3" runat="server" Font-Names="Algerian" Font-Size="Large" Visible="False">No product in the Cart</asp:Label>
     </center>


     <table align="center" style="margin-top:50px;font-size:18px" cellpadding="10" runat="server" id="pd"> 
      <tr>
       <td>Product name</td>
       <td>
           <asp:Label ID="Label2" runat="server" Font-Size="Large" ForeColor="Blue"></asp:Label>
       </td>
      </tr>

      <tr>
       <td>Product photo</td>
       <td>
           <asp:Image ID="Image1" runat="server" Width="100px" />
       </td>
      </tr>

      <tr>
       <td>Product price</td>
       <td>
           <asp:Label ID="Label1" runat="server" Font-Bold="False" Font-Size="Large" 
               ForeColor="Blue"></asp:Label>
       </td>
      </tr>
      </table>


    </div>
    </form>
        </body>
</html>




ShowProduct.aspx.cs

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

namespace Shopping_Cart
{
    public partial class ShowProduct : System.Web.UI.Page
    {
        static int c;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["p1"] == null)
                {
                    pd.Visible = false;
                    Label3.Visible = true;
                    return;
                }
                Button1.Visible = true;
                Button2.Visible = true;
                c = 1;
                showItem(c);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (c == 1)
            {
                Response.Write("<script>alert('this is first product')</script>");
                return;
            }
            showItem(--c);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (c == (int)Session["ctr"])
            {
                Response.Write("<script>alert('this is LAST product')</script>");
                return;
            }
            showItem(++c);
        }
        void showItem(int pn)
        {
            Label4.Text = pn + "of" + Session["ctr"];
            Product pd = (Product)Session["p" + pn];
            Label2.Text = pd.name;
            Image1.ImageUrl = pd.image;
            Label1.Text = pd.price;

        }
    }
}



Home Page:

Shopping Cart Home Page



AddProduct Page:




ShowProduct Page:


Post a Comment

1 Comments