You are on page 1of 9

Carrito de Compras

Diciembre 8, 2011

venta.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="venta.aspx.cs" Inherits="venta" %> <!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></title> <style type="text/css"> .texto1{text-align:right} </style> <script type="text/javascript"> function borra(c) { if (confirm("Desea borrar el item cuyo codigo es : " + c)) {

document.forms[0].submit(); } else { return false; } } </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>Codigo</td> <td><asp:TextBox ID="t1" runat="server" Width="50"/></td> </tr> <tr> <td>Producto</td> <td><asp:TextBox ID="t2" runat="server" Width="250" /></td> </tr> <tr> <td>Precio</td> <td><asp:TextBox ID="t3" runat="server" /></td> </tr> <tr> <td>Unidades</td> <td><asp:TextBox ID="t4" runat="server" /></td> </tr> <tr> <td colspan="2"> <asp:Button ID="b2" runat ="server" Text="Aceptar" onclick="b2_Click"/> <asp:Button ID="b3" runat ="server" Text="Ver Carrito" onclick="b3_Click" /> </td> </tr> </table> </div> <div> <asp:GridView runat="server" ID="gvCart" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Width="499px" onrowcommand="gvCart_RowCommand" onrowdatabound="gvCart_RowDataBound" onprerender="gvCart_PreRender"> <Columns> <asp:BoundField DataField="Codpro" HeaderText="Codigo" />

<asp:BoundField DataField="Producto" HeaderText="Producto" /> <asp:BoundField DataField="Precio" HeaderText="Precio" > <ItemStyle HorizontalAlign="Center" /> </asp:BoundField> <asp:TemplateField HeaderText="Cantidad"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Cantidad") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Height="19px" Text='<%# Bind("Cantidad") %>' Width="55px" CssClass="texto1"></asp:TextBox> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:BoundField DataField="Total" HeaderText="Total" /> <asp:TemplateField> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:ImageButton ID="ImageButton1" runat="server" CommandName="b1" ImageUrl="~/images/actualizar.gif" /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:ImageButton ID="ImageButton2" runat="server" CommandName="b2" ImageUrl="~/images/borrar.gif" /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> </Columns> <FooterStyle BackColor="White" ForeColor="#000066" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#007DBB" />

<SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#00547E" /> </asp:GridView> </div> <div><asp:Literal runat="server" ID="pago" /></div> </form> </body> </html> ventas.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Capa_negocios; using Capa_Entidades; public partial class venta : System.Web.UI.Page { controller cart = new controller(); protected void b2_Click(object sender, EventArgs e) { int c = int.Parse(this.t1.Text); string pro = this.t2.Text; double pre = double.Parse(t3.Text); int q = int.Parse(t4.Text); List<items> ocar = null; if (Session["cart"] == null) { ocar = new List<items>(); } else { ocar = (List<items>)Session["cart"]; } items car= cart.add_To_cart(c,pro,pre,q,pre*q); ocar.Add(car);

Session["cart"] = ocar; t1.Text = ""; t2.Text = ""; t3.Text = ""; t4.Text = ""; t1.Focus(); } protected void b3_Click(object sender, EventArgs e) { List<items> ocar = (List<items>)Session["cart"]; this.gvCart.DataSource = ocar; this.gvCart.DataBind(); this.pago.Text = "Total S/. : " + totalpago().ToString("C"); } protected void gvCart_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "b1") { //actualizar int f=Convert.ToInt32(e.CommandArgument.ToString()); //indice de la fila GridViewRow fila = this.gvCart.Rows[f]; TextBox txtq = (TextBox)fila.FindControl("TextBox2"); int q= Convert.ToInt32(txtq.Text); int cod = Convert.ToInt32(fila.Cells[0].Text); //codigo del producto double pre = Convert.ToDouble(fila.Cells[2].Text); List<items> cart = (List<items>)Session["cart"]; cart[f].Cantidad = q; cart[f].Total = pre * q; double subtot = pre * q; fila.Cells[4].Text = subtot.ToString("C"); Session["cart"] = cart; this.pago.Text = "Total S/. : " + totalpago().ToString(); } if (e.CommandName == "b2") { //borrar } }

double totalpago() { double mtotal = 0; List<items> cart = (List<items>)Session["cart"]; for (int j = 0; j < cart.Count;j++ ) { mtotal += cart[j].Total; } return mtotal; } protected void gvCart_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType==DataControlRowType.DataRow) { ImageButton b1 = (ImageButton)e.Row.FindControl("ImageButton1"); if (b1.CommandName == "b1") { b1.CommandArgument = e.Row.RowIndex.ToString(); } } } protected void gvCart_PreRender(object sender, EventArgs e) { GridViewRow row; ImageButton btn; for (int i = 0; i < this.gvCart.Rows.Count;i++ ) { row = this.gvCart.Rows[i]; if (row.RowType == DataControlRowType.DataRow) { int c=Convert.ToInt32(row.Cells[0].Text); btn = (ImageButton)row.FindControl("ImageButton2"); btn.Attributes.Add("onclick", "return borra(" + c + ")"); } } } } items.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Capa_Entidades { public class items { private int codpro; public int Codpro { get { return codpro; } set { codpro = value; } } private string producto; public string Producto { get { return producto; } set { producto = value; } } private double precio; public double Precio { get { return precio; } set { precio = value; } } private int cantidad; public int Cantidad { get { return cantidad; } set { cantidad = value; } } private double total; public double Total { get { return total; } set { total = value; } } public items() {

} public items(int codpro,string producto,double precio,int cantidad,double total) { this.codpro = codpro; this.producto = producto; this.precio = precio; this.cantidad = cantidad; this.total = total; } } } controller.cs namespace Capa_negocios { public class controller {

public items add_To_cart(int codpro, string prod, double precio, int cantidad, double total) { return new carrito().addpro(codpro,prod,precio,cantidad,total); } } } carrito.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using Capa_Entidades; namespace Capa_Datos { public class carrito {

items ocar = new items(); public items addpro(int codpro,string prod, double precio,int cantidad,double total) { ocar.Codpro=codpro; ocar.Producto= prod; ocar.Precio= precio; ocar.Cantidad = cantidad; ocar.Total = total; return ocar; } } }

You might also like