佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > ASP.NET購(gòu)物車(chē)實(shí)現(xiàn)過(guò)程詳解

ASP.NET購(gòu)物車(chē)實(shí)現(xiàn)過(guò)程詳解

熱門(mén)標(biāo)簽:網(wǎng)站排名優(yōu)化 硅谷的囚徒呼叫中心 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 地方門(mén)戶(hù)網(wǎng)站 百度競(jìng)價(jià)排名 阿里云 集中運(yùn)營(yíng)管理辦法 服務(wù)器配置

本文實(shí)例為大家分享了ASP.NET實(shí)現(xiàn)購(gòu)物車(chē)的具體代碼,供大家參考,具體內(nèi)容如下

1、 將test數(shù)據(jù)庫(kù)附加到數(shù)據(jù)庫(kù)管理系統(tǒng)中;數(shù)據(jù)庫(kù)中的book_info包含下列數(shù)據(jù):

2、 新建一個(gè)網(wǎng)站,將images文件夾復(fù)制到網(wǎng)站中;

3、 在Default.aspx中,通過(guò)DataList控件展示數(shù)據(jù)庫(kù)中的所有數(shù)據(jù),以行為主序,每行3列,單擊購(gòu)買(mǎi)按鈕時(shí),將商品的ID和數(shù)量保存到HashTable中,并將HashTable放置到Session中。

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) 
 { 
  string id = e.CommandArgument.ToString(); 
  Hashtable ht; 
  if (Session["shopcar"] == null) 
  { 
   ht = new Hashtable(); 
   ht.Add(id, 1); 
   Session["shopcar"] = ht; 
  } 
  else 
  { 
   ht = (Hashtable)Session["shopcar"]; 
   if (ht.Contains(id)) 
   { 
    int count = int.Parse(ht[id].ToString()); 
    ht[id] = count + 1; 
    Session["shopcar"] = ht; 
    Response.Write(count + 1); 
   } 
   else 
   { 
    ht.Add(id, 1); 
    Session["shopcar"] = ht; 
   } 
  } 
 } 

4、 在Default.aspx中添加一個(gè)超鏈接,鏈接到shopcart.aspx,在shopcart.aspx中顯示用戶(hù)購(gòu)買(mǎi)的商品信息。
提示:

A、在shopcart中先定義下列變量:

Hashtable ht;
 DataTable dt;
 string connstring=@"DataSource=.\SQLEXPRESS;Initial Catalog=test;Integrated Security=True";
 SqlConnection conn;
 SqlCommand cmd;
 SqlDataReader sdr;

B、頁(yè)面中添加一個(gè)GridView。
C、在page_load中,將dt實(shí)例化,建立各列。

protected void Page_Load(object sender, EventArgs e)
 {
  dt = new DataTable();
  DataColumn col = new DataColumn();
  col.ColumnName= "id";
  col.DataType =System.Type.GetType("System.String");
  dt.Columns.Add(col);
  col = new DataColumn();
  col.ColumnName= "name";
  col.DataType =System.Type.GetType("System.String");
  dt.Columns.Add(col);
  col = new DataColumn();
  col.ColumnName= "Num";
  col.DataType =System.Type.GetType("System.Int32");
  dt.Columns.Add(col);
  col = new DataColumn();
  col.ColumnName= "price";
  col.DataType =System.Type.GetType("System.Single");
  dt.Columns.Add(col);
  col = new DataColumn();
  col.ColumnName= "Total";
  col.DataType =System.Type.GetType("System.Single");
  dt.Columns.Add(col);
  if (!IsPostBack)
  {
   Bind();
  }
 }
 
 
 public void Bind()
 {
  
 
  if (Session["shopcar"] == null)
  {
   Response.Write("script>if(confirm('你沒(méi)有登錄')window.location='Default15.aspx';else window.close();/script>");
  }
  else
  {
   ht = (Hashtable)Session["shopcar"];
   foreach (object item in ht.Keys)
   {
    string id = item.ToString();
    int num = int.Parse(ht[item].ToString());
    string sql = "selectbook_name,price from book_info where book_id='" + id + "'";
    conn = new SqlConnection(connstring);
    cmd = new SqlCommand(sql, conn);
    conn.Open();
    sdr =cmd.ExecuteReader();
    if (sdr.HasRows)
    {
     sdr.Read();
     DataRow row = dt.NewRow();
     row["id"] = id;
     row["Num"] = num;
     row["name"] = sdr.GetString(0);
     row["price"] =float.Parse(sdr[1].ToString());
     row["total"] =num*(float.Parse(sdr[1].ToString()));
     dt.Rows.Add(row);
    }
    sdr.Close();
    conn.Close();
        
   }
   GridView1.DataSource = dt.DefaultView;
   GridView1.DataBind();
  }
}

