好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > ASP.NET購物車實現過程詳解

ASP.NET購物車實現過程詳解

熱門標簽:西寧電銷外呼系統公司 上海市三維地圖標注 海東防封電銷卡 安陸市地圖標注app 南昌自動外呼系統線路 寧德防封版電銷卡 聊城智能電銷機器人電話 云南外呼系統代理 辦公用地圖標注網點怎么操作

本文實例為大家分享了ASP.NET實現購物車的具體代碼,供大家參考,具體內容如下

1、 將test數據庫附加到數據庫管理系統中;數據庫中的book_info包含下列數據:

2、 新建一個網站,將images文件夾復制到網站中;

3、 在Default.aspx中,通過DataList控件展示數據庫中的所有數據,以行為主序,每行3列,單擊購買按鈕時,將商品的ID和數量保存到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中添加一個超鏈接,鏈接到shopcart.aspx,在shopcart.aspx中顯示用戶購買的商品信息。
提示:

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

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

B、頁面中添加一個GridView。
C、在page_load中,將dt實例化,建立各列。

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('你沒有登錄')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、這時可以看到用戶購買的商品,但不能修改數量,也不能刪除。
E、添加修改數量,刪除商品功能,在aspx頁面中定義GridView中的各列:

 asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
   Columns>
    asp:BoundField DataField="id" HeaderText="ID" />
    asp:BoundField DataField="name" HeaderText="名稱" />
    asp:BoundField DataField="price" HeaderText="價格" />
    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="總計" />
   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();
}

購物車代碼: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('你沒有登錄')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(); 
 
 } 
} 

制作一個簡單的購物車就是這么簡單,大家可以按照我的思路進行創作,在此基礎上在添加一些功能。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • asp.net 實現自定義Hashtable (.net)
  • asp.net Hashtable 遍歷寫法
  • asp.net基于session實現購物車的方法
  • asp.net 購物車的實現淺析
  • asp.net 購物車實現詳細代碼
  • asp.net基于HashTable實現購物車的方法

標簽:平涼 汕尾 洛陽 衢州 南寧 崇左 贛州 青海

巨人網絡通訊聲明:本文標題《ASP.NET購物車實現過程詳解》,本文關鍵詞  ASP.NET,購物車,實現,過程,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ASP.NET購物車實現過程詳解》相關的同類信息!
  • 本頁收集關于ASP.NET購物車實現過程詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 好大好紧好舒服| 国产????XXXX天美| 国产精品??的国产| 麻豆视频一区| 精品无码久久久久S888AV| 被夫上司欺辱的人妻HD中文版 | 舌头的湿润感对女性的性刺激作用| 日本女性爱| 国产精品成人免费综合| 艳妇荡女欲乱A片在线视| 午夜理伦三级做爰电影| 久久福利影视| 久久精品亚洲精品五月色| 小雪教练按住嗯嗯啊泳池| 大战丰满人妻性色AV偷偷红豆| 波霸欧美性猛交xxxxxx| 嘿咻视频在线观看| mm131美女做爰A片爽免费| 97人妻人人澡人人爽人人学生| 久久久久久精品免费看A级| bl顶弄巨大哭叫双性生子| 全国男人的天堂天堂网| 泰国男女做爰视频| 国产成人免费在线视频| 三上悠亚AV精品免费播放| 国产盗摄女厕一区二区三区| 国产三级久久精品| 伊人网色| 免费国产a| 扑克牌又疼又叫| 国产三级精品在线观看| 军营里娇喘呻吟声嗯啊| 国产片a| 丝袜脚榨精| 久久91精品国产91久久| 黃色A片三級三級三級免费看精东 秋霞电影网午夜鲁丝片国产 | 欧美日韩精品免费观看| xxxx曰本| 污污的视频在线观看| 和黑人房东在床2在线观看| 欧美在线视频免费播放|