ASP.NET购物车的实现及结算处理源代码

该博客通过实例展示了如何在ASP.NET中实现一个简单的购物车功能,利用Session对象存储数据。文中详细介绍了Book类的定义,包括属性和方法,以及购物车集合Books类的实现,包括添加、删除和统计书籍数量等操作。此外,还提到了如何将数据绑定到DataGrid控件进行展示,并提供了处理图片的ImageView页面。最后,给出了购物车页面WebForm1的实现,包括DataGrid展示和数量输入验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 ASP.NET购物车的实现及结算处理源代码


作者:Mr.zhao

本示例利用Session对象来实现一个简单的购物车。主要用于教学演示。

Book类
此类主是代表购物车的一本书
using System;

namespace CartTest
{
 /// <summary>
 /// Books 的摘要说明。
 /// </summary>
 public class Book
 {
  string bookid;
  string title;
  decimal price;
  int num;


  public Book()
  {
  }

  /// <summary>
  /// ID
  /// </summary>
  public string BookID
  {
   get{return bookid;}
   set{bookid=value;}
  }
  /// <summary>
  /// 书名
  /// </summary>
  public string Title
  {
   get{return title;}
   set{title=value;}
  }
 
  /// <summary>
  /// 金额
  /// </summary>
  public decimal Price
  {
   get{return price;}
   set{price=value;
   sum=price*num;
   }
  }
  /// <summary>
  /// 数量
  /// </summary>
  public int Num
  {
   get{return num;}
   set{num=value;
   sum=price*num;
   }
  }
  decimal sum=0m;
  //一种书的总金额
  public decimal Sum
  {
   get{return sum;}
   set{sum=value;}
  }
 }

}

//购物车集合
//Books 用户所有订购的书 ,实现IEnumerable接口,我们可以将其绑定到datagrid控件
using System;
using System.Collections;
namespace CartTest
{
 /// <summary>
 ///
 /// </summary>
 public class Books :IEnumerable
 {
  Hashtable ht=null;
  public Books()
  {
   ht=new Hashtable();
  
  }

  public Books(int count)
  {
   ht=new Hashtable(count);
  }

  public void Add(Book b)
  {
   //如果集合中有相同ID的书,则对书的数量进行相加
   if(ht.ContainsKey(b.BookID))
   {
 ((Book)ht[b.BookID]).Num=((Book)ht[b.BookID]).Num+b.Num;
   
   }
   else
   {
    ht.Add(b.BookID,b);
   }
  }

  public void Remove(string bookid)
  {
   if(ht.ContainsKey(bookid))
   ht.Remove(bookid);
  }
//统计有多少种书
  public int Count
  {
   get
   {
    return ht.Count;
   }
  }

  public void Clear()
  {
   ht.Clear();
  }

  public Book this[string bookid]
  {
   get
   {
    if(ht.ContainsKey(bookid))
     return (Book)ht[bookid];
    return null;
   }
  }
  #region IEnumerable 成员

  public IEnumerator GetEnumerator()
  {
   // TODO:  添加 Books.GetEnumerator 实现
   return ht.Values.GetEnumerator();
  }

  #endregion
 }
}

 

//此页面主要是用于显示所有的书。用的是DataList来自定义显示模板。但是实际上可以使用DataGrid来处理。DataGrid也可以实现分页功能及自定义模板。只要将dDatagrid设为一个模板列,然后将DataList里的模板列代码Copy过去即可。
//此页面中每本书都要显示封面。这个问题我们可以通过一个过渡页来处理图片数据

<%@ Page language="c#" Codebehind="BookList.aspx.cs" AutoEventWireup="false" Inherits="CartTest.BookList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>BookList</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="javascript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  <LINK href="http://localhost/CartTest/StyleSheet1.css" type="text/css" rel="stylesheet">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:datalist id="DataList1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px" runat="server"
    DataKeyField="BookGuid" Width="650">
    <ItemTemplate>
     <TABLE id="Table14" cellSpacing="1" cellPadding="1" border="0">
      <TR>
       <TD>
        <a href='<%# "BookView.aspx?BookID="+DataBinder.Eval(Container, "DataItem.BookGuid") %>'>
  <!--imageview.aspx页面专用来处理书的图片-->    <asp:Image id=Image1 runat="server" Width="120px" Height="144px" ImageUrl='<%# "ImageView.aspx?imgid="+DataBinder.Eval(Container, "DataItem.BookGuid") %>'>
         </asp:Image>
        </a>
       </TD>
       <TD vAlign="top">
        <TABLE id="Table15" cellSpacing="1" cellPadding="1" width="300" border="1">
         <TR>
          <TD>书名:
           <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.BookTitle") %>'>
           </asp:Label></TD>
         </TR>
         <TR>
          <TD>图书简介:
           <asp:Label id=Label2 style="OVERFLOW: hidden; TEXT-OVERFLOW: ellipsis" runat="server" Width="496" Text='<%# "<nobr>"+DataBinder.Eval(Container, "DataItem.BookComment")+"/<nobr>"%>' Height="50px">
           </asp:Label></TD>
         </TR>
         <TR>
          <TD>金额:
           <asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Price","{0:C}") %>'>
           </asp:Label></TD>
         </TR>
        </TABLE>
       </TD>
      </TR>
      <TR>
       <TD>
        <asp:Label id="Label4" runat="server">日期:</asp:Label>
        <asp:Label id=Label5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.PublishDate", "{0:D}") %>'>
        </asp:Label></TD>
       <TD align="right">
        <asp:ImageButton id="Imagebutton1" runat="server" ImageUrl="a.gif" CommandName="AddCart"></asp:ImageButton></TD>
      </TR>
     </TABLE>
    </ItemTemplate>
    <AlternatingItemTemplate>
     <TABLE id="Table4" cellSpacing="1" cellPadding="1" bgColor="#eefeff" border="0">
      <TR>
       <TD>
        <a href='<%# "BookView.aspx?BookID="+DataBinder.Eval(Container, "DataItem.BookGuid") %>'>
 <!--imageview.aspx页面专用来处理书的图片-->        <asp:Image id=Image2 runat="server" Width="120px" Height="144px" ImageUrl='<%# "ImageView.aspx?imgid="+DataBinder.Eval(Container, "DataItem.BookGuid") %>'>
         </asp:Image></a></TD>
       <TD vAlign="top">
        <TABLE id="Table5" cellSpacing="1" cellPadding="1" width="300" border="1">
         <TR>
          <TD>书名:
           <asp:Label id=Label6 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.BookTitle") %>'>
           </asp:Label></TD>
         </TR>
         <TR>
          <TD>图书简介:
           <asp:Label id=Label7 style="OVERFLOW: hidden; TEXT-OVERFLOW: ellipsis" runat="server" Width="496px" Text='<%# DataBinder.Eval(Container, "DataItem.BookComment") %>' Height="50px">
           </asp:Label></TD>
         </TR>
         <TR>
          <TD>金额:
           <asp:Label id=Label8 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Price") %>'>
           </asp:Label></TD>
         </TR>
        </TABLE>
       </TD>
      </TR>
      <TR>
       <TD>
        <asp:Label id="Label9" runat="server">日期:</asp:Label>
        <asp:Label id=Label10 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.PublishDate") %>'>
        </asp:Label></TD>
       <TD align="right">
        <asp:ImageButton id="Imagebutton2" runat="server" ImageUrl="a.gif"></asp:ImageButton></TD>
      </TR>
     </TABLE>
    </AlternatingItemTemplate>
   </asp:datalist></form>
 </body>
</HTML>

//CS CODE
using System;
using System.Collections;
using System.Comp

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值