<%@ Page language="c#" Codebehind="shoppingcart.aspx.cs" AutoEventWireup="false" Inherits="myshop.shoppingcart" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional// PT" ><HTML> <HEAD>
<título>carrinho de compras
</título>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312"> <LINK href="mycss.css" type="text/css" rel="stylesheet">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" http://schemas.microsoft.com/intellisense/ie5 "> </HEAD>
<corpo> <centro>
<form id="Form1" runat="servidor"> <table width="500" border="0" cellpacing="0" cellpadding="0"> <tr> <td>
<asp:DataGrid id="ShoppingCartDlt" runat="server" Width="500" BackColor="white" BorderColor="black" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana " Font-Size="8pt" HeaderStyle-BackColor="#cecfd6" AutoGenerateColumns="false" KeepState="true"> <Colunas>
<asp:TemplateColumn HeaderText="删除">
<ItemTemplate> <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="数量">
<ItemTemplate>
<asp:TextBox id="CountTb" runat="server" 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:Button id="CheckOut" runat="server" Text="结算" CssClass="button5" />
<input type="button" name="close2" value="继续购物" onClick="window.close ( );
retornar falso;
" class="button2"></td> <td align="right"><br>
<asp:Label id="label" runat="server" Width="100px" Visible="True" ForeColor="#FF8080" Height="18px"></asp:Label></td> </tr> </tabela>
</forma> </centro>
</corpo></HTML>========================================== ========================================================================================================================
================================================= =======================================
usando o sistema;
usando System.Collections;
usando System.ComponentModel;
usando System.Web.SessionState;
usando System.Web;
usando System.Web.UI;
usando System.Web.UI.HtmlControls;
usando System.Web.UI.WebControls;
usando System.Data;
usando System.Data.OleDb;
usando System.Configuration;
namespace minhaloja
{
/// <resumo> /// carrinho de compras 的摘要说明. ///</summary> carrinho de compras de classe pública: System.Web.UI.Page
{
protegido System.Web.UI.WebControls.DataGrid ShoppingCartDlt;
atualização protegida de System.Web.UI.WebControls.Button;
System.Web.UI.WebControls.Button CheckOut protegido;
System.Web.UI.HtmlControls.HtmlForm Form1 protegido;
rótulo 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 protegido;
string AddProID;
private void Page_Load (remetente do objeto, System.EventArgs e)
{
tentar
{
if ( Sessão["logon"]!="yes" Sessão["nomedeusuário"]==null )
{
Response.Redirect("erro.htm");
}
}
pegar
{
Response.Redirect("erro.htm");
}
/////////////查看用户是否已经登陆.
if(!IsPostBack)
{
if( Request.Params["mode"]=="view" ) //检测是否为直接查看购物车.
{
VerCarrinhoDeCompras();
Calculadora();
}
if( Request.Params["productID"]!=null Request.Params["productID"]!="" )
{
AddProID=Solicitação["productID"];
AtualizarCarrinhoDeCompras();
Calculadora();
}
}
// 在此处放置用户代码以初始化页面
}
public void CreateCartTable() //创建购物车
{
DataSet ds = new DataSet();
DataTable newDT=new DataTable("CartTable");
ds.Tables.Add(novoDT);
DataColumn novoDC;
newDC=new DataColumn( "ProdID",System.Type.GetType( "System.Int32" ));
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "ProdCount",System.Type.GetType( "System.Int32" ));
newDC.DefaultValue=1;
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "ProName",System.Type.GetType( "System.String" ) );
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "UnitPrice",System.Type.GetType( "System.Double" ));
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "TotalPrice",System.Type.GetType( "System.Double" ));
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "IsDeleted",System.Type.GetType( "System.Int32" ) );
newDC.DefaultValue=0;
// public void WriteShoppingCart() por newDR[5]="0";
Aqui, ds.Tables["CartTable"].Columns.Add( newDC );
Sessão["minhaTabelaCarrinho"]=newDT;
ShoppingCartDlt.DataSource=ds.Tables["CartTable"].DefaultView;
ShoppingCartDlt.DataBind();
}
public void UpdateShoppingCart()
{
if( Session["myCartTable"]==null )//Session["myCartTable"]==null
{
CriarCartTable();
//definir o nome CreateCartTable( ) e usar DataTable WriteShoppingCart( );
}
outro
{
//如果购物蓝中已有商品,则需要对购物信息表DataTable进行更新,并将其棒定到ShoppingCartDlt WriteShoppingCart( );
}
}
public void ViewShoppingCart() //查看购物车
{
if(Sessão["minhaTabelaCarrinho"]!=null )
{
DataTable viewTable=new DataTable("nowCartTable");
viewTable=(DataTable)Sessão["minhaTabelaCarrinho"];
ShoppingCartDlt.DataSource = viewTable.DefaultView;
//购物车棒定到ShoppingCartDlt ShoppingCartDlt.DataBind();
}
}
público vazio WriteShoppingCart()
{
if( Request.Params["mode"]!="view" ) //检查是否是直接查看购物车,如果直接查看,就不再写MYCARTTABLE
{
DataTable agoraTable=new DataTable( "nowCartTable" );
nowTable=( DataTable )Sessão["myCartTable"];
int pn=nowTable.Rows.Count;
int i=0;
bool hasone = falso;
int agoraProdID;
enquanto(i<pn && !hasone )
{
nowProdID=Int32.Parse(nowTable.Rows[i][0].ToString( ) );
if( nowProdID==Int32.Parse( AddProID ) ) //判断购物信息表中,是否存有当前放入商品. if(nowProdID==Int32.Parse( AddProID ) )
{
hasone=verdadeiro;
}
outro
{
eu++;
}
}
se(tem um)
{
//如果已有该商品,则 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( ) );
}
outro
{
//如果没有该商品,在表中新加如一行. DataRow novoDR;
unidade duplap;
String strcon="provider=Microsoft.jet.OLEDB.4.0;
fonte de dados="+Server.MapPath(ConfiguraçãoSettings.AppSettings["MDBpath2"] )+";
";
OleDbConnection minhaConexão = new OleDbConnection(strcon);
string strSQL= "selecione * do pro onde product_id="+AddProID+"";
OleDbDataAdapter meuCommand = new OleDbDataAdapter(strSQL, minhaConexão);
DataSet ds = new DataSet();
meuCommand.Fill(ds, "AddP");
newDR=nowTable.NewRow();
novoDR[0]=AddProID;
newDR[2]=ds.Tables["Addp"].Rows[0]["product_name"].ToString( );
unitp=Double.Parse(ds.Tables["AddP"].Rows[0]["product_memprice"].ToString() );
//会员价 newDR[3]=unitp;
novoDR[4]=unidadep;
//第一次读库,所以总价格和单价是一样的. //newDR[5]="0";
agoraTable.Rows.Add(newDR);
minhaConexão.Close();
}
ShoppingCartDlt.DataSource = nowTable.DefaultView;
//将更新后的 DataTable棒定到ShoppingCartDlt ShoppingCartDlt.DataBind();
Session["minhaTabelaCarrinho"] = tabelaagora;
//重新保存更新过的DataTable
}
}
Calculadora de vazio público ( )
{
if( Session["myCartTable"]!=null ) //购物车是否为空
{
interno h;
TotalPri duplo;
TotalPri=0;
DataTable agoraTable3=new DataTable( "nowCartTable3" );
nowTable3=( DataTable )Sessão["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((string)TotalText.Text);
}
label.Text="总计: "+TotalPri.ToString()+" e" ;
}
}
}
atualização pública nula ( )
{
int eu;
intj;
intk;
ArrayList deleteItem = new ArrayList(10);
DataGridItem _item ;
j=0;
int deletado;
k=0;
DataTable agoraTable2=new DataTable( "nowCartTable2" );
nowTable2=( DataTable )Sessão["myCartTable"];
para(eu=0;
i<=this.ShoppingCartDlt.Items.Count-1;
eu++)
{
_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( ) );
if(ProductIDCheck.Checked)
{
nowTable2.Rows[i][5] = 1;
//添加删除标记1 j=j+1;
}
}
string strExpr="IsDeleted>0";
//http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfSystemDataTableClassSelectTopic.asp DataRow[] foundRows = nowTable2.Select( strExpr );
para(int m = 0;
m<foundRows.Length;
m++)
{
//Console.WriteLine(foundRows[i][0] );
encontradoRows[m].Delete();
}
ShoppingCartDlt.DataSource = nowTable2.DefaultView;
ShoppingCartDlt.DataBind();
Session["myCartTable"] = nowTable2;
Calculadora();
}
#region Web 窗体设计器生成的代码 substituir protected void OnInit( EventArgs e )
{
// // CODEGEN: 该调用是 asp.NET Web 窗体设计器所必需的. // InicializarComponent();
base.OnInit( e );
}
/// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容. /// </resumo> private void InitializeComponent()
{
this.update.Click += novo System.EventHandler( this.update_Click );
this.CheckOut.Click += novo System.EventHandler( this.CheckOut_Click );
this.Load += novo System.EventHandler( this.Page_Load );
}
#endregion
private void update_Click(objeto remetente, System.EventArgs e)
{
Atualizar( );
}
private void CheckOut_Click (remetente do objeto, System.EventArgs e)
{
Atualizar( );
Response.Redirect("checkout.aspx");
}
}
}
http://www.cnblogs.com/boundless/archive/2007/01/16/621553.html