国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

asp.net 實(shí)現(xiàn)購(gòu)物車

2024-07-10 13:06:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

<%@ page language="c#" codebehind="shoppingcart.aspx.cs" autoeventwireup="false" inherits="myshop.shoppingcart" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
  <head>
  <title>shoppingcart</title>
  <meta http-equiv="content-type" content="text/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>
 <body>
  <center>
   <form id="form1" runat="server">
    <table width="500" border="0" cellspacing="0" cellpadding="0">
     <tr>
      <td>
       <asp:datagrid id="shoppingcartdlt" runat="server" width="500" backcolor="white" bordercolor="black"
        showfooter="false" cellpadding="3" cellspacing="0" font-name="verdana" font-size="8pt" headerstyle-backcolor="#cecfd6"
        autogeneratecolumns="false" maintainstate="true">
        <columns>
         <asp:templatecolumn headertext="刪除">
          <itemtemplate>
           <center>
            <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="單價(jià)" />
         <asp:templatecolumn headertext="數(shù)量">
          <itemtemplate>
           <asp:textbox id="counttb" runat="server" text='<%#databinder.eval(container.dataitem,"prodcount")%>'>
           </asp:textbox>
          </itemtemplate>
         </asp:templatecolumn>
         <asp:boundcolumn datafield="totalprice" headertext="小計(jì)(元)" />
        </columns>
       </asp:datagrid></td>
     </tr>
    </table>
    <br>
    <table width="500" border="0" cellspacing="0" cellpadding="0">
     <tr>
      <td><asp:button id="update" runat="server" text="更新我的購(gòu)物車"  cssclass="button2" /></td>
      <td><asp:button id="checkout" runat="server" text="結(jié)算"  cssclass="button5" />&nbsp;&nbsp;<input type="button" name="close2" value="繼續(xù)購(gòu)物" onclick="window.close();return false;"
        class="button2"></td>
      <td align="right"><br>
       <asp:label id="label" runat="server" width="100px" visible="true" forecolor="#ff8080" height="18px"></asp:label></td>
     </tr>
    </table>
   </form>
  </center>
 </body>
</html>
=======================================================================================
以上為html頁(yè)面部分
==========================================================================================


using system;
using system.collections;
using system.componentmodel;
using system.web.sessionstate;
using system.web;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.data;
using system.data.oledb;
using system.configuration;

namespace myshop
{
 /// <summary>
 /// shoppingcart 的摘要說(shuō)明。
 /// </summary>
 public class shoppingcart : system.web.ui.page
 {
  protected system.web.ui.webcontrols.datagrid shoppingcartdlt;
  protected system.web.ui.webcontrols.button update;
  protected system.web.ui.webcontrols.button checkout;
  protected system.web.ui.htmlcontrols.htmlform form1;
  protected system.web.ui.webcontrols.label label;
  protected system.web.ui.webcontrols.checkbox     chkproductid;
  protected system.web.ui.webcontrols.textbox      txtcount;
  protected system.web.ui.webcontrols.textbox      counttb;
  string addproid;
 
  private void page_load(object sender, system.eventargs e)
  {
   try
   {
    if (session["logon"]!="yes"||session["username"]==null)
    {
     response.redirect("error.htm") ;
    }
   }
   catch
   {
    response.redirect("error.htm") ;
   }                                         /////////////查看用戶是否已經(jīng)登陸。

   if(!ispostback)
   {
    if(request.params["mode"]=="view")         //檢測(cè)是否為直接查看購(gòu)物車。
    {
     viewshoppingcart();
     caculator();
    }
    if(request.params["productid"]!=null||request.params["productid"]!="")
    {
     addproid=request["productid"];
     updateshoppingcart();
     caculator();
    }
   }// 在此處放置用戶代碼以初始化頁(yè)面
  }

