购物网第三阶段总结笔记6:商品收藏页面

【一】:用户退出

由于使用了票据认证,所以退出很好做:

  

aspx代码:

<asp:LinkButton ID="LinkButton1" runat="server">退出</asp:LinkButton>

cs代码:

引入命名空间:

using System.Web.Security;

        //退出
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();//退出
            Response.Redirect("~/index.aspx");//重定向到首页
        }

【二】:商品收藏页面

(1):建立数据表:Shop_favorite



(2):使用动软生成器生成代码,复制到项目中,修改FavoriteDAO.cs,把分页和计算记录数的方法复制进去。

(3):

aspx代码:

                      <asp:Repeater ID="rep" runat="server">
                        <ItemTemplate>
                                      <tr bgcolor="#FFFFFF">
                            <td height="22">
                                <div align="center">
                                    <input name="shopid" type="checkbox" checked="" value="171">
                                </div>
                            </td>
                            <td style="padding-left: 5px" height="22">
                                <div align="left">


                                    <asp:Literal ID="litproname" runat="server"></asp:Literal>

                                    <%--隐藏域控件,添加此控件可以在后台获取当前行的id是什么--%>//注意,此处!
                                    <asp:HiddenField ID="hidproid" Value='<%#Eval("proid")%>' runat="server" />



                                    </div>
                            </td>
                            <td height="22">
                                <div align="center">
                                    <s>
                                        <asp:Literal ID="litmarketprice" runat="server"></asp:Literal></s>元</div>
                            </td>
                            <td height="22">
                                <div align="center">
                                    <asp:Literal ID="litmemberprice" runat="server"></asp:Literal>元</div>
                            </td>
                            <td height="22">
                                <div align="center">
                                    <asp:Literal ID="litvipprice" runat="server"></asp:Literal>元</div>
                            </td> 
                            <td height="22">
                                <div align="center">
                                    <asp:LinkButton ID="LinkButton1" OnClick="Del" CommandArgument='<%#Eval("id") %>' OnClientClick="return confirm('是否删除此商品?')" runat="server"> 
                                    <img src="../images/trash.gif" width="15" height="17" border="0">
                                    </asp:LinkButton>
                                       
                                        
                                        </div>
                            </td>
                        </tr>
                        </ItemTemplate>
                        </asp:Repeater>

cs代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.User
{
    public partial class myfavorite : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rep.DataSource = new MyShop.DAL.FavoriteDAO().GetList("username='"+User.Identity.Name+"'");
                rep.DataBind();
            }
        }

        //删除
        protected void Del(object sender, EventArgs e)
        {
            string id = (sender as LinkButton).CommandArgument;
            new MyShop.DAL.FavoriteDAO().Delete(int.Parse(id));
            Response.Redirect(Request.Url.ToString());
        }

        //Repeater的行循环事件,在此中绑定rep数据
        protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
            {
                HiddenField hidproid = e.Item.FindControl("hidproid") as HiddenField;
                Literal litproname = e.Item.FindControl("litproname") as Literal;
                Literal litmarketprice = e.Item.FindControl("litmarketprice") as Literal;
                Literal litmemberprice = e.Item.FindControl("litmemberprice") as Literal;
                Literal litvipprice = e.Item.FindControl("litvipprice") as Literal;

                MyShop.Model.Product model = new MyShop.DAL.ProductDAO().GetModel(int.Parse(hidproid.Value));
                if (model!=null)
                {
                    litproname.Text = "<a href='../pro.aspx?id="+model.id+"'>"+model.proname+"</a>";
                    litmarketprice.Text = model.marketprice.ToString("c2");
                    litmemberprice.Text = model.memberprice.ToString("c2");
                    litvipprice.Text = model.vipprice.ToString("c2");
                }
            }
        }
    }
}


(4):放入购物车按钮:【购物网第四阶段总结笔记1】



aspx代码:

Button按钮

                                    <asp:Button ID="Button1"  class="go-wenbenkuang" runat="server" Text="放入购物车" />

CheckBox按钮:

<asp:CheckBox ID="chk" ToolTip='<%#Eval("proid") %>' runat="server" />


cs代码:

//放入购物车
        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rep.Items)//循环Reprater控件的每一行
            {
                CheckBox chk = item.FindControl("chk") as CheckBox;
                if (chk.Checked)
                {
                    string proid = chk.ToolTip;

                    MyShop.Model.Product pro = new MyShop.DAL.ProductDAO().GetModel(int.Parse(proid));//取出商品
                    MyShop.Model.User user = new MyShop.DAL.UserDAO().GetModel(User.Identity.Name);//取出用户名

                    if (pro != null && user != null)//如果商品和用户都存在
                    {
                        decimal price = 0;
                        if (user.type == "vip")//如果用户是vip
                        {
                            price = pro.vipprice;
                        }
                        else
                        {
                            price = pro.memberprice;
                        }



                        if (Session["shopcart"] == null)//如果Session["shopcart"]不存在
                        {
                            Session["shopcart"] = new Model.ShopCart();//新建一个Session["shopcart"]
                        }
                        Model.ShopCart sc = Session["shopcart"] as Model.ShopCart;//根据Session["shopcart"]取出Model
                        sc.Add(int.Parse(proid), new Model.ShopItem()
                        {
                            Quantity = 1,
                            Proid = int.Parse(proid),
                            Price = price
                        });

                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>if(confirm('商品添加成功,是否跳转到购物车页面')){location.href='../shopcart.aspx'}else{location.href='" + Request.Url.ToString() + "'}</script>");
                    }      
                }
            }
        }



阅读更多
个人分类: 购物网学习
上一篇购物网第三阶段总结笔记5:商品内容页制作
下一篇购物网第四阶段总结笔记1:购物车页面
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