EnableEventValidation="false" CodeBehind="Addtocart.aspx.cs" Inherits="EPC2.Addtocart" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
.modal
{
position: fixed;
top: 0;
left: 0;
background-color: black;
z-index: 99;
opacity: 0.8;
filter: alpha(opacity=80);
-moz-opacity: 0.8;
min-height: 100%;
width: 100%;
}
.loading
{
font-family: Arial;
font-size: 10pt;
width: 200px;
height: 100px;
display: none;
position: fixed;
z-index: 999;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function ShowProgress() {
setTimeout(function () {
var modal = $('<div />');
modal.addClass("modal");
$('body').append(modal);
var loading = $(".loading");
loading.show();
var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
loading.css({ top: top, left: left });
}, 200);
}
$('form').live("submit", function () {
ShowProgress();
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<section id="cart_items">
<div class="container">
<div class="breadcrumbs">
<asp:LinkButton ID="LkBtnSelect" runat="server" class="btn btn-fefault cart"
onclick="LkBtnSelect_Click" > Go Back To Select More Products</asp:LinkButton>
<div class="table-responsive cart_info">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdeleting="GridView1_RowDeleting" onrowcommand="GridView1_RowCommand"
ShowFooter="True" class="table table-condensed">
<HeaderStyle CssClass="cart_menu" BorderColor="Orange"/>
<RowStyle BorderColor="White" />
<FooterStyle BorderColor="White" />
<Columns >
<asp:TemplateField HeaderText="Details" >
<ItemTemplate>
<span><asp:Label ID="lbltitle" runat="server" Text='<%# Eval("title") %>'></asp:Label></span>
(<span><asp:Label ID="lblcode" runat="server" Text='<%# Eval("product_code") %>'></asp:Label>
</span>)
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Price" >
<ItemTemplate>
<asp:Label ID="lblprice" runat="server" Font-Bold="True" class="quantity" Text='<%# Eval("our_price","${0}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txt_qty" runat="server" Enabled="false"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
Total:
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total" >
<FooterTemplate>
<asp:Label ID="lbl_total" runat="server" Font-Bold="True"></asp:Label>
</FooterTemplate>
<ItemTemplate>
$ <%# Eval("Total") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<%-- <asp:LinkButton ID="LkBtn" runat="server" CommandName="abc" class="btn btn-fefault cart">Update</asp:LinkButton>--%>
</FooterTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="delete" style="text-align:right"
ImageUrl="images/Delete.jpg" Height="24px" Width="24px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Left" />
</asp:GridView>
</div>
<asp:Label ID="lblempty" runat="server" Text=""></asp:Label>
</div></div></section>
<asp:ImageButton ID="imgbtn" runat="server" OnClick="btnpayal_Click" ImageUrl="~/images/btn_xpressCheckout.gif"
Style="display: block; margin-left: auto; margin-right: auto"></asp:ImageButton>
<div class="loading" align="center">
<br />
<br />
<img src="images/spinner.gif" alt="" />
</div>
</section >
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE_EPC2;
using BL_EPC2;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Text;
namespace EPC2
{
public partial class Addtocart : System.Web.UI.Page
{
public class ShoppingCart
{
public string Name { get; set; }
public string amount { get; set; }
public string quantity { get; set; }
}
SqlConnection con = new SqlConnection(); // for connection
SqlDataAdapter adp = new SqlDataAdapter(); // for fetch the records acc. to condition
DataTable dt = new DataTable(); // fill the fetched records
DataTable tb; // will store the session
TextBox txt_qty; // for Product Quantity
//Label lb_Footer_total = null; // for total Product Price
DataRowView r; // for Datarows
Label lbl_total = null;
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["DbEPC2"].ConnectionString;
try
{
con.Open();
}
catch
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
finally
{
con.Close();
}
if (Session["ss"] != null) // first attempt to check whether the session is null or not
{
}
else
{
//will create a temporary datatable for shopping cart
//*NOTE column name must be same as in database table
DataTable shop = new DataTable("Table");
// create a dataTable name shop ["Table"] always be same.
// declare the columns name according to your need.
shop.Columns.Add(new DataColumn("id", Type.GetType("System.Int32")));
shop.Columns.Add(new DataColumn("title", Type.GetType("System.String")));
shop.Columns.Add(new DataColumn("product_code", Type.GetType("System.String")));
shop.Columns.Add(new DataColumn("our_price", Type.GetType("System.Int32")));
shop.Columns.Add(new DataColumn("quantity", Type.GetType("System.Int32")));
shop.Columns.Add(new DataColumn("total", Type.GetType("System.Decimal")));
shop.Columns["total"].Expression = "our_price*quantity";
Session["ss"] = shop;
// storing in sessi on
}
if (!IsPostBack)
{
if (Session["Prodid"] != null)
{
adp = new SqlDataAdapter("select * from Product where PId=@id", con); // will fetch the record acc. to product id
adp.SelectCommand.Parameters.AddWithValue("@id", Convert.ToInt32(Session["Prodid"])); // query string of product id.
dt = new DataTable();
adp.Fill(dt);
adp.Dispose();
r = dt.DefaultView[0]; // creating DataRow
tb = (DataTable)(Session["ss"]);
// ***************** will append the same id with Quantity. it will not insert if the same product id request *************************//
Int32 a = tb.Rows.Count;
int i;
for (i = 0; i <= a - 1; i++)
{
Int32 t = Convert.ToInt32(tb.Rows[i][0]);
if (t == Convert.ToInt32(Session["Prodid"]))
{
// int k = 1;//Convert.ToInt32(Session["quantity"]);
int k = 1;
int k1 = Convert.ToInt32(tb.Rows[i][4]); // column no. of datatable for Qty.
k1 = k1 + k; // adding qty.
tb.Rows[i][4] = k1; // adding with previous value.
GridView1.DataSource = tb; // displaying the records in Gridview
GridView1.DataBind();
lbl_total = (Label)(GridView1.FooterRow.FindControl("lbl_total")); //*** binding footer control with total.
lbl_total.Text = tb.Compute("sum(total)", "").ToString();
grdview(); // refreshing the data in gridview
return;
}
}
DataRow r1; // declaring for Rows
r1 = tb.NewRow(); // assigning and creating the New Row
r1[0] = Convert.ToInt32(r["PId"]); // database columns name after fetching the records acc. to Product ID.
r1[1] = r["PName"].ToString();
r1[2] = Convert.ToInt32(r["PId"]);
r1[3] = Convert.ToInt32(r["PPrice"]);
//r1[4] = Session["PPrice"]; // here you can use your column name using session. coming from product page
r1[4] = 1; //default will be one
tb.Rows.Add(r1); // Adding the rows
GridView1.DataSource = tb; // displaying the records in Gridview
GridView1.DataBind();
for (int k = 0; k <= tb.Rows.Count - 1; k++)
{
txt_qty = (TextBox)(GridView1.Rows[k].FindControl("txt_qty")); // finding textbox for binding in G.View
txt_qty.Text = (tb.Rows[k][4].ToString()); //*** this the 6th column of Temp. table for manipulating the Quantity of products ***//
}
dt.Dispose();
lbl_total = (Label)(GridView1.FooterRow.FindControl("lbl_total")); //*** binding footer control with total.
lbl_total.Text = tb.Compute("sum(total)", "").ToString();
// Optional label out side the gridview.
// lbl_total.Text = tb.Compute("sum(total)", "").ToString(); // computing the total of all products
Session.Remove("Prodid");
}
else {
grdview();
}
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
tb = (DataTable)Session["ss"];
tb.Rows.RemoveAt(e.RowIndex); // deleting the current record in Temporary table
grdview();
}
catch
{ }
}
private void grdview()
{
tb = (DataTable)Session["ss"];
if (tb.Rows.Count == 0) // if no record found
{
lblempty.Text = "Your Shopping Cart is Empty";
Session.Clear();
imgbtn.Visible = false;
GridView1.DataSource = tb;
GridView1.DataBind();
}
else
{
GridView1.DataSource = tb;
GridView1.DataBind();
for (int k = 0; k <= tb.Rows.Count - 1; k++)
{
txt_qty = (TextBox)(GridView1.Rows[k].FindControl("txt_qty")); // finding drodown for binding in G.View
// you can use any control i have used TEXTBOX for Qty u can use DropDownlist etc.
txt_qty.Text = (tb.Rows[k][4].ToString()); //*** this the 6th column of Temp. table for manipulating the Quantity of products ***//
}
lbl_total = (Label)(GridView1.FooterRow.FindControl("lbl_total")); //*** binding footer control with total.
lbl_total.Text = tb.Compute("sum(total)", "").ToString();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "abc") // I have used GridView Rowcommand to UPDATE the Product from shopping cart.
{
tb = (DataTable)Session["ss"];
for (int i = 0; i <= tb.Rows.Count - 1; i++)
{
txt_qty = (TextBox)(GridView1.Rows[i].FindControl("txt_qty"));
tb.Rows[i][4] = txt_qty.Text; //*** this the 6th column of Temp. table for manipulating the Quantity of products ***//
}
grdview();
}
}
protected void btnpayal_Click(object sender, EventArgs e)
{
//imgprocess.Visible = true;
List<ShoppingCart> lstshp = new List<ShoppingCart>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
ShoppingCart be = new ShoppingCart();
Label pName = (Label)GridView1.Rows[i].FindControl("lbltitle");
Label pcode = (Label)GridView1.Rows[i].FindControl("lblcode");
Label pPrice = (Label)GridView1.Rows[i].FindControl("lblprice");
TextBox txtqty = (TextBox)(GridView1.Rows[i].FindControl("txt_qty"));
be.Name = pName.Text;
// be.= pDescription.Text;
be.amount = pPrice.Text;
be.quantity = txtqty.Text;
lstshp.Add(be);
}
//lstshp.Add(new ShoppingCart { Name = "Apple", amount = "05", quantity = "2" });
//lstshp.Add(new ShoppingCart { Name = "Mango", amount = "02", quantity = "1" });
Session["lst"] = lstshp;
List<ShoppingCart> list = (List<ShoppingCart>)Session["lst"];
StringBuilder sb = new StringBuilder();
sb.Append("cmd=_cart");
sb.Append("&first_name=EPC2-SOFTWARE");
sb.Append("&business=adeelasghar091-facilitator@gmail.com");
sb.Append("&button_subtype=services");
sb.Append("&upload=1");
sb.Append("&no_note=1");
sb.Append("&rm=2");
sb.Append("&return=http://localhost:1100/index.aspx");
sb.Append("&cancel_return=http://localhost:1100/index.aspx");
sb.Append("¤cy_code=USD");
int itemNumber = list.Count;
for (Int32 i = 0; i < list.Count; i++)
{
sb.AppendFormat("&item_name_{0}={1}", i + 1, list[i].Name);
sb.AppendFormat("&amount_{0}={1}", i + 1, list[i].amount);
sb.AppendFormat("&quantity_{0}={1}", i + 1, list[i].quantity);
}
Session.Clear();
Response.Redirect(@"https://www.sandbox.paypal.com/cgi-bin/webscr?" + sb.ToString());
}
protected void LkBtnSelect_Click(object sender, EventArgs e)
{
Response.Redirect("Index.aspx");
}
}
}
No comments :
Post a Comment