咨询电话:186 7916 6165 咨询电话:186 7916 6165 (微信同号)    在线QQ:181796286
NEWS BLOG ·
学无止境
关注开优网络 关注前沿
Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入
ASP.NET常用服务器控件方法及属性

ASP.NET Web 开发之购物车

发表日期:2015-12-27    文章编辑:南昌开优网络    浏览次数:4059    标签:ASP.NET应用

一、在Model层中创建购物车只的每一项类ShopItem.cs
 public class ShopItem
    {
        private int _proid;//商品id        

        public int Proid
        {
            get { return _proid; }
            set { _proid = value; }
        }
        private decimal _price;//商品单价

        public decimal Price
        {
            get { return _price; }
            set { _price = value; }
        }
        private int _quantity;//商品数量

        public int Quantity
        {
            get { return _quantity; }
            set { _quantity = value; }
        }
    }
二、在Model层中创建购物车类ShopCatr.cs
public class ShopCatr
    {
        private Hashtable _sc = new Hashtable();
        /// <summary>向购物车中添加商品
        /// 
        /// </summary>
        /// <param name="proid">商品的id</param>
        /// <param name="item">购物车</param>
        public void Add(int proid, ShopItem item)
        {
            if (_sc[proid] == null)
            {
                _sc.Add(proid, item);
            }
            else
            {
                //已存在该商品
                ShopItem si = _sc[proid] as ShopItem;
                si.Quantity += 1;
                _sc[proid] = si;
            }
        }

        /// <summary>删除购物车中的商品
        /// 
        /// </summary>
        /// <param name="proid">商品的id</param>
        public void Del(int proid)
        {
            if (_sc[proid] != null)
            {
                _sc.Remove(proid);
            }
        }
        /// <summary>修改购物车中商品的数量
        /// 
        /// </summary>
        /// <param name="proid">商品ID</param>
        /// <param name="quantity">商品数量</param>
        public void Mod(int proid, int quantity)
        {
            if (_sc[proid] != null)
            {
                if (quantity > 0)
                {
                    ShopItem si = _sc[proid] as ShopItem;
                    si.Quantity = quantity;
                    _sc[proid] = si;
                }
            }
        }

        /// <summary>获取购物车中商品的种类数
        /// 
        /// </summary>
        /// <returns></returns>
        public int GetItemCount()
        {
            return _sc.Count;
        }
        /// <summary>获取购物车中商品的总数量
        /// 
        /// </summary>
        /// <returns></returns>
        public int GetItemTotalCount()
        {
            int total = 0;
            foreach (ShopItem item in _sc.Values)
            {
                total += item.Quantity;
            }
            return total;
        }

        /// <summary>获取购物车中项的内容用于绑定数据控件
        /// 
        /// </summary>
        /// <returns></returns>
        public ICollection GetItemList()
        {
            return _sc.Values;
        }

        /// <summary>获取购物车的总价
        /// 
        /// </summary>
        /// <returns></returns>
        public decimal GetTotalPrice()
        {
            decimal total = 0;
            foreach (ShopItem item in _sc.Values)
            {
                total += item.Price * item.Quantity;
            }
            return total;
        }
    }

加入收藏
protected void lbtnFav_Click(object sender, EventArgs e)
{
if (!User.Identity.IsAuthenticated)
{
Utility.Tool.Alert("请先登录", this.Page);
return;
}

string proid = (sender as LinkButton).CommandArgument; //该商品ID
DAL.FavoriteDAL fdao = new DAL.FavoriteDAL();
int y = fdao.CalcCount("username='" + User.Identity.Name + "' and proid=" + proid);
if (y != 0)
{
Utility.Tool.Alert("该商品已收藏过了!", this.Page);
return;
}
int x = fdao.CalcCount("username='" + User.Identity.Name + "'");
if (x == 10)
{
Utility.Tool.Alert("你已经添加了十种商品", this.Page);
return;
}

fdao.Add(new Model.Favorite()
{
proid = int.Parse(proid),
username = User.Identity.Name
});
Utility.Tool.Alert("收藏成功", this.Page);
}


