购物车

private void BindCartList()
    {
        DataTable dt = new DataTable();
        //如果Session变量存在,则直接获取
        if (Session["Cart"] != null)
        {
            dt = (DataTable)Session["Cart"];
        }
        else//如果Session变量不存在,创建存储数据的表结构
        {
            dt.Columns.Add(new DataColumn("PID", typeof(Int32)));
            dt.Columns.Add(new DataColumn("PName", typeof(String)));
            dt.Columns.Add(new DataColumn("ImageUrl", typeof(String)));
            dt.Columns.Add(new DataColumn("Price", typeof(String)));
            dt.Columns.Add(new DataColumn("Amount", typeof(Int32)));
        }

        //PID不为null
        //则表示选中一件商品添加到购物车
        if (ProductNo != null)
        {
            //先判断购物车中是否已经存在该商品
            Boolean IsExist = false;
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt32(dr["PID"].ToString()) == ProductNo)
                {
                    IsExist = true;
                    break;
                }
            }
            //如果购物车中存在该商品,则提示客户该商品不会被重复添加到购物车
            if (IsExist)
            {
                //ScriptManager.RegisterStartupScript(
                //    this, typeof(Page), "alertExist", "alert('您选择的商品(编号:" + ProductNo + ")已在购物车存在!')", true);
            }
            else//如果购物车中不存在该商品,则添加到购物车
            {
                if (ProductNo != 0)
                {
                    string strqry = string.Format("Select * From 商品表 where PID='{0}'", ProductNo);
                    DataTable dtRow = DBFun.dataTable(strqry);
                    dt.Rows.Add(new object[]{
                    Convert.ToInt32(dtRow.Rows[0]["PID"].ToString()),
                    dtRow.Rows[0]["PName"].ToString(),
                    dtRow.Rows[0]["ImageUrl"].ToString(),
                    dtRow.Rows[0]["Price"].ToString(),
                    1});
                }
            }
        }

        gvCart.DataSource = dt;
        gvCart.DataBind();
        Session["Cart"] = dt;
    }



    protected void gvCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
         //点击删除时从DataTable中删除对应的数据行  
        if (Session["Cart"] != null)
        {
            DataTable dt = (DataTable)Session["Cart"];
            dt.Rows.RemoveAt(e.RowIndex); 
            dt.AcceptChanges();
            Session["Cart"] = dt;
            Response.Redirect("ShoppingCart.aspx");
        }
    }

    protected void gvCart_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //GridView行的加亮显示功能  
            e.Row.Attributes.Add("onmouseover", "b=this.style.backgroundColor;this.style.backgroundColor='#E1ECEE'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=b");
            //给+号图片和-号图片添加客户端click事件  
            //用JavaScript实现数量的+1和-1  
            TextBox tb = (TextBox)e.Row.FindControl("txtAmount");
            ((ImageButton)e.Row.FindControl("imgReduce")).Attributes.Add("onclick", "Reduce(" + tb.ClientID + ")");
            ((ImageButton)e.Row.FindControl("imgPlus")).Attributes.Add("onclick", "Plus(" + tb.ClientID + ")");
            
            //根据商品单价和数量计算购物车中商品的总金额  
            DataRowView drv = (DataRowView)e.Row.DataItem;

            Total += (double)(double.Parse(drv["Price"].ToString())) * Int32.Parse(tb.Text);
            Response.Cookies["TotalPrice"].Value = Total.ToString();//<------

        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            //将总金额显示在金额一列对应的Footer单元格  
            e.Row.Cells[1].Text = "金额总计:";
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[2].Text = Total.ToString("c2");
            e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
            e.Row.Cells[2].Font.Size = 15;
          
            
        }  


           

    }

    protected void imgbtnTotal_Click(object sender, ImageClickEventArgs e)
    {
        //遍历GridView,根据每行的文本框中的值  
        //修改DataTable中对应行中数量一列的值  
        if (Session["Cart"] != null)
        {
            DataTable dt = (DataTable)Session["Cart"];
            for (int i = 0; i < gvCart.Rows.Count; i++)
            {
                dt.Rows[i]["Amount"] = ((TextBox)gvCart.Rows[i].FindControl("txtAmount")).Text;
            }
            dt.AcceptChanges();
            Session["Cart"] = dt;
            BindCartList();
            //Response.Redirect("ShoppingCart.aspx");
        }  
    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //业务逻辑
        string subject = "";
        string subject1 = "";

        if (Session["Cart"] != null)
        {

            DataTable dt = (DataTable)Session["Cart"];
            for (int i = 0; i < gvCart.Rows.Count; i++)
            {
                subject += dt.Rows[i]["PID"].ToString().Trim();
                dt.Rows[i]["Amount"] = ((TextBox)gvCart.Rows[i].FindControl("txtAmount")).Text;
                subject += "=" + dt.Rows[i]["Amount"] + ",";

                subject1 += dt.Rows[i]["PName"].ToString().Trim();
                subject1 += "=" + dt.Rows[i]["Amount"] + ",";

            }
            Response.Cookies["subjuct"].Value = Server.UrlEncode(subject);//<------
            Response.Cookies["subjuct1"].Value = Server.UrlEncode(subject1);

          Response.Redirect("default.aspx");

        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("index.aspx");
    }
前台代码:

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>


    <br /><br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   <ContentTemplate>
    <div id="zone">
        <div align="left" class="divBorder" style="	height:45px; font-family:微软雅黑;font-size:20px;font-weight:bold; font-style: normal;">
            我的购物车
        </div>
        
        <div class="divBorder">
            <asp:GridView ID="gvCart" runat="server" DataKeyNames="PID" AutoGenerateColumns="False"
                ShowFooter="True" Width="98%" OnRowDataBound="gvCart_RowDataBound" 
                OnRowDeleting="gvCart_RowDeleting" BorderStyle="None" GridLines="None">
                <Columns>                   
                    <asp:BoundField DataField="PName" HeaderText="商品"/>  
                                      
                    <asp:TemplateField HeaderText="单价">
                        <ItemStyle Width="280px" Height="60px"/>
                        <ItemTemplate>
                            <%# Eval("Price")%>
                        </ItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="数量">
                        <ItemStyle Width="250px" />
                        <ItemTemplate>
                          <asp:ImageButton ID="imgReduce" runat="server" ImageUrl="~/image/ProductList/bag_open.png" OnClick="imgbtnTotal_Click" />
                          <asp:TextBox ID="txtAmount" Width="75px" Height="28px" runat="server" 
                                Text='<%# Eval("Amount") %>' Enabled="False"></asp:TextBox>
                          <asp:ImageButton ID="imgPlus" runat="server" ImageUrl="~/image/ProductList/bag_close.png" OnClick="imgbtnTotal_Click" />

                       </ItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="操作" ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                                CommandName="Delete" Text="删除"></asp:LinkButton>
                        </ItemTemplate>
                        <ItemStyle Width="100px" />
                    </asp:TemplateField>
                    
                </Columns>
                <EmptyDataTemplate>
                    您的购物车中没有任何商品。
                </EmptyDataTemplate>
            </asp:GridView>

        </div>
        <br />

    </div>
        </ContentTemplate>
    </asp:UpdatePanel>

        <div style="width:1240px;">
        <table style="width:100%">
            <tr>
            <td width="600px">
                 </td>
            <td align="right">
                 <asp:ImageButton ID="ImageButton2" runat="server" 
                    ImageUrl="~/image/ProductList/继续购物.png" οnclick="ImageButton2_Click" />
  
                <asp:ImageButton ID="ImageButton1" runat="server" 
                    ImageUrl="image/ProductList/去结账.png" οnclick="ImageButton1_Click" />
             </td>
            </tr>
        </table>
            
            
        </div>
   
     </form>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值