cookie自动登录,添加到购物车

 

cookie的读和写

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
                {
                    string username = Request.Cookies["username"].Value;
                    string password = Request.Cookies["password"].Value;
                    if (username == "zpp" && password == "111")
                    {
                        Response.Redirect("WebIndex.aspx");
                    }
                }

                if (Request.Cookies["username"] != null)
                {
                    string cusername = Request.Cookies["username"].Value;  // 读cookie
                    txbusername.Text = cusername;
                }
                //aotuman();
                   
            }
        }

        //private void aotuman()
        //{
        //    if (ckautoman.Checked == true)
        //    {
        //        HttpCookie cookie = new HttpCookie("username" );
        //        cookie.Value = txbusername.Text;
        //        cookie.Expires = DateTime.Now.AddMinutes(5);
        //        Response.Cookies.Add(cookie);
        //        //写密码
        //        HttpCookie password = new HttpCookie("password");
        //        cookie.Value = txbPassword.Text;
        //        cookie.Expires = DateTime.Now.AddMinutes(5);
        //        Response.Cookies.Add(cookie);
        //        Response.Redirect("WebIndex.aspx");
        //    }
        //}

        protected void btnLogon_Click(object sender, EventArgs e)
        {
            if (txbusername.Text == "zpp" && txbPassword.Text == "111")
            {

                    //写cookie
                if (ckremember.Checked == true)
                {
                    HttpCookie cookie = new HttpCookie("username");
                    cookie.Value = txbusername.Text;
                    //cookie.Value = txbPassword.Text;
                    cookie.Expires = DateTime.Now.AddMinutes(5);
                    Response.Cookies.Add(cookie);
                }

                if (ckautoman.Checked == true)
                {
                    HttpCookie username = new HttpCookie("username");
                    username.Value = txbusername.Text;
                    username.Expires = DateTime.Now.AddMinutes(5);
                    Response.Cookies.Add(username);
                    HttpCookie password = new HttpCookie("password");
                    password.Value = txbPassword.Text;
                    password.Expires = DateTime.Now.AddMinutes(2);
                    Response.Cookies.Add(password);
                    //Response.Redirect("WebIndex.aspx");
                }
            }
                Response.Redirect("WebIndex.aspx");
           
        }

 

点击按钮,用cookies保存

protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (Request.Cookies["productname"] != null)
            {
                HttpCookie cookie = new HttpCookie("productname");
                cookie.Value += this.imgproduct.Src + "," + this.spproductname.InnerText + "," + this.Sppruductprice.InnerText + "|";
                cookie.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = new HttpCookie("productname");
                cookie.Value += this.imgproduct.Src + "," + this.spproductname.InnerText + "," + this.Sppruductprice.InnerText + "|";
                cookie.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(cookie);
            }
            Response.Redirect("WebProduct3.aspx");
        }

运用数据库从数据中调出数据

  public partial class WebProduct2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (String.IsNullOrEmpty(Request.QueryString["productname"]) == false)
                {
                    string productname = Request.QueryString["productname"];  //从另一个界面传值
                    getProductname(productname);
                }
            }
        }

        private void getProductname(string productname)
        {
            string sqlstr = "Data Source=SXVPTNJT9Q07YYP;Initial Catalog=Student2;User ID=sa;Password=abcdef";
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(sqlstr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from T_SaleProduct where ProductName=@productname";
                    cmd.Parameters.AddWithValue("@productname",productname);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(dt);
                }
            }
            this.imgproduct.Src = dt.Rows[0]["ProductImage"].ToString();
            this.spproductname.InnerText = dt.Rows[0]["ProductName"].ToString();
            this.Sppruductprice.InnerText = dt.Rows[0]["Price"].ToString();
        }

        protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (Request.Cookies["productname"] != null)
            {
                HttpCookie cookie = new HttpCookie("productname");
                cookie.Value += this.imgproduct.Src + "," + this.spproductname.InnerText + "," + this.Sppruductprice.InnerText + "|";
                cookie.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = new HttpCookie("productname");
                cookie.Value += this.imgproduct.Src + "," + this.spproductname.InnerText + "," + this.Sppruductprice.InnerText + "|";
                cookie.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(cookie);
            }
            Response.Redirect("WebProduct3.aspx");
        }

        protected void imgBtnBuy_Click(object sender, ImageClickEventArgs e)
        {
            if (txbNumber.Text != null && txbNumber.Text != "0")
            {
                double money = Convert.ToDouble(Sppruductprice.InnerText) * Convert.ToDouble(txbNumber.Text);
                string sqlstr = "Data Source=SXVPTNJT9Q07YYP;Initial Catalog=Student2;User ID=sa;Password=abcdef";
                int result = 0;
                using (SqlConnection conn = new SqlConnection(sqlstr))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "insert into T_BuyProduct values(@username,@totalmoney)";
                        cmd.Parameters.AddWithValue("@username",Session["Username"].ToString());
                       // cmd.Parameters.AddWithValue("@address",Session["Address"].ToString());
                        cmd.Parameters.AddWithValue("@totalmoney", money);
                        result= cmd.ExecuteNonQuery();
                    }
                }
                if (result > 0)
                {
                    Response.Write("交易成功!");
                }
                else { Response.Write("交易不成功,请重新购买!"); }
            }
        }
    }

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值