D、這時(shí)可以看到用戶(hù)購(gòu)買(mǎi)的商品,但不能修改數(shù)量,也不能刪除。
E、添加修改數(shù)量,刪除商品功能,在aspx頁(yè)面中定義GridView中的各列:

 asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
   Columns>
    asp:BoundField DataField="id" HeaderText="ID" />
    asp:BoundField DataField="name" HeaderText="名稱(chēng)" />
    asp:BoundField DataField="price" HeaderText="價(jià)格" />
    asp:TemplateField>   
    ItemTemplate>
     asp:TextBox runat="server" ID="textbox1" Text='%# Eval("Num") %>'
      ontextchanged="textbox1_TextChanged" AutoPostBack="True" >/asp:TextBox>
    /ItemTemplate>   
    /asp:TemplateField>
   asp:BoundField DataField="total" HeaderText="總計(jì)" />
   asp:TemplateField>
    ItemTemplate>
    asp:Button runat="server" ID="button1" CommandArgument='%# Eval("id") %>'
      Text="刪除" onclick="button1_Click" />
    
    /ItemTemplate>
   
   /asp:TemplateField>
   /Columns>   
  /asp:GridView>

F、為GridView中的文本框添加TextChanged事件:

protected void textbox1_TextChanged(object sender, EventArgs e)
 {
  
  Hashtable ht =(Hashtable)Session["shopcar"];
  if (ht == null) return;
  for (int i = 0; i  GridView1.Rows.Count;i++)
  {
   string id =GridView1.Rows[i].Cells[0].Text.ToString();
   Response.Write(id);
   string num = ((TextBox)GridView1.Rows[i].FindControl("textbox1")).Text;
   Response.Write(" "+num+"br />");
   ht[id] = num;
  }
  Session["shopcar"] = ht;
  Bind();
  
 }

G、為按鈕添加單擊事件:

protected void button1_Click(object sender, EventArgs e)
 {
  string id = ((Button)sender).CommandArgument;
  Hashtable ht = (Hashtable)Session["shopcar"];
  if (ht == null) return;
  ht.Remove(id);
  Bind();
}

購(gòu)物車(chē)代碼:showcart.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Collections; 
using System.Data; 
using System.Data.SqlClient; 
 
public partial class shopcart : System.Web.UI.Page 
{ 
 Hashtable ht; 
 DataTable dt; 
 string connstr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=F:

\\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; 
 SqlConnection conn; 
 SqlCommand cmd; 
 SqlDataReader sdr; 
 protected void Page_Load(object sender, EventArgs e) 
 { 
  dt = new DataTable(); 
  DataColumn col = new DataColumn(); 
  col.ColumnName = "id"; 
  col.DataType = System.Type.GetType("System.String"); 
  dt.Columns.Add(col); 
  col = new DataColumn(); 
  col.ColumnName = "name"; 
  col.DataType = System.Type.GetType("System.String"); 
  dt.Columns.Add(col); 
  col = new DataColumn(); 
  col.ColumnName = "Num"; 
  col.DataType = System.Type.GetType("System.Int32"); 
  dt.Columns.Add(col); 
  col = new DataColumn(); 
  col.ColumnName = "price"; 
  col.DataType = System.Type.GetType("System.Single"); 
  dt.Columns.Add(col); 
  col = new DataColumn(); 
  col.ColumnName = "Total"; 
  col.DataType = System.Type.GetType("System.Single"); 
  dt.Columns.Add(col); 
 
  if (!IsPostBack) 
  { 
   Bind(); 
  } 
 
 } 
 
