<%@ Halaman bahasa="c#" Codebehind="shoppingcart.aspx.cs" AutoEventWireup="false" Inherits="myshop.shoppingcart" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional// EN" ><HTML> <KEPALA>
<judul>keranjang belanja
</judul>
<meta http-equiv="Jenis Konten" content="teks/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>
<tubuh> <tengah>
<form id="Form1" runat="server"> <lebar tabel="500" border="0" Cellspacing="0" cellpadding="0"> <tr> <td>
<asp:DataGrid id="ShoppingCartDlt" runat="server" width="500" BackColor="putih" BorderColor="hitam" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana " Font-Size="8pt" HeaderStyle-BackColor="#cecfd6" AutoGenerateColumns="false" HoldState="true"> <Kolom>
<asp:TemplateColumn HeaderText="删除">
<Templat Item> <tengah>
<asp:CheckBox id="chkProductID" runat="server" /> </center>
</ItemTemplate> </asp:TemplateColumn>
<asp:BoundColumn DataField="ProdID" HeaderText="ID" />
<asp:BoundColumn DataField="ProName" HeaderText="商品名称" />
<asp:BoundColumn DataField="UnitPrice" HeaderText="单价" />
<asp:TemplateColumn HeaderText="数量">
<Templat Barang>
<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 ( );
kembali salah;
" class="button2"></td> <td align="kanan"><br>
<asp:Label id="label" runat="server" width="100px" Visible="True" ForeColor="#FF8080" Height="18px"></asp:Label></td> </tr> </meja>
</form> </tengah>
</body></HTML>============= ==================== HTML页面部分
==================== ==========
menggunakan Sistem;
menggunakan Sistem.Koleksi;
menggunakan Sistem.ComponentModel;
menggunakan Sistem.Web.SessionState;
menggunakan Sistem.Web;
menggunakan Sistem.Web.UI;
menggunakan Sistem.Web.UI.HtmlControls;
menggunakan Sistem.Web.UI.WebControls;
menggunakan Sistem.Data;
menggunakan Sistem.Data.OleDb;
menggunakan Sistem.Konfigurasi;
namespace toko saya
{
/// <ringkasan> /// keranjang belanja 的摘要说明. /// </summary> keranjang belanja kelas publik : System.Web.UI.Page
{
Sistem.Web.UI.WebControls.DataGrid ShoppingCartDlt yang dilindungi;
pembaruan System.Web.UI.WebControls.Button yang dilindungi;
Sistem.Web.UI.WebControls.Button CheckOut yang dilindungi;
dilindungi System.Web.UI.HtmlControls.HtmlForm Form1;
label System.Web.UI.WebControls.Label yang dilindungi;
dilindungi System.Web.UI.WebControls.CheckBox chkProductID;
dilindungi System.Web.UI.WebControls.TextBox txtCount;
dilindungi System.Web.UI.WebControls.TextBox CountTb;
string TambahkanProID;
private void Page_Load( pengirim objek, System.EventArgs e )
{
mencoba
{
if ( Sesi["logon"]!="ya" Sesi["nama pengguna"]==null )
{
Respon.Redirect( "error.htm" ) ;
}
}
menangkap
{
Respon.Redirect( "error.htm" ) ;
}
/////////////查看用户是否已经登陆.
jika( !IsPostBack )
{
if( Request.Params["mode"]=="view" ) //检测是否为直接查看购物车.
{
LihatKeranjang Belanja( );
Kalkulator( );
}
if( Permintaan.Params["productID"]!=null Permintaan.Params["productID"]!="" )
{
AddProID=Permintaan["ID produk"];
PerbaruiKeranjang Belanja( );
Kalkulator( );
}
}
// 在此处放置用户代码以初始化页面
}
public void CreateCartTable( ) //创建购物车
{
Kumpulan Data ds = Kumpulan Data baru();
DataTable newDT=DataTable baru( "Tabel Keranjang" );
ds.Tabel.Tambahkan( newDT );
Kolom Data DC baru;
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( "Harga Unit",System.Type.GetType( "System.Double" ) );
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "TotalHarga",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( ) dan newDR[5]="0";
行,已被注销, ds.Tables["CartTable"].Columns.Add( newDC );
Sesi["myCartTable"]=newDT;
ShoppingCartDlt.DataSource=ds.Tables["CartTable"].DefaultView;
Keranjang BelanjaDlt.DataBind( );
}
kekosongan publik UpdateShoppingCart()
{
if( Sesi["myCartTable"]==null )//Sesi["myCartTable"]==null
{
BuatTabelKeranjang( );
//调用函数CreateCartTable( )新建一个DataTable WriteShoppingCart( );
}
kalau tidak
{
//如果购物蓝中已有商品,则需要对购物信息表DataTable进行更新,并将其棒定到ShoppingCartDlt WriteShoppingCart( );
}
}
public void ViewShoppingCart( ) //查看购物车
{
jika( Sesi["myCartTable"]!=null )
{
DataTable viewTable=DataTable baru( "nowCartTable" );
viewTable=( DataTable )Sesi["myCartTable"];
ShoppingCartDlt.DataSource = viewTable.DefaultView;
//购物车棒定到ShoppingCartDlt ShoppingCartDlt.DataBind( );
}
}
kekosongan publik WriteShoppingCart()
{
if( Request.Params["mode"]!="view" ) //检查是否是直接查看购物车,如果直接查看,就不再写MYCARTTABLE
{
DataTable nowTable=DataTable baru( "nowCartTable" );
nowTable=( DataTable )Sesi["myCartTable"];
int pn=sekarangTabel.Baris.Hitungan;
ke dalam saya=0;
bool hasone=salah;
int sekarangProdID;
sementara( saya<pn && !hasone )
{
nowProdID=Int32.Parse( nowTable.Rows[i][0].ToString( ) );
if( nowProdID==Int32.Parse( AddProID ) ) //判断购物信息表中,是否存有当前放入商品. jika( sekarangProdID==Int32.Parse( AddProID ) )
{
hasone=benar;
}
kalau tidak
{
saya++;
}
}
jika (memiliki seseorang)
{
//如果已有该商品,则 hasone=true,更改该数据行 DataRow oldDR;
oldDR=sekarangTabel.Baris[i];
oldDR["ProdCount"]=Int32.Parse( oldDR["ProdCount"].ToString( ) )+1;
oldDR["TotalPrice"]=Int32.Parse( oldDR["ProdCount"].ToString( ) )*Double.Parse( oldDR["UnitPrice"].ToString( ) );
}
kalau tidak
{
//如果没有该商品,在表中新加如一行. DataRow DR baru;
unit ganda;
String strcon="penyedia=Microsoft.jet.OLEDB.4.0;
Sumber data="+Server.MapPath( ConfigurationSettings.AppSettings["MDBpath2"] )+";
";
OleDbConnection myConnection = OleDbConnection baru( strcon );
string strSQL= "pilih * dari pro di mana product_id="+AddProID+"";
OleDbDataAdapter myCommand = OleDbDataAdapter baru( strSQL, myConnection );
Kumpulan Data ds = Kumpulan Data baru();
myCommand.Fill( ds, "TambahP" );
newDR=sekarangTable.NewRow();
newDR[0]=TambahkanProID;
newDR[2]=ds.Tables["Addp"].Baris[0]["nama_produk"].ToString( );
unitp=Double.Parse( ds.Tables["AddP"].Rows[0]["product_memprice"].ToString( ) );
//会员价 newDR[3]=unitp;
newDR[4]=unitp;
//第一次读库, 所以总价格和单价是一样的. //DR baru[5]="0";
sekarangTabel.Baris.Tambahkan( newDR );
koneksi saya.Tutup( );
}
ShoppingCartDlt.DataSource = sekarangTable.DefaultView;
//将更新后的 DataTable棒定到ShoppingCartDlt ShoppingCartDlt.DataBind( );
Sesi["myCartTable"] = tabel sekarang;
//重新保存更新过的DataTable
}
}
Kalkulator kekosongan publik( )
{
if( Session["myCartTable"]!=null ) //购物车是否为空
{
ke dalam jam;
TotalPri Ganda;
JumlahPri=0;
DataTable nowTable3=DataTable baru( "nowCartTable3" );
nowTable3=( DataTable )Sesi["myCartTable"];
if( nowTable3.Rows.Count>0 ) //返回购物车中是否有货物
{
untuk( h=0;
h<=sekarangTabel3.Baris.Hitungan-1;
jam++ )
{
TotalPri=TotalPri+Int32.Parse( sekarangTable3.Rows[h][4].ToString( ) );
//Double.Parse( ( string )TotalTeks.Teks );
}
label.Text="总计: "+TotalPri.ToString( )+" 元" ;
}
}
}
Pembaruan kekosongan publik()
{
ke dalam aku;
ke dalam j;
ke dalam k;
ArrayList deleteItem = Daftar Array baru( 10 );
DataGridItem _item;
j=0;
int hapusid;
k=0;
DataTable nowTable2=DataTable baru( "nowCartTable2" );
nowTable2=( DataTable )Sesi["myCartTable"];
untuk( saya=0;
i<=ini.KeranjangBelanjaDlt.Item.Hitungan-1;
saya++ )
{
_item = ini.Keranjang BelanjaDlt.Item[i];
TextBox CountText=( TextBox )this.ShoppingCartDlt.Items[i].Cells[4].FindControl( "CountTb" );
//Kontrol[1];
//_item.FindControl( "HitunganTb" );
Kotak Centang ProductIDCheck =( Kotak Centang ) _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.Diperiksa )
{
sekarangTabel2.Baris[i][5] = 1;
//添加删除标记1 j=j+1;
}
}
string strExpr="Dihapus>0";
//http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfSystemDataDataTableClassSelectTopic.asp DataRow[] foundRows = nowTable2.Select( strExpr );
untuk( ke dalam m = 0;
m < foundRows.Length;
m ++ )
{
//Konsol.WriteLine( foundRows[i][0] );
foundRows[m].Hapus();
}
ShoppingCartDlt.DataSource = sekarangTable2.DefaultView;
Keranjang BelanjaDlt.DataBind( );
Sesi["myCartTable"] = sekarangTable2;
Kalkulator( );
}
#region Web 窗体设计器生成的代码 menimpa void OnInit yang dilindungi( EventArgs e )
{
// // CODEGEN: 该调用是 asp.NET Web 窗体设计器所必需的. // Inisialisasi Komponen( );
base.OnInit( e );
}
/// <ringkasan> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容. /// </ringkasan> private void InitializeComponent( )
{
this.update.Click += Sistem baru.EventHandler( this.update_Click );
this.CheckOut.Click += Sistem baru.EventHandler( this.CheckOut_Click );
this.Load += Sistem baru.EventHandler( this.Page_Load );
}
#wilayah akhir
private void update_Click( pengirim objek, System.EventArgs e )
{
Memperbarui( );
}
private void CheckOut_Click( pengirim objek, System.EventArgs e )
{
Memperbarui( );
Respon.Redirect( "checkout.aspx" );
}
}
}
http://www.cnblogs.com/boundless/archive/2007/01/16/621553.html