牛腩购物26:购物车页面的整合,判断是否是验证了登陆用户
点击立即购买-> 把商品加入到购物车中->弹出一个选择框,选择 是到 购物车还是继续购物->
shopcart.aspx->order_modify.aspx->order_confirm.aspx->order_ok.aspx
1:是否是登陆用户 User.Identity.IsAuthenticated
2:在tool.cs 工具类里面增加一个后台执行 js 的方法。(要注意这里的 js 加了一个 defer 表示网页全部执行之后,再执行 js。相当于是 body加载完之后,才执行js,这样可以避免,图片还没有加载完就弹出了js,那样的话,就会显示空白的图或者是空白的背景,所以要加 defer )
/// <summary>后台执行alert方法 /// /// </summary> /// <param name="mes"></param> /// <param name="_page"></param> public static void alert(string mes, Page _page) { _page.ClientScript.RegisterStartupScript(_page.GetType(), "message", "<script language='javascript' defer>
alert('" + mes + "');</script>"); } /// <summary>后台执行javascript代码 /// /// </summary> /// <param name="js">js代码,不需要写外面的script type·</param> /// <param name="_page"></param> public static void ExecJs(string js, Page _page) { _page.ClientScript.RegisterStartupScript(_page.GetType(), "ExecJs", "<script language='javascript' defer>
" + js + "</script>"); } /// <summary>后台执行alert方法,并跳转到指定页面 /// /// </summary> /// <param name="mes">alert的语句</param> /// <param name="url">跳转到的页面</param> /// <param name="_page"></param> public static void alert(string mes,string url, Page _page) { if (!string.IsNullOrEmpty(url)) { _page.ClientScript.RegisterStartupScript(_page.GetType(), "message", "<script language='javascript' defer>
alert('" + mes + "');location.href='" + url + "'</script>"); } else { _page.ClientScript.RegisterStartupScript(_page.GetType(), "message", "<script language='javascript' defer>
alert('" + mes + "');</script>"); } }
3:用户注销退出
FormsAuthentication.SignOut();
Session["cart"] = null; //退出登录的时候,要把购物车给清空掉
Response.Redirect("~/default.aspx");
4:批量添加进购物车,用到 checkbox
//批量添加进购物车 protected void btnAdd_Click(object sender, EventArgs e) { //我们在批量添加进购物的时候,需要对 收藏的 rep 进行循环,来判断到底是 哪个 checkbox 被选中了,选中的话,获取商品id foreach (RepeaterItem item in repFav.Items) { CheckBox chk = item.FindControl("chkId") as CheckBox; if (chk.Checked==true) { string proid = (item.FindControl("hfProid") as HiddenField).Value; Model.Product pt = new DAL.ProductDAO().GetModel(int.Parse(proid)); Model.User u = new DAL.UserDAO().GetModel(User.Identity.Name); decimal price = 0; if (pt != null && u != null) { if (u.type == "vip") //如果是会员,则是会员价 { price = pt.vipprice; } else { price = pt.memberprice; } } if (Session["cart"] != null) { //如果不等于空的话,我们就先取出来,然后再加进去 Niunan.Shop.Model.ShopCart sc = Session["cart"] as Niunan.Shop.Model.ShopCart; sc.Add(int.Parse(proid), new Model.ShopItem { proid = int.Parse(proid), price = price, quantity = 1 }); Session["cart"] = sc; } else { Niunan.Shop.Model.ShopCart sc = new Model.ShopCart(); sc.Add(int.Parse(proid), new Model.ShopItem { proid = int.Parse(proid), price = price, quantity = 1 }); //然后把购物车,放到session里面 Session["cart"] = sc; } //最后弹出提示 是继续购买,还是去购物车 Utility.Tool.ExecJs("if(confirm('添加成功,是否跳转到购物车?')){location.href='/shopcart.aspx'}
else{location.href='" + Request.Url.ToString() + "'}", this.Page); } } }
5:Repeater.ItemDataBound 事件
该事件在 Repeater 控件中的某一项被数据绑定后但尚未呈现在页面上之前发生。
//当rep循环绑定的时候,执行的事件 protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { string proid = (e.Item.FindControl("hfProid") as HiddenField).Value; Model.Product pt = new DAL.ProductDAO().GetModel(int.Parse(proid)); if (pt != null) { //再在 rep 里面找控件 Literal proname = e.Item.FindControl("litProName") as Literal; Literal litMember = e.Item.FindControl("litMember") as Literal; Literal litVip = e.Item.FindControl("litVip") as Literal; Literal litXiaoJi = e.Item.FindControl("litXiaoJi") as Literal; proname.Text = pt.proname; litMember.Text = pt.memberprice.ToString("c2"); litVip.Text = pt.vipprice.ToString("c2"); } } }