Preview:
Hello
Everybody,
Here I am
sharing you a shopping cart example with source code. Have a try on that, and
let me know your feedback on same.
.aspx code:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Cart.aspx.cs"
Inherits="Cart"
%>
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>Shopping
Carttitle>
<SCRIPT language=Javascript>
SCRIPT>
head>
<body>
<form id="form1" runat="server">
<div>
Item:
<asp:DropDownList ID="ddlItems" runat="server">
<asp:ListItem Value="Coke">Cokeasp:ListItem>
<asp:ListItem Value="7Up">7Upasp:ListItem>
<asp:ListItem Value="Mirinda">Mirindaasp:ListItem>
asp:DropDownList> <br />
Price:
<asp:TextBox ID="txtPrice" onkeypress="return isNumberKey(event)" runat="server">asp:TextBox><br />
Quantity:
<asp:TextBox ID="txtQuantity" onkeypress="return isNumberKey(event)" runat="server">asp:TextBox><br />
<asp:Button ID="btnAdd" runat="server"
Text="Add
to cart" onclick="btnAdd_Click"/>
<asp:GridView ID="gvCart" runat="server" DataKeyNames="Productid" AutoGenerateColumns="False"
ShowFooter="True"
onrowediting="gvCart_RowEditing" onrowupdating="gvCart_RowUpdating"
onrowdeleting="gvCart_RowDeleting" onrowdatabound="gvCart_RowDataBound">
<Columns>
<asp:BoundField DataField="Productid"
HeaderText="Product
Id" />
<asp:BoundField DataField="ProductName"
HeaderText="Product
Name" />
<asp:TemplateField HeaderText="Unit
Price" FooterStyle-Font-Bold="True">
<ItemTemplate>
<asp:Label ID="lblUnitPrice"
runat="server"
Text='<%#Eval("UnitPrice")%>'>asp:Label>
ItemTemplate>
<FooterStyle Font-Bold="True">FooterStyle>
asp:TemplateField>
<asp:TemplateField HeaderText="Quantity"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="txtQuantity"
runat="server"
onkeypress="return
isNumberKey(event)" Width="30" Text='<%#Eval("Quantity")
%>'>asp:TextBox>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Total"
FooterStyle-Font-Bold="True">
<ItemTemplate>
<asp:Label ID="lblTotalPrice"
runat="server"
Text='<%#Eval("TotalPrice")%>' >asp:Label>
ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblSubTotal"
runat="server"
>asp:Label>
FooterTemplate>
<FooterStyle Font-Bold="True">FooterStyle>
asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Update">asp:LinkButton>
ItemTemplate>
asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" OnClientClick="javascript:return confirm('Are you sure want to
delete record?');" Text="Delete">asp:LinkButton>
ItemTemplate>
asp:TemplateField>
Columns>
asp:GridView>
div>
form>
body>
html>
.asps.cs code:
using System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public partial class Cart : System.Web.UI.Page
{
protected decimal TotalAmt;
DataTable
dt = new DataTable();
DataRow
dr;
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
}
}
protected void btnAdd_Click(object
sender, EventArgs e)
{
BindCart();
}
protected void gvCart_RowEditing(object
sender, GridViewEditEventArgs e)
{
gvCart.EditIndex = e.NewEditIndex;
gvCart.Rows[e.NewEditIndex].Cells[1].Enabled = false;
BindCart();
}
protected void BindCart()
{
if
(ViewState["myCart"] == null)
{
dt.Columns.Add("Productid", System.Type.GetType("System.Int32"));
dt.Columns.Add("ProductName");
dt.Columns.Add("UnitPrice", System.Type.GetType("System.Decimal"));
dt.Columns.Add("Quantity", System.Type.GetType("System.Int32"));
dt.Columns.Add("TotalPrice", System.Type.GetType("System.Decimal"));
dt.Columns.Add("SubTotal", System.Type.GetType("System.Decimal"));
dr = dt.NewRow();
dr[0] = dt.Rows.Count + 1;
dr[1] = ddlItems.SelectedItem.Text;
dr[2] = txtPrice.Text;
dr[3] = txtQuantity.Text;
dr[4] = Convert.ToDecimal(txtPrice.Text)
* Convert.ToDecimal(txtQuantity.Text);
dt.Rows.Add(dr);
}
else
{
dt = (DataTable)ViewState["myCart"];
dr = dt.NewRow();
dr[0] = dt.Rows.Count + 1;
dr[1] = ddlItems.SelectedItem.Text;
dr[2] = txtPrice.Text;
dr[3] = txtQuantity.Text;
dr[4] = Convert.ToDecimal(txtPrice.Text)
* Convert.ToDecimal(txtQuantity.Text);
dt.Rows.Add(dr);
}
ViewState["myCart"]
= dt;
gvCart.DataSource = dt;
gvCart.DataBind();
}
protected void gvCart_RowUpdating(object
sender, GridViewUpdateEventArgs e)
{
dt = (DataTable)ViewState["myCart"];
TextBox
txtQty = (TextBox)gvCart.Rows[e.RowIndex].FindControl("txtQuantity");
Label
lblUnitPrice = (Label)gvCart.Rows[e.RowIndex].FindControl("lblUnitPrice");
decimal
unitPrice = Convert.ToDecimal(lblUnitPrice.Text);
dt.Rows[e.RowIndex].BeginEdit();
dt.Rows[e.RowIndex]["Quantity"] = txtQty.Text;
dt.Rows[e.RowIndex]["TotalPrice"] = Convert.ToDecimal(txtQty.Text)
* unitPrice;
dt.Rows[e.RowIndex].EndEdit();
gvCart.DataSource = dt;
gvCart.DataBind();
}
protected void gvCart_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
dt = (DataTable)ViewState["myCart"];
dt.Rows[e.RowIndex].Delete();
gvCart.DataSource = dt;
gvCart.DataBind();
}
protected void gvCart_RowDataBound(object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
TotalAmt += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem,
"TotalPrice"));
}
if
(e.Row.RowType == DataControlRowType.Footer)
{
Label
lblSubTotal = (Label)e.Row.FindControl("lblSubTotal");
lblSubTotal.Text =
TotalAmt.ToString();
}
}
}
I hope
you will enjoy. Please share your view/ comments to make this blog more
reliable...
Thanks
you again.
Comments