protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (SaleCarsEntities ctx = new SaleCarsEntities()) { DDlType.DataSource = ctx.Types.ToList(); DDlType.DataValueField = "TypeID"; DDlType.DataTextField = "TypeName"; DDlType.DataBind(); DDLCategory.DataSource = ctx.Categories.ToList(); DDLCategory.DataValueField = "CatID"; DDLCategory.DataTextField = "CatName"; DDLCategory.DataBind(); } string keySearch = Request.QueryString["keySearch"]; keySearch = Server.UrlDecode(keySearch); if (!string.IsNullOrEmpty(keySearch)) { SearchSimple(keySearch); } } }
protected void btnUpDate_Click(object sender, EventArgs e) { using (SaleCarsEntities ctx = new SaleCarsEntities()) { int count = ctx.Users.Where(u => u.f_Username == txtUsername.Text).Count(); if (count == 0) { } else { User cUs = CurrentContext.GetCurUser(); cUs.f_Name = txtName.Text; cUs.f_Email = txtEmail.Text; cUs.f_DOB = (DateTime.ParseExact(txtDate.Text, "dd/M/yyyy", null)); ctx.Entry(cUs).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); if (ClientScript.IsStartupScriptRegistered("updateinfo") == false) { ClientScript.RegisterStartupScript(this.GetType(), "updateinfo", "alert('Cập nhật thành công!');", true ); } } } }
protected void btnLapHoaDon_Click(object sender, EventArgs e) { if (CurrentContext.GetCart().Items.Count == 0) { if (ClientScript.IsStartupScriptRegistered("thongbao") == false) { ClientScript.RegisterStartupScript(this.GetType(), "thongbao", "alert('Giỏ hàng rỗng!');", true ); } return; } Order ord = new Order { OrderDate = System.DateTime.Now, UserID = CurrentContext.GetCurUser().f_ID, Total = (long)Convert.ToDecimal(lblTotal.Text.Substring(0, lblTotal.Text.Length - 1)) }; using (SaleCarsEntities ctx = new SaleCarsEntities()) { foreach (CartItems item in CurrentContext.GetCart().Items) { Product pro = ctx.Products.Where(p => p.ProID == item.ProID).FirstOrDefault(); if (pro != null) { Orderdetail d = new Orderdetail { ProID = item.ProID, Quantity = item.Quantity, Price = pro.Price, Amount = item.Quantity * pro.Price }; ord.Orderdetails.Add(d); UpdateQuantityAndSale(ctx, pro, item); } } ctx.Orders.Add(ord); ctx.SaveChanges(); } CurrentContext.GetCart().Items.Clear(); ((template_sale_cars)this.Master).UpdateCartLink(); lvwCart.DataSource = CurrentContext.GetCart().Items; lvwCart.DataBind(); if (ClientScript.IsStartupScriptRegistered("thongbaotc") == false) { ClientScript.RegisterStartupScript(this.GetType(), "thongbaotc", "alert('Đặt hàng thành công!');", true ); } }
protected void btnLogin_Click(object sender, EventArgs e) { string username = txtUsernameLogin.Text; string enc_pwd = StringUtils.MD5(txtPassLogin.Text); using (SaleCarsEntities ctx = new SaleCarsEntities()) { User us = ctx.Users .Where(u => u.f_Username == username && u.f_Password == enc_pwd) .FirstOrDefault(); if (us != null) { Session["IsLogin"] = 1; Session["CurUser"] = us; if (chkRemember.Checked) { Response.Cookies["Username"].Value = username; Response.Cookies["Username"].Expires = DateTime.Now.AddDays(7); } string retUrl = Request.QueryString["retUrl"]; if (string.IsNullOrEmpty(retUrl)) { retUrl = "~/index.aspx"; } Response.Redirect(retUrl); } else { lbThongBao.Text = "Username hoac mat khau khong dung!"; } } }
private void UpdateView(SaleCarsEntities ctx, Product pro) { //update view pro.Views++; ctx.Entry(pro).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); }
private void SearchName(string keySearch) { if (!string.IsNullOrEmpty(keySearch) && !string.IsNullOrWhiteSpace(keySearch)) { using (SaleCarsEntities ctx = new SaleCarsEntities()) { if (chkAll.Checked) { list = ctx.Products.Where(p => p.ProName.Contains(keySearch)).ToList(); } else { int type = Convert.ToInt32(DDlType.Items[selectedIndexType].Value); int cat = Convert.ToInt32(DDLCategory.Items[selectedIndexCat].Value); list = ctx.Products.Where(p => (p.ProName.Contains(keySearch) && (p.TypeID == type) && (p.CatID == cat))).ToList(); } LoadData(); } } }
private void UpdateQuantityAndSale(SaleCarsEntities ctx, Product pro, CartItems item) { //update quantity pro.Quantity -= item.Quantity; //update sale pro.Sales += item.Quantity; ctx.Entry(pro).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); }
private void SearchPrice(string keySearch) { decimal keyPrice = 0; decimal.TryParse(keySearch, out keyPrice); if (keyPrice > 0) { using (SaleCarsEntities ctx = new SaleCarsEntities()) { if (chkAll.Checked) { if (keyPrice <= 100000) { list = ctx.Products.Where(p => p.Price >= keyPrice - 5000 && p.Price <= keyPrice + 5000).ToList(); } else // > 100.000 { list = ctx.Products.Where(p => p.Price >= keyPrice - 10000 && p.Price <= keyPrice + 10000).ToList(); } } else { int type = Convert.ToInt32(DDlType.Items[selectedIndexType].Value); int cat = Convert.ToInt32(DDLCategory.Items[selectedIndexCat].Value); if (keyPrice <= 100000) { list = ctx.Products.Where(p => p.Price >= keyPrice - 5000 && p.Price <= keyPrice + 5000 && (p.TypeID == type) && (p.CatID == cat)).ToList(); } else // > 100.000 { list = ctx.Products.Where(p => p.Price >= keyPrice - 10000 && p.Price <= keyPrice + 10000 && (p.TypeID == type) && (p.CatID == cat)).ToList(); } } LoadData(); } } }
private void LoadData() { string typeID = Request.QueryString["typeID"]; string catID = Request.QueryString["catID"]; if (string.IsNullOrEmpty(typeID) && string.IsNullOrEmpty(catID)) { using (SaleCarsEntities ctx = new SaleCarsEntities()) { List <Product> list = ctx.Products.ToList(); lvwProducts.DataSource = list; lvwProducts.DataBind(); lblTitle.Text = "All products"; } return; } // Types if (string.IsNullOrEmpty(typeID) == false) { int i_typeId = Convert.ToInt32(typeID); using (SaleCarsEntities ctx = new SaleCarsEntities()) { List <Product> list = ctx.Products.Where(p => p.TypeID == i_typeId).ToList(); lvwProducts.DataSource = list; lvwProducts.DataBind(); } } else { //Categories if (string.IsNullOrEmpty(catID) == false) { int i_catId = Convert.ToInt32(catID); using (SaleCarsEntities ctx = new SaleCarsEntities()) { List <Product> list = ctx.Products.Where(p => p.CatID == i_catId).ToList(); lvwProducts.DataSource = list; lvwProducts.DataBind(); } } } }
protected void lvwCart_ItemCommand(object sender, ListViewCommandEventArgs e) { if (e.CommandName == "DeleteItem") { int proID = Convert.ToInt32(e.CommandArgument); CurrentContext.GetCart().RemoveItem(proID); ((template_sale_cars)this.Master).UpdateCartLink(); lvwCart.DataSource = CurrentContext.GetCart().Items; lvwCart.DataBind(); return; } if (e.CommandName == "UpdateItem") { int proID = Convert.ToInt32(e.CommandArgument); using (SaleCarsEntities ctx = new SaleCarsEntities()) { Product pro = ctx.Products.Where(p => p.ProID == proID).FirstOrDefault(); int quantity = Convert.ToInt32(((TextBox)e.Item.FindControl("txtSL")).Text); if (quantity <= pro.Quantity) { CurrentContext.GetCart().UpdateItem(proID, quantity); ((template_sale_cars)this.Master).UpdateCartLink(); lvwCart.DataSource = CurrentContext.GetCart().Items; lvwCart.DataBind(); } else { if (ClientScript.IsStartupScriptRegistered("thongbaoUP") == false) { ClientScript.RegisterStartupScript(this.GetType(), "thongbaoUP", "alert('Bạn chỉ có thể đặt tối đa là: " + pro.Quantity + " chiếc!');", true ); } ((TextBox)e.Item.FindControl("txtSL")).Text = pro.Quantity.ToString(); } } return; } }
//search private void SearchSimple(string keySearch) { if (!string.IsNullOrEmpty(keySearch)) { using (SaleCarsEntities ctx = new SaleCarsEntities()) { if (chkAll.Checked) { list = ctx.Products.Where(p => p.ProName.Contains(keySearch)).ToList(); } } LoadData(); } }
protected void cvUsername_ServerValidate(object source, ServerValidateEventArgs args) { string username = args.Value; using (SaleCarsEntities ctx = new SaleCarsEntities()) { int n = ctx.Users.Where(u => u.f_Username == username).Count(); if (n > 0) { args.IsValid = false; } else { args.IsValid = true; } } }
protected void Page_Load(object sender, EventArgs e) { using (SaleCarsEntities ctx = new SaleCarsEntities()) { List <Product> listDOR = ctx.Products.OrderByDescending(x => x.DOR).Take(9).ToList(); lvwDOR.DataSource = listDOR; lvwDOR.DataBind(); List <Product> listSales = ctx.Products.OrderByDescending(x => x.Sales).Take(9).ToList(); lvwSales.DataSource = listSales; lvwSales.DataBind(); List <Product> listViews = ctx.Products.OrderByDescending(x => x.Views).Take(9).ToList(); lvwViews.DataSource = listViews; lvwViews.DataBind(); } }
protected void btnRegister_Click(object sender, EventArgs e) { User u = new User { f_Username = txtUserName.Text, f_Password = StringUtils.MD5(txtRegPass.Text), f_Email = txtEmail.Text, f_Name = txtName.Text, f_DOB = (DateTime.ParseExact(txtDate.Text, "dd/M/yyyy", null)), f_Permission = 0 }; using (SaleCarsEntities ctx = new SaleCarsEntities()) { ctx.Users.Add(u); ctx.SaveChanges(); } Response.Redirect("~/login.aspx?msg=1"); }
protected void btnChangePass_Click(object sender, EventArgs e) { string oldPass = StringUtils.MD5(txtOldPass.Text); using (SaleCarsEntities ctx = new SaleCarsEntities()) { int count = ctx.Users.Where(u => u.f_Username == txtUsername.Text && u.f_Password == oldPass).Count(); if (count == 0) { if (ClientScript.IsStartupScriptRegistered("swal") == false) { ClientScript.RegisterStartupScript(this.GetType(), "swal", "alert('Mật khẩu không đúng!');", true ); } } else { User cUs = CurrentContext.GetCurUser(); cUs.f_Password = StringUtils.MD5(txtNewPass.Text); ctx.Entry(cUs).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); if (ClientScript.IsStartupScriptRegistered("updatepass") == false) { ClientScript.RegisterStartupScript(this.GetType(), "updatepass", "alert('Đổi mật khẩu thành công!');", true ); } } } }
protected void lvwCart_ItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { CartItems item = (CartItems)e.Item.DataItem; ((ImageButton)e.Item.FindControl("btnUpdate")).ValidationGroup = "grp_" + item.ProID; ((CompareValidator)e.Item.FindControl("valUpdate")).ValidationGroup = "grp_" + item.ProID; using (SaleCarsEntities ctx = new SaleCarsEntities()) { Product pro = ctx.Products.Where(p => p.ProID == item.ProID).FirstOrDefault(); ((Label)e.Item.FindControl("lblSP")).Text = pro.ProName; ((Label)e.Item.FindControl("lblGia")).Text = String.Format("{0:N0}$", pro.Price); decimal amount = pro.Price * item.Quantity; ((Label)e.Item.FindControl("lblTTien")).Text = string.Format("{0:N0}$", amount); total += amount; } } }
protected void Page_Load(object sender, EventArgs e) { if (CurrentContext.IsLogged()) { pnCart.Visible = true; pnLogged.Visible = true; pnNotLogged.Visible = false; lnkUsername.Text = string.Format("<b>{0}</b>", CurrentContext.GetCurUser().f_Name); UpdateCartLink(); } else { pnCart.Visible = false; pnLogged.Visible = false; pnNotLogged.Visible = true; } if (IsPostBack == false) { using (SaleCarsEntities ctx = new SaleCarsEntities()) { List <Type> listType = ctx.Types.ToList(); lvwTypes.DataSource = listType; lvwTypes.DataBind(); List <Category> listCategories = ctx.Categories.ToList(); lvwCategories.DataSource = listCategories; lvwCategories.DataBind(); } } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string proID = Request.QueryString["proID"]; if (string.IsNullOrEmpty(proID)) { Response.Redirect("~/index.aspx"); } else { int i_proID = Convert.ToInt32(proID); int catID = 0; int typeID = 0; using (SaleCarsEntities ctx = new SaleCarsEntities()) { Product pro = ctx.Products.Where(p => p.ProID == i_proID).FirstOrDefault(); if (pro != null) { img1.ImageUrl = string.Format("../images/products/{0}/1.jpg", pro.ProID); img1_th.ImageUrl = string.Format("../images/products/{0}/1_thumbs.jpg", pro.ProID); img2.ImageUrl = string.Format("../images/products/{0}/2.jpg", pro.ProID); img2_th.ImageUrl = string.Format("../images/products/{0}/2_thumbs.jpg", pro.ProID); lblName.Text = pro.ProName; lblPrice.Text = string.Format("$ {0:N0}", pro.Price); lblQuantity.Text = pro.Quantity.ToString(); lblViews.Text = pro.Views.ToString(); lblSales.Text = pro.Sales.ToString(); lblTinyDes.Text = HttpUtility.HtmlDecode(pro.TinyDes); lblFullDes.Text = HttpUtility.HtmlDecode(pro.FullDes); catID = pro.CatID; typeID = pro.TypeID; if (CurrentContext.IsLogged() && pro.Quantity > 0) { pnAddCart.Visible = true; } else { pnAddCart.Visible = false; } UpdateView(ctx, pro); } // get Category List <Product> listCat = ctx.Products.Where(p => p.CatID == catID && p.ProID != pro.ProID).Take(5).ToList(); lvwCat.DataSource = listCat; lvwCat.DataBind(); // get Type List <Product> listType = ctx.Products.Where(p => p.TypeID == typeID && p.ProID != pro.ProID).Take(5).ToList(); lvwType.DataSource = listType; lvwType.DataBind(); } } } }