三、加入购物车
             if (!User.Identity.IsAuthenticated)
            {
                Utility.Tool.Alert("请先登录再购买", this.Page);
                return;
            }
            string proid = Request.QueryString["id"];
            Model.Product pro = new DAL.ProductDAL().GetModel(int.Parse(proid));
            Model.User user = new DAL.UserDAL().GetModel(User.Identity.Name);
            if (pro != null && user != null)
            {
                if (user.isuse.Length != 0)
                {
                    Utility.Tool.Alert("请先激活帐号再购买", this.Page);
                    return;
                }
                decimal price = 0;
                if (user.type == "vip")
                {
                    price = pro.vipprice;
                }
                else
                {
                    price = pro.memberprice;
                }

                if (Session["shopcart"] == null)
                {
                    Session["shopcart"] = new Model.ShopCatr();
                }
                Model.ShopCatr sc = Session["shopcart"] as Model.ShopCatr;
                sc.Add(int.Parse(proid), new Model.ShopItem()
                {
                    Quantity = 1,
                    Price = price,
                    Proid = int.Parse(proid)
                });
                Utility.Tool.ExecJs("if(confirm('商品添加成功是否跳转到购物车页面?')){location.href='shopcart.aspx'}else{location.href='" + Request.Url.ToString() + "'}", this.Page);

前台显示
if (Session["shopcart"] != null)
                {
                    Model.ShopCatr sc = Session["shopcart"] as Model.ShopCatr;
                    if (sc.GetItemCount() > 0)
                    {
                        hlGWC.Text = "<a href='/shopcart.aspx'>购物车【<span style='font-weight:bold;color:red'>" + sc.GetItemCount() + "</span>】</a>";
                    }
                }

//收藏页面中的商品批量加入购物车
foreach (RepeaterItem item in rep.Items)
{
        //在rep中加上复选框前台:
        <asp:CheckBox ID="chk" ToolTip='<%#Eval("proid") %>' runat="server" />
CheckBox chk = item.FindControl("chk") as CheckBox;
if (chk.Checked)
{
string proid = chk.ToolTip;
Model.Product pro = new DAL.ProductDAL().GetModel(int.Parse(proid));
    其它和上面一样

四、激活用户
1、在用户数据表中加上isuse字段
2、注册时string isuse=Guid.NewGuid().Tosring();
3、在UserDAL.cs中加上通过isuse获取实体类的方法

在弹出成功之前加上:
//发送激活邮件
 string url = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/jh_user.aspx?str=" + isuse;
 Utility.Tool.SendMail("请激活您在购物网注册的帐户", "点击以下链接即可激活:<a href='" + url + "' target='_blank'>" + url + "</a>", email, "181796286@qq.com", "181796286@qq.com", "123456", "smtp.qq.com");

HttpCookie cook;               
string roles = "user";  //用户角色
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
   1, name, DateTime.Now, DateTime.Now.AddMinutes(30), false, roles);
cook = new HttpCookie("mycook");
cook.Value = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(cook);
Response.Redirect("user/index.aspx");

创建jh_user.aspx文件
string str = Request.QueryString["str"];
                if (!string.IsNullOrEmpty(str))
                {
                    Model.User model = new DAL.UserDAO().GetModelByIsuse(str); //GetModelByIsuse根据isuse生成Model
                    if (model!=null)
                    {
                        model.isuse = "";
                        new DAL.UserDAO().Update(model);
                        litmes.Text = "您的账户已激活,可以进行购物了。";

                        //用户登录
                        HttpCookie cook;
                        string roles = "user"; // 用户角色 
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                            1, model.username, DateTime.Now, DateTime.Now.AddMinutes(30), true, roles);
                        cook = new HttpCookie("mycook");
                        cook.Value = FormsAuthentication.Encrypt(ticket);
                        Response.Cookies.Add(cook);                  
                    }
                    else
                    {
                        litmes.Text = "没有这个账户";
                    }
五、在user/index.aspx中加上激活用户
Page_Load中加上
 Model.User u = new DAL.UserDAO().GetModel(User.Identity.Name);
                if (u != null)
                {
                    if (u.isuse.Length != 0)
                    {
                        //未激活
                        pnlJH.Visible = true;
                    }
                    else
                    {
                        //已激活
                        pnlJH.Visible = false;

                    }
                }
单击激活
Model.User u = new DAL.UserDAO().GetModel(User.Identity.Name);
            if (u != null)
            {
                string isuse = u.isuse;
                string email = u.email;
                //发送激活邮件
                string url = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/jh_user.aspx?str=" + isuse;
                Utility.Tool.SendMail("请激活您在购物网的账户", "点击以下链接即可激活账户: <a href='" + url + "' target='_blank'>" + url + "</a>", email, "181796286@qq.net", " 181796286@qq.net ", "123456", "smtp.qq.com");
                Utility.Tool.Alert("激活邮件已经发送成功。",this.Page);
            }


//前台搜索商品
//搜索
protected void btnSo_Click(object sender, EventArgs e)
{
string key = txtkey.Text.Trim();
if (key.Length == 0)
{
Utility.Tool.Alert("请输入关键字", this.Page);
return;
}
key = Server.UrlEncode(key);    //编码
string url = "prolist.aspx?key=" + key;
Response.Redirect(url);
}
//获取条件 Prolist.aspx页面
private string GetCond()
{
string cond = "isdel=0";
string key = Request.QueryString["key"];
if (!string.IsNullOrEmpty(key))
{
key = Server.UrlDecode(key);    //转码
key = Utility.Tool.GetSafeSQL(key);    //过滤字符
cond += " and proname like '%" + key + "%' ";
}
return cond;
}