简阳人论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 1182|回复: 1

[爆料] 网上购物车简单代码1

[复制链接]
发表于 2009-1-6 23:10:56 | 显示全部楼层 |阅读模式 来自 中国北京
购物商品.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 购物商品 的摘要说明
/// </summary>
public class 购物商品
{
    private int id;
    public int Id
    {
        get
        {
            return id;
        }
        set
        {
            this.id = value;
        }
    }
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private double price;

    public double Price
    {
        get { return price; }
        set { price = value; }
    }
    private int snum;

    public int Snum
    {
        get { return snum; }
        set { snum = value; }
    }
        public 购物商品(int id,string name,double price,int snum)
        {
        this.id = id;
        this.name = name;
        this.price = price;
        this.snum = snum;
        }
    public void AddSnum(int num)
    {
        this.snum += num;
    }
    public double getTotalPrice()
    {
        return snum * price;
    }
}
chaxun.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chaxun.aspx.cs" Inherits="chaxun" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>查询购物车信息</title>
    <script language="javascript">
     function love(id,name,price,snum)
       {
//          alert(id);
//          alert(name);
//          alert(price);
//          alert(snum);
          document.getElementById("show").style.display="block";
          document.getElementById("sname").value=name;
          document.getElementById("sprice").value=price;
          document.getElementById("snum").value=document.getElementById("n"+id).innerText;
          document.getElementById("sid").value=id;
       }
     function xiao()
       {
          document.getElementById("show").style.display="none";
       }
       function del(id)
       {
           if(window.confirm("确实要删除吗?"))
           {
              doSearch(id);
           }
           else
           {
            return false;
           }
       }
    function doSearch(type)
       {
           if(type=="mod")
           {
               var gounum=parseInt(document.getElementById("snum").value);
               if(gounum>0)
               {
                   var output="mai+++"+document.getElementById("sid").value;           
                   output+="+++"+document.getElementById("sname").value;  
                   output+="+++"+gounum;
                   CallServer(output,"");
               }
               else
               {
                  messageShow("请输入大于0的数字");
               }
           }
           else
           {//删除乐
               var output="del+++"+type;           
               output+="+++"+document.getElementById("f"+type).innerText;
               output+="+++0";//0没有意义
               CallServer(output,"");
           }
       }
       function receive(text)
       {
          var arr;
          if(text.indexOf("+++")!=-1)
          {
             arr=text.split("+++");
             if(arr[0]=="mai")
             {
             document.getElementById("n"+arr[1]).innerText=arr[3];
             messageShow(arr[2]+arr[3]+arr[4]);
             }
             else
             {  
                //document.getElementById("n"+arr[1]).innerText=arr[2];
                messageShow(arr[1]+arr[2]);
                window.setTimeout("shua()",3000);
             }
          }
          else
          {
            messageShow(text);
          }
          document.getElementById("snum").value="";
          xiao();
//          document.getElementById("form1").reset();
//          document.getElementById("show").innerHTML=text;
//            alert(document.getElementById("show"));
       }
       function shua()
       {
         location.reload();
       }
        var j;
       function messageShow(text)
       {
          var w=document.body.offsetWidth;
          var h=document.body.offsetHeight;
          var mw=document.getElementById("message").style.width;
          var mh=document.getElementById("message").style.height;
          var toph=parseInt(document.body.scrollTop);
          //alert(toph);
          document.getElementById("message").style.left=parseInt(w)-parseInt(mw)-30;
          document.getElementById("message").style.top=parseInt(h)+toph-parseInt(mh);
          document.getElementById("message").innerHTML=text;
          document.getElementById("message").style.display="block";
          j=window.setTimeout("messageXiao()",3000);
       }
       function messageXiao()
       {
          window.clearTimeout(j);
          document.getElementById("message").innerText="";
          document.getElementById("message").style.display="none";
       }
    </script>
</head>
<body>
    <form id="form1" runat="server">
     <div id="message" style="position:absolute; left:0px; top:0px; display:none; width:150px; height:60px;background-color:#8cacbb"></div>
       <div style="position:absolute;left:400px; top:18px; display:none; background-color:#8cacbb" id="show">
         <table>
           <tr><td>商品名</td><td><input id="sname" readonly/><input type="hidden" id="sid"/></td></tr>
           <tr><td>单价</td><td><input id="sprice" readonly/></td></tr>           
           <tr><td>数量</td><td><input id="snum" /></td></tr>
           <tr><td><input type="button" value="购买" onclick="doSearch('mod')" /></td><td><input type="button" value="取消" onclick="xiao()"/></td></tr>
         </table>
     </div>
    <div>
        <%
            String temppage = Request.QueryString["page"];
            int page = 0;
            if (temppage == null || temppage.Trim() == "")
            {
                page = 1;
            }
            else {
                page = Convert.ToInt32(temppage);
            }
            if (page < 1)
            {
                page = 1;
            }
            Hashtable chaht = (Hashtable)Session["shopping"];
            if(chaht.Count==0)
            {
                %>
                <table>
                 <tr><td>您还没有购物,请回到商城中完成购物。</td></tr>
                </table>
                <%
            }else
            {
            int hangnum =chaht.Count;
            int pagecount = Convert.ToInt32(ConfigurationManager.AppSettings["pagecount"]);
            int MaxPage = (int)Math.Ceiling((double)hangnum / pagecount);
            if (page > MaxPage)
            {
                page = MaxPage;
            }
            object[] o = new object[chaht.Count];
            chaht.CopyTo(o, 0);
            int starthang = (page - 1) * pagecount;  //page=2  下标为 1*3=3 从3号索引开始,即可第4个
            int endhang = starthang + pagecount-1;
            %>
            <table><tr><td>选择</td><td>商品名</td><td>单价</td><td>购物数量</td><td>操作方式</td></tr>
            <%
             for (int i = starthang; i < o.Length && i <= endhang; i++)
           {
               DictionaryEntry de = (DictionaryEntry)o;
               购物商品 s = (购物商品)de.Value;
               %>
                <tr><td><input type="checkbox" id="cid" value="<%=s.Id %>" /></td><td id="f<%=s.Id %>"><%=s.Name %></td><td><%=s.Price %></td><td id="n<%=s.Id %>"><%=s.Snum %></td><td><input type="button" value="更新" onclick="love('<%=s.Id %>','<%=s.Name %>','<%=s.Price %>','<%=s.Snum %>')"/><input type="button" value="删除" onclick="del('<%=s.Id %>')"/></td></tr>
               <%
            }
         %>
         </table>
         <table>
          <tr>
              <td><a href="chaxun.aspx?page=1">首页</a></td>
              <%
                  if (page == 1)
                  {
                      %>
                       <td>上一页</td>
                      <%
                  }
                  else
                  {
                      %>
                      <td><a href="chaxun.aspx?page=<%=page-1 %>">上一页</a></td>
                      <%
                  }
                  if (page ==MaxPage)
                  {
                      %>
                       <td>下一页</td>
                      <%
                  }
                  else
                  {
                      %>
                      <td><a href="chaxun.aspx?page=<%=page+1 %>">下一页</a></td>
                      <%
                  }
               %>
                <td><a href="chaxun.aspx?page=<%=MaxPage %>">尾页</a></td>
              </tr>
         </table>
                <%
            }
             %>
    </div>
    <div>
    <%
      if(chaht.Count>0)
      {
    %>
    <input type="button" onclick="fukuan()" value="到收银台付款"/></div>
    <%
     }
     %>
    <script language="javascript">
    var ziwin;
      function fukuan()
      {
        ziwin= window.open ("shouyin.aspx", "", "height=600, width=800, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
        window.setTimeout("guanbi()",1000);
      }
      function guanbi()
      {
         window.close();
      }
    </script>
    </form>
</body>
 楼主| 发表于 2009-1-6 23:21:13 | 显示全部楼层 来自 中国北京
在本代码段最后加上</html>
本下程序由“网上购物车简单代码12345”构成,请感兴趣的朋友看完,由于是初学,请多多指教!
*滑块验证:
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|小黑屋|手机版|Archiver|简阳人论坛 ( 蜀ICP备07502275号-3 )

川公网安备 51200002000214号

GMT+8, 2024-5-15 00:20 , Processed in 0.401213 second(s), 10 queries , Gzip On, File On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表