 public void Bind() 
 { 
  if (Session["shopcar"] == null) 
  { 
   Response.Write("script>if(confirm('你沒(méi)有登錄')window.location='Default.aspx';else window.close();/script>"); 
  } 
  else 
  { 
   ht = (Hashtable)Session["shopcar"]; 
   foreach (object item in ht.Keys) 
   { 
    string id = item.ToString(); 
 
    int num = int.Parse((ht[item].ToString())); 
    string sql = "select book_name,price from book_info where book_id='" + id + "'"; 
    conn = new SqlConnection(connstr); 
 
    cmd = new SqlCommand(sql, conn); 
    conn.Open(); 
 
    sdr = cmd.ExecuteReader(); 
    if (sdr.HasRows) 
    { 
     sdr.Read(); 
     DataRow row = dt.NewRow(); 
     row["id"] = id; 
     row["Num"] = num; 
     row["name"] = sdr.GetString(0); 
     row["price"] = float.Parse(sdr[1].ToString()); 
     row["total"] = num * (float.Parse(sdr[1].ToString())); 
     dt.Rows.Add(row); 
 
    } 
    sdr.Close(); 
    conn.Close(); 
   } 
  } 
  GridView1.DataSource = dt.DefaultView; 
  GridView1.DataBind(); 
 
 } 
 protected void textbox1_TextChanged(object sender, EventArgs e) 
 { 
  Hashtable ht = (Hashtable)Session["shopcar"]; 
  if (ht == null) return; 
  for (int i = 0; i  GridView1.Rows.Count; i++) 
  { 
   string id = GridView1.Rows[i].Cells[0].Text.ToString(); 
   Response.Write(id); 
   string num = ((TextBox)GridView1.Rows[i].FindControl("textbox1")).Text; 
   Response.Write(" " + num + "br />"); 
   ht[id] = num; 
  } 
  Session["shopcar"] = ht; 
  Bind(); 
 
 } 
 protected void button1_Click(object sender, EventArgs e) 
 { 
  string id = ((Button)sender).CommandArgument; 
  Hashtable ht = (Hashtable)Session["shopcar"]; 
  if (ht == null) return; 
  ht.Remove(id); 
  Bind(); 
 
 } 
} 

制作一個(gè)簡(jiǎn)單的購(gòu)物車(chē)就是這么簡(jiǎn)單,大家可以按照我的思路進(jìn)行創(chuàng)作,在此基礎(chǔ)上在添加一些功能。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • asp.net 實(shí)現(xiàn)自定義Hashtable (.net)
  • asp.net Hashtable 遍歷寫(xiě)法
  • asp.net基于session實(shí)現(xiàn)購(gòu)物車(chē)的方法
  • asp.net 購(gòu)物車(chē)的實(shí)現(xiàn)淺析
  • asp.net 購(gòu)物車(chē)實(shí)現(xiàn)詳細(xì)代碼
  • asp.net基于HashTable實(shí)現(xiàn)購(gòu)物車(chē)的方法

標(biāo)簽:威海 廣西 烏蘭察布 隨州 梧州 甘孜 西雙版納 開(kāi)封

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET購(gòu)物車(chē)實(shí)現(xiàn)過(guò)程詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    黎平县| 樟树市| 温泉县| 永年县| 光山县| 湄潭县| 泽普县| 江山市| 安陆市| 永年县| 宜良县| 江都市| 朝阳区| 兴宁市| 双辽市| 孝感市| 阳春市| 循化| 龙井市| 施甸县| 那曲县| 奇台县| 麻阳| 镇沅| 岗巴县| 晴隆县| 托克逊县| 方山县| 突泉县| 志丹县| 武威市| 岳阳市| 宜春市| 昌平区| 桐柏县| 二连浩特市| 彭水| 松原市| 鲁山县| 遵义县| 凉山|