<%@ Idioma de página="c#" Codebehind="shoppingcart.aspx.cs" AutoEventWireup="false" Inherits="myshop.shoppingcart" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transicional// ES" ><HTML> <HEAD>
<título>carrito de compras
</título>
<meta http-equiv="Tipo de contenido" content="text/html;
charset=gb2312"> <ENLACE href="mycss.css" type="text/css" rel="stylesheet">
<meta nombre="vs_defaultClientScript" contenido="JavaScript">
<meta name="vs_targetSchema" content=" http://schemas.microsoft.com/intellisense/ie5 "> </HEAD>
<cuerpo> <centro>
<id de formulario="Form1" runat="servidor"> <ancho de tabla="500" border="0" cellspaceing="0" cellpadding="0"> <tr> <td>
<asp:DataGrid id="ShoppingCartDlt" runat="servidor" Ancho="500" BackColor="blanco" BorderColor="negro" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana " Font-Size="8pt" HeaderStyle-BackColor="#cecfd6" AutoGenerateColumns="false" KeepState="true"> <Columnas>
<asp:TemplateColumn HeaderText="删除">
<Plantilla de elemento> <centro>
<asp:CheckBox id="chkProductID" runat="servidor" /> </center>
</ItemTemplate> </asp:TemplateColumn>
<asp:BoundColumn DataField="ProdID" HeaderText="ID" />
<asp:BoundColumn DataField="ProName" HeaderText="商品名称" />
<asp:BoundColumn DataField="UnitPrice" HeaderText="单价" />
<asp:TemplateColumn HeaderText="数量">
<Plantilla de artículo>
<asp:TextBox id="CountTb" runat="servidor" Text='<%#DataBinder.Eval( Container.DataItem,"ProdCount" )%>'> </asp:TextBox>
</ItemTemplate> </asp:TemplateColumn>
<asp:BoundColumn DataField="TotalPrice" HeaderText="小计( 元 )" /> </Columns> </asp:DataGrid></td> </tr> </table> <br> <table width=" 500" border="0" cellpacing="0" cellpadding="0"> <tr> <td>
<asp:Button id="update" runat="server" Text="更新我的购物车" CssClass="button2" /></td> <td>
<asp:ID de botón="CheckOut" runat="servidor" Text="结算" CssClass="botón5" />
<tipo de entrada="botón" nombre="close2" valor="继续购物" onClick="window.close ( );
devolver falso;
" class="botón2"></td> <td align="right"><br>
<asp:Label id="label" runat="servidor" Ancho="100px" Visible="True" ForeColor="#FF8080" Height="18px"></asp:Label></td> </tr> </mesa>
</formulario> </centro>
</cuerpo></HTML>============================================ ==============================================以上为HTML页面部分
==================================================== ==========================================
usando Sistema;
usando System.Collections;
usando System.ComponentModel;
usando System.Web.SessionState;
usando System.Web;
utilizando System.Web.UI;
usando System.Web.UI.HtmlControls;
usando System.Web.UI.WebControls;
usando System.Data;
usando System.Data.OleDb;
usando System.Configuration;
espacio de nombres mi tienda
{
/// <resumen> /// carrito de compras 的摘要说明. /// </summary> carrito de compras de clase pública: System.Web.UI.Page
{
protegido System.Web.UI.WebControls.DataGrid ShoppingCartDlt;
actualización protegida System.Web.UI.WebControls.Button;
System.Web.UI.WebControls.Button CheckOut protegido;
protegido System.Web.UI.HtmlControls.HtmlForm Form1;
etiqueta protegida System.Web.UI.WebControls.Label;
protegido System.Web.UI.WebControls.CheckBox chkProductID;
protegido System.Web.UI.WebControls.TextBox txtCount;
protegido System.Web.UI.WebControls.TextBox CountTb;
cadena AddProID;
Page_Load vacío privado (remitente del objeto, System.EventArgs e)
{
intentar
{
if ( Sesión["inicio de sesión"]!="yes" Sesión["nombre de usuario"]==null )
{
Respuesta.Redirect( "error.htm" );
}
}
atrapar
{
Respuesta.Redirect( "error.htm" );
}
/////////////查看用户是否已经登陆.
si (! IsPostBack)
{
if( Request.Params["mode"]=="view" ) //检测是否为直接查看购物车.
{
Vercarritodecompras( );
Calculadora( );
}
if( Request.Params["productID"]!=null Request.Params["productID"]!="" )
{
AddProID=Solicitud["productID"];
Actualizarcarritodecompras( );
Calculadora( );
}
}
// 在此处放置用户代码以初始化页面
}
public void CreateCartTable( ) //创建购物车
{
Conjunto de datos ds = nuevo Conjunto de datos( );
DataTable newDT=new DataTable( "CartTable" );
ds.Tables.Add(nuevoDT);
Columna de datos nuevoDC;
newDC=new DataColumn( "ProdID",System.Type.GetType( "System.Int32" ) );
ds.Tables["CartTable"].Columns.Add( nuevoDC );
newDC=new DataColumn( "ProdCount",System.Type.GetType( "System.Int32" ) );
newDC.DefaultValue=1;
ds.Tables["CartTable"].Columns.Add( nuevoDC );
newDC=new DataColumn( "ProName",System.Type.GetType( "System.String" ) );
ds.Tables["CartTable"].Columns.Add( nuevoDC );
newDC=new DataColumn( "PrecioUnitario",System.Type.GetType( "System.Double" ) );
ds.Tables["CartTable"].Columns.Add( nuevoDC );
newDC=new DataColumn( "Precio Total",System.Type.GetType( "System.Double" ) );
ds.Tables["CartTable"].Columns.Add( nuevoDC );
newDC=nueva columna de datos( "IsDeleted",System.Type.GetType( "System.Int32" ) );
newDC.DefaultValue=0;
// public void WriteShoppingCart( ) como newDR[5]="0";
行,已被注销, ds.Tables["CartTable"].Columns.Add( newDC );
Sesión["myCartTable"]=newDT;
ShoppingCartDlt.DataSource=ds.Tables["CartTable"].DefaultView;
ShoppingCartDlt.DataBind( );
}
Actualizar carrito de compras vacío público ()
{
if( Sesión["myCartTable"]==null )//Sesión["myCartTable"]==null
{
CrearCartTable( );
//调用函数CreateCartTable( )新建一个DataTable WriteShoppingCart( );
}
demás
{
//如果购物蓝中已有商品,则需要对购物信息表DataTable进行更新,并将其棒定到ShoppingCartDlt WriteShoppingCart( );
}
}
public void ViewShoppingCart( ) //查看购物车
{
if( Sesión["myCartTable"]!=null )
{
Tabla de datos viewTable=nueva tabla de datos( "nowCartTable" );
viewTable=( DataTable )Sesión["myCartTable"];
ShoppingCartDlt.DataSource = viewTable.DefaultView;
//购物车棒定到ShoppingCartDlt ShoppingCartDlt.DataBind( );
}
}
vacío público WriteShoppingCart ()
{
if( Request.Params["mode"]!="view" ) //检查是否是直接查看购物车,如果直接查看,就不再写MYCARTTABLE
{
DataTable nowTable=new DataTable( "nowCartTable" );
nowTable=( DataTable )Sesión["myCartTable"];
int pn=nowTable.Rows.Count;
int i=0;
bool hasone = falso;
int ahoraProdID;
mientras (i<pn &&!hasone)
{
nowProdID=Int32.Parse( nowTable.Rows[i][0].ToString( ) );
if( nowProdID==Int32.Parse( AddProID ) ) //判断购物信息表中,是否存有当前放入商品. si( nowProdID==Int32.Parse( AddProID ) )
{
hasone=verdadero;
}
demás
{
yo ++;
}
}
si (tiene uno)
{
//如果已有该商品,则 hasone=true,更改该数据行 DataRow oldDR;
oldDR=nowTable.Rows[i];
oldDR["ProdCount"]=Int32.Parse( oldDR["ProdCount"].ToString( ) )+1;
oldDR["TotalPrice"]=Int32.Parse( oldDR["ProdCount"].ToString( ) )*Double.Parse( oldDR["UnitPrice"].ToString( ) );
}
demás
{
//如果没有该商品,在表中新加如一行. DataRow nuevoDR;
unidad doble;
Cadena strcon="proveedor=Microsoft.jet.OLEDB.4.0;
fuente de datos="+Server.MapPath( ConfigurationSettings.AppSettings["MDBpath2"] )+";
";
OleDbConnection myConnection = nueva OleDbConnection( strcon );
string strSQL= "seleccione * de pro donde product_id="+AddProID+"";
OleDbDataAdapter myCommand = nuevo OleDbDataAdapter( strSQL, myConnection );
Conjunto de datos ds = nuevo Conjunto de datos( );
myCommand.Fill(ds, "AddP");
newDR=nowTable.NewRow( );
newDR[0]=AgregarProID;
newDR[2]=ds.Tables["Addp"].Rows[0]["product_name"].ToString( );
unitp=Double.Parse( ds.Tables["AddP"].Rows[0]["product_memprice"].ToString( ) );
//会员价 newDR[3]=unitp;
nuevoDR[4]=unidadp;
//第一次读库,所以总价格和单价是一样的. //newDR[5]="0";
nowTable.Rows.Add( nuevoDR );
miConexión.Cerrar( );
}
ShoppingCartDlt.DataSource = nowTable.DefaultView;
//将更新后的 DataTable棒定到ShoppingCartDlt ShoppingCartDlt.DataBind( );
Sesión["myCartTable"] = nowTable;
//重新保存更新过的DataTable
}
}
Calculador público vacío ()
{
if( Sesión["myCartTable"]!=null ) //购物车是否为空
{
inth;
Doble Pri Total;
Pritototal=0;
DataTable nowTable3=new DataTable( "nowCartTable3" );
nowTable3=( DataTable )Sesión["myCartTable"];
if( nowTable3.Rows.Count>0 ) //返回购物车中是否有货物
{
para (h=0;
h<=nowTable3.Rows.Count-1;
h++)
{
TotalPri=TotalPri+Int32.Parse( nowTable3.Rows[h][4].ToString( ) );
//Double.Parse( (cadena)TotalText.Text);
}
label.Text="总计: "+TotalPri.ToString( )+" no" ;
}
}
}
Actualización pública vacía ()
{
ent i;
intj;
intk;
ArrayList eliminarItem = nuevo ArrayList( 10 );
Elemento de cuadrícula de datos _elemento;
j=0;
int eliminadoid;
k=0;
DataTable nowTable2=new DataTable( "nowCartTable2" );
nowTable2=( DataTable )Sesión["myCartTable"];
para (yo = 0;
i<=this.ShoppingCartDlt.Items.Count-1;
yo ++)
{
_item = this.ShoppingCartDlt.Items[i];
TextBox CountText=( TextBox )this.ShoppingCartDlt.Items[i].Cells[4].FindControl( "CountTb" );
//Controles[1];
//_item.FindControl( "CountTb" );
CheckBox ProductIDCheck =( CheckBox ) _item.FindControl( "chkProductID" );
nowTable2.Rows[i][1] = Int32.Parse( CountText.Text.ToString( ) );
nowTable2.Rows[i][4] = Int32.Parse( nowTable2.Rows[i][1].ToString( ) ) * Double.Parse( nowTable2.Rows[i][3].ToString( ) );
si (ProductIDCheck.Checked)
{
nowTable2.Rows[i][5] = 1;
//添加删除标记1 j=j+1;
}
}
string strExpr="Está eliminado>0";
//http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfSystemDataDataTableClassSelectTopic.asp DataRow[] foundRows = nowTable2.Select( strExpr );
para (int m = 0;
m < filas encontradas.Longitud;
m++)
{
//Console.WriteLine( foundRows[i][0] );
filas encontradas [m]. Eliminar ();
}
ShoppingCartDlt.DataSource = nowTable2.DefaultView;
ShoppingCartDlt.DataBind( );
Sesión["myCartTable"] = nowTable2;
Calculadora( );
}
#region Web 窗体设计器生成的代码 anular el vacío protegido OnInit( EventArgs e )
{
// // CODEGEN: 该调用是 asp.NET Web 窗体设计器所必需的. // InicializarComponente( );
base.OnInit(e);
}
/// <resumen> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容. /// </summary> privado vacío InicializarComponente( )
{
this.update.Click += nuevo System.EventHandler (this.update_Click);
this.CheckOut.Click += nuevo System.EventHandler (this.CheckOut_Click);
this.Load += nuevo System.EventHandler (this.Page_Load);
}
#regiónfinal
update_Click vacío privado (remitente del objeto, System.EventArgs e)
{
Actualizar( );
}
checkOut_Click vacío privado (remitente del objeto, System.EventArgs e)
{
Actualizar( );
Respuesta.Redirect( "checkout.aspx" );
}
}
}
http://www.cnblogs.com/boundless/archive/2007/01/16/621553.html