  public void createcarttable()   //創(chuàng)建購(gòu)物車
  { 
   dataset ds = new dataset();
   datatable newdt=new datatable("carttable");
   ds.tables.add(newdt);
   datacolumn newdc;
   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("unitprice",system.type.gettype("system.double"));
   ds.tables["carttable"].columns.add(newdc);
             
   newdc=new datacolumn("totalprice",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() 中 newdr[5]="0"; 行,已被注銷,
   ds.tables["carttable"].columns.add(newdc);

   session["mycarttable"]=newdt;
   shoppingcartdlt.datasource=ds.tables["carttable"].defaultview;
   shoppingcartdlt.databind();
      
  }

  public void updateshoppingcart()
  {
   if(session["mycarttable"]==null)//session["mycarttable"]==null
   {
    createcarttable();                                    //調(diào)用函數(shù)createcarttable()新建一個(gè)datatable
    writeshoppingcart();


   }
   else
   {                                                         //如果購(gòu)物藍(lán)中已有商品,則需要對(duì)購(gòu)物信息表datatable進(jìn)行更新,并將其棒定到shoppingcartdlt
                 
    writeshoppingcart();
   }
  }

  public void viewshoppingcart()                               //查看購(gòu)物車
  {
   if(session["mycarttable"]!=null)
   {
    datatable viewtable=new datatable("nowcarttable");
    viewtable=(datatable)session["mycarttable"];
    shoppingcartdlt.datasource = viewtable.defaultview;         //購(gòu)物車棒定到shoppingcartdlt
    shoppingcartdlt.databind();
   }
             
  }

  public void writeshoppingcart()
  {
   if(request.params["mode"]!="view")                             //檢查是否是直接查看購(gòu)物車,如果直接查看,就不再寫mycarttable
   {
    datatable nowtable=new datatable("nowcarttable");
    nowtable=(datatable)session["mycarttable"];
    int pn=nowtable.rows.count;

    int i=0;
    bool hasone=false;
    int nowprodid;
                 
    while(i<pn && !hasone)
    {
     nowprodid=int32.parse(nowtable.rows[i][0].tostring());
     if(nowprodid==int32.parse(addproid))                                   //判斷購(gòu)物信息表中,是否存有當(dāng)前放入商品。 if(nowprodid==int32.parse(addproid))
     {
      hasone=true;
     }
     else
     {
      i++;
     }

    }
    if(hasone)                          
    {                                                      //如果已有該商品,則 hasone=true,更改該數(shù)據(jù)行
     datarow olddr;
     olddr=nowtable.rows[i];
     olddr["prodcount"]=int32.parse(olddr["prodcount"].tostring())+1;
     olddr["totalprice"]=int32.parse(olddr["prodcount"].tostring())*double.parse(olddr["unitprice"].tostring());
    }
    else
    {                                                      //如果沒(méi)有該商品,在表中新加如一行。
     datarow newdr;
     double unitp;
     string strcon="provider=microsoft.jet.oledb.4.0;data source="+server.mappath(configurationsettings.appsettings["mdbpath2"])+";";
     oledbconnection myconnection = new oledbconnection(strcon);
     string strsql= "select *  from pro where product_id="+addproid+"";
     oledbdataadapter mycommand = new  oledbdataadapter(strsql, myconnection);
     dataset ds = new dataset();
     mycommand.fill(ds, "addp");

     newdr=nowtable.newrow();
     newdr[0]=addproid;
                       
     newdr[2]=ds.tables["addp"].rows[0]["product_name"].tostring();
     unitp=double.parse(ds.tables["addp"].rows[0]["product_memprice"].tostring());        //會(huì)員價(jià)
                      
     newdr[3]=unitp;
     newdr[4]=unitp;                                           //第一次讀庫(kù),所以總價(jià)格和單價(jià)是一樣的。
     //newdr[5]="0";
     nowtable.rows.add(newdr);
                
     myconnection.close();
                    
    }
                 
    shoppingcartdlt.datasource = nowtable.defaultview;         //將更新后的 datatable棒定到shoppingcartdlt
    shoppingcartdlt.databind();

    session["mycarttable"] = nowtable; 
    //重新保存更新過(guò)的datatable
   }   
  }


 
  public void caculator()
  {
   if(session["mycarttable"]!=null)                         //購(gòu)物車是否為空
   {
    int h;
    double totalpri;
    totalpri=0;
    datatable nowtable3=new datatable("nowcarttable3");
    nowtable3=(datatable)session["mycarttable"];
    if(nowtable3.rows.count>0)                               //返回購(gòu)物車中是否有貨物
    {
     for(h=0;h<=nowtable3.rows.count-1;h++)
     {       
      totalpri=totalpri+int32.parse(nowtable3.rows[h][4].tostring());//double.parse((string)totaltext.text);
                                        
     }
     label.text="總計(jì): "+totalpri.tostring()+" 元" ;
    }
   }
  
  }

  public void update()
  {
  
   int i;
   int j;
   int k;
   arraylist deleteitem = new arraylist(10);
   datagriditem _item ;
   j=0;
   int deleteid;
     
            
   k=0;
   datatable nowtable2=new datatable("nowcarttable2");
   nowtable2=(datatable)session["mycarttable"];
    
           
   
   
   
   for(i=0;i<=this.shoppingcartdlt.items.count-1;i++)
   {
    _item = this.shoppingcartdlt.items[i];
    textbox counttext=(textbox)this.shoppingcartdlt.items[i].cells[4].findcontrol("counttb");//controls[1];//_item.findcontrol("counttb");
    checkbox productidcheck =(checkbox) _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.checked)
    {
     nowtable2.rows[i][5] = 1;//添加刪除標(biāo)記1
     j=j+1;
    }
    
   }
   string strexpr="isdeleted>0";                     //http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/cpref/html/frlrfsystemdatadatatableclassselecttopic.asp
   datarow[] foundrows = nowtable2.select(strexpr);
   for(int m = 0; m < foundrows.length; m ++)
   {
    //console.writeline(foundrows[i][0]);
    foundrows[m].delete();
   }
          
            
             
                             
   
   shoppingcartdlt.datasource = nowtable2.defaultview;       
   shoppingcartdlt.databind();
   session["mycarttable"] = nowtable2;
   caculator();

  }

  #region web 窗體設(shè)計(jì)器生成的代碼
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 該調(diào)用是 asp.net web 窗體設(shè)計(jì)器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
 
  /// <summary>
  /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </summary>
  private void initializecomponent()
  {   
   this.update.click += new system.eventhandler(this.update_click);
   this.checkout.click += new system.eventhandler(this.checkout_click);
   this.load += new system.eventhandler(this.page_load);

  }
  #endregion

  private void update_click(object sender, system.eventargs e)
  {
   update();
 
  }

  private void checkout_click(object sender, system.eventargs e)
  {
   update();
   response.redirect("checkout.aspx");
  }
 }
}


 



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 定边县| 龙陵县| 西乌珠穆沁旗| 安多县| 武鸣县| 尉犁县| 长子县| 梨树县| 凭祥市| 商丘市| 凭祥市| 阿荣旗| 壤塘县| 海伦市| 西乡县| 新郑市| 隆林| 建阳市| 广州市| 拜城县| 五台县| 东港市| 上栗县| 荥经县| 沐川县| 三亚市| 寻乌县| 巴林右旗| 瑞金市| 舞钢市| 唐海县| 民勤县| 阆中市| 孝义市| 保康县| 兰州市| 博乐市| 微博| 南皮县| 东宁县| 和龙市|