Mã:
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Configuration;
namespace MobileEcommerce.Admin
{
/// <summary>
/// Summary description for AdminProduct.
/// </summary>
public partial class AdminProduct : System.Web.UI.Page
{
public string getDSN()
{
return ConfigurationSettings.AppSettings["connstring"];
}
protected void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if ((IsPostBack == false) || (Request.Params["do"] != "add"))
{
dgList_BindData();
}
if (IsPostBack == false)
{
isSearch.Checked = false;
}
//check the params to show proper panels and lable messages
if (Request.Params["do"] == "add")
{
PopulatePnlAdd();
lblTitle.Text = "Thêm sản phẩm";
}
else if (Request.Params["added"] == "1")
{
lblmsg.Text = "Sản phẩm đã được thêm vào.";
}
else if (Request.Params["updated"] == "1")
{
lblmsg.Text = "Sản phẩm đã được cập nhật.";
}
else if (Request.Params["deleted"] == "1")
{
lblmsg.Text = "Sản phẩm đã được xóa thành công";
}
}
void dgList_BindData()
{
// bind category info to datagrid
MobileEcommerce.ProductsDB objProducts = new MobileEcommerce.ProductsDB(getDSN());
if (isSearch.Checked == true)
{
dgList.DataSource = objProducts.SearchProductAdmin(txtsearch.Text);
}
else
{
dgList.DataSource = objProducts.GetAllProducts();
}
dgList.DataBind();
}
// *****************************************************************************************************
// dgList_PageIndexChanged() method
// DataGrid Paging for Product Info
// *****************************************************************************************************
private void dgList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
// bind Product info to datagrid on each page change event
dgList.CurrentPageIndex = e.NewPageIndex;
dgList_BindData();
}
// *****************************************************************************************************
// This method handle the DataGrid dgList// s ItemCommand,
// determine whether the ItemCommand is Edit Command or Delete Command to show the proper panel
// and populate the product detail data.
// The method calls methods in the ProductDB class: GetProductDetails() ; GetProductCategories();
// ********************************************************************************************************
private void dgList_ItemCommand(object source, DataGridCommandEventArgs e)
{
// //show the proper panel for clicking the Edit & Delete links
if (e.CommandName == "edit")
{
// // show pnlEdit panels
pnlAll.Visible = false;
pnlEdit.Visible = true;
// populate fields with DB data
MobileEcommerce.ProductDetails objProdDetails;
MobileEcommerce.ProductsDB objProduct = new MobileEcommerce.ProductsDB(getDSN());
objProdDetails = objProduct.GetProductDetails((int)(this.dgList.DataKeys[e.Item.ItemIndex]));
// lblmsg.Text += dgList.DataKeys[e.Item.ItemIndex] + " " + dgList.SelectedIndex;
//txtEditCost.Text = Decimal.Truncate(objProdDetails.UnitCost).ToString();
txtEditPrice.Text = Decimal.Truncate(objProdDetails.UnitPrice).ToString();
txtEditName.Text = objProdDetails.ProductName;
ftbEditDesc.Text = objProdDetails.Description;
ftbEditSpec.Text = objProdDetails.Specification;
txtEditQuantity.Text = objProdDetails.QuantityInStock.ToString();
//
erdBL.Checked = (objProdDetails.bluetooth == 1);
erdBNN.Checked = (objProdDetails.bonhongoai == 1);
erdBNT.Checked = (objProdDetails.bonhotrong == 1);
erdBT.Checked = (objProdDetails.baothuc == 1);
erdDK.Checked = (objProdDetails.dkgiongnoi == 1);
erdMA.Checked = (objProdDetails.mayanh == 1);
erdNN.Checked = (objProdDetails.nghenhac == 1);
//
epr_rd1.Text = objProdDetails.gut1.ToString();
epr_rd2.Text = objProdDetails.gut2.ToString();
epr_rd3.Text = objProdDetails.gut3.ToString();
ext_rd1.Text = objProdDetails.xut1.ToString();
ext_rd2.Text = objProdDetails.xut2.ToString();
ext_rd3.Text = objProdDetails.xut3.ToString();
eth_rd1.Text = objProdDetails.dut1.ToString();
eth_rd2.Text = objProdDetails.dut2.ToString();
eth_rd3.Text = objProdDetails.dut3.ToString();
emm_rd1.Text = objProdDetails.kut1.ToString();
emm_rd2.Text = objProdDetails.kut2.ToString();
emm_rd3.Text = objProdDetails.kut3.ToString();
if (objProdDetails.LargeProductImage != "")
{
imgEditImage.ImageUrl = @"..\images\ProductImages\Large\" + objProdDetails.LargeProductImage;
imgEditImage.Visible = true;
strLargeImage.Text = objProdDetails.LargeProductImage.ToString();
}
if (objProdDetails.SmallProductImage != "")
{
imgEditThumbImage.ImageUrl = @"..\images\ProductImages\Thumbs\" + objProdDetails.SmallProductImage;
imgEditThumbImage.Visible = true;
strSmallImage.Text = objProdDetails.SmallProductImage;
}
// populate DropDownList ddlEditCategory with Category information
SqlDataReader dtrReader = objProduct.GetProductCategories();
while (dtrReader.Read())
{
ListItem myItem = new ListItem();
myItem.Text = dtrReader["CategoryName"].ToString();
myItem.Value = dtrReader["CategoryID"].ToString();
ddlEditCategory.Items.Add(myItem);
}
// get Selected Item From Category from product list datagrid
int counter = 0;
foreach (ListItem itemCat in ddlEditCategory.Items)
{
if (itemCat.Text.Trim() == e.Item.Cells[1].Text)
{
ddlEditCategory.SelectedIndex = counter;
}
counter = counter + 1;
}
// pass the CatID to a form field in the EditCats panel
EditProductID.Text = dgList.DataKeys[e.Item.ItemIndex].ToString();
}
else if (e.CommandName == "delete")
{
// the Delete ItemCommand is redirected to Confirm delete page
Response.Redirect("DeleteProduct.aspx?pid=" + dgList.DataKeys[e.Item.ItemIndex]);
}
}
// *****************************************************************************************************
// PopulatePnlAdd() method
// if they have clicked on the // Add Product' link -- populate pnlAdd and make visible
// *****************************************************************************************************
private void PopulatePnlAdd()
{
pnlAll.Visible = false;
pnlAdd.Visible = true;
lblSearch.Visible = false;
txtsearch.Visible = false;
btnGo.Visible = false;
// populate the Add Product -- Category DDL
MobileEcommerce.ProductsDB objProduct = new MobileEcommerce.ProductsDB(getDSN());
SqlDataReader dtrReader;
dtrReader = objProduct.GetProductCategories();
while (dtrReader.Read())
{
ListItem myItem = new ListItem();
myItem.Text = dtrReader["CategoryName"].ToString();
myItem.Value = dtrReader["CategoryID"].ToString();
ddlCategory.Items.Add(myItem);
}
}
// *****************************************************************************************************
// SaveProduct() method
// This method adds a new Product to database by calling AddNewProduct() class in ProductDB
// The image of product is uploaded to Images folder on server after check their type, this require calling
// ValidFileType() method in CommonUtils class
// *****************************************************************************************************
private void btnSaveProduct_Click(object s, System.EventArgs e)
{
MobileEcommerce.ProductsDB objNewProduct = new MobileEcommerce.ProductsDB(getDSN());
MobileEcommerce.CommonUtils objFileType = new MobileEcommerce.CommonUtils();
// this is the path where the image uploads are stored -- both large and thumb size images for products
string VirtualPath = Server.MapPath(Request.ServerVariables["URL"]);
string ImageSavePath = VirtualPath.Substring(0, VirtualPath.IndexOf("Admin") - 1) + @"\images\ProductImages\Large\";
string ThumbSavePath = VirtualPath.Substring(0, VirtualPath.IndexOf("Admin") - 1) + @"\images\ProductImages\Thumbs\";
// save the name of the image file they are uploading to DB as well as the rest of the info --
// make sure they are uploading an image
string filename = Path.GetFileName(txtImage.PostedFile.FileName).Trim();
string thumbfilename = Path.GetFileName(txtThumbImage.PostedFile.FileName).Trim();
// check the proper image file type
if ((filename != "") && (objFileType.ValidFileType(filename) == false))
{
lblmsg.Text = "Bạn chỉ có thể Upload file: .jpg, .jpeg or .gif";
}
else if ((thumbfilename != "") && (objFileType.ValidFileType(thumbfilename) == false))
{
lblmsg.Text = "Bạn chỉ có thể Upload file: .jpg, .jpeg or .gif";
}
else
{
if (filename == "")
{
filename = "not-available.jpg";
}
if (thumbfilename == "")
{
thumbfilename = "not-available.jpg";
}
// save the Product info to the db, if success return the productID
int result =objNewProduct.AddNewProduct(txtName.Text.Trim(), Convert.ToInt32(ddlCategory.SelectedItem.Value), filename.ToString(), thumbfilename.ToString(), Convert.ToDecimal(txtPrice.Text), Convert.ToDecimal(txtPrice.Text), ftbDesc.Text, ftbSpec.Text, Convert.ToInt32(txtQuantity.Text.Trim()),
rdBL.Checked,rdBT.Checked,rdDK.Checked,rdMA.Checked,rdNN.Checked,rdBNT.Checked,rdBNN.Checked);
if (result > 0)// if the method AddNewProduct is success
{
// if there was an image added, upload image, otherwise, redirect back to AdminProduct.aspx
// upload the image to its folder
try
{
if (filename != "not-available.jpg")
{
// upload the image to its folder
System.Web.HttpPostedFile postedFile = txtImage.PostedFile;
string contentType = postedFile.ContentType;
int contentLength = postedFile.ContentLength;
postedFile.SaveAs(ImageSavePath + filename);
}
//if there was an thumb added, upload it, else, redirect back to AdminProduct.aspx
if (thumbfilename != "not-available.jpg")
{
// upload the thumbnail image to its folder
System.Web.HttpPostedFile postedFileThumb = txtThumbImage.PostedFile;
string contentTypeThumb = postedFileThumb.ContentType;
int contentLengthThumb = postedFileThumb.ContentLength;
postedFileThumb.SaveAs(ThumbSavePath + thumbfilename);
}
}
catch { }
Response.Redirect("Quanlyhanghoa.aspx?added=1");
}
else if (result == -1)
{
lblmsg.ForeColor = Color.Red;
lblmsg.Text = "Sản phẩm đã tồn tại hoặc 'Thêm sản phẩm' đã bị lỗi. Xin hãy thử lại.";
}
else
{
lblmsg.Text = "Có vấn đề với đầu vào. Xin hãy thử lại.";
}
// this is the end if statment to make sure they are uploading .gif, .jpg or .jpeg images
}
}
// *****************************************************************************************************
// UpdateProduct() method
// This method handles btnEditProduct.Click event to update an existing Product Information by
// calling UpdateProduct() method in ProductDB class
// *****************************************************************************************************
private void btnEditProduct_Click(object s, System.EventArgs e)
{
MobileEcommerce.ProductsDB objEditProduct = new MobileEcommerce.ProductsDB(getDSN());
CommonUtils objFileType = new CommonUtils();
// this is the path where the image uploads are stored -- both large and thumb size images for products
string VirtualPath = Server.MapPath(Request.ServerVariables["URL"]);
string ImageSavePath = VirtualPath.Substring(0, VirtualPath.IndexOf("Admin")) + @"\images\ProductImages\Large\";
string ThumbSavePath = VirtualPath.Substring(0, VirtualPath.IndexOf("Admin")) + @"\images\ProductImages\Thumbs\";
// save the name of the image file they are uploading to DB as well as the rest of the info --
// make sure they are uploading an image
string filename = Path.GetFileName(txtEditImage.PostedFile.FileName).Trim();
string thumbfilename = Path.GetFileName(txtEditThumbImage.PostedFile.FileName).Trim();
// check the proper image file type
if ((filename != "") && (objFileType.ValidFileType(filename) == false))
{
lblmsg.Text = "Bạn chỉ có thể Upload file: .jpg, .jpeg or .gif";
}
else if ((thumbfilename != "") && (objFileType.ValidFileType(thumbfilename) == false))
{
lblmsg.Text = "Bạn chỉ có thể Upload file: .jpg, .jpeg or .gif";
}
else
{
if (filename == "")
{
filename = strLargeImage.Text;
}
if (thumbfilename == "")
{
thumbfilename = strSmallImage.Text;
}
// Update the Product info to the db
int result = objEditProduct.UpdateProduct(Convert.ToInt32(EditProductID.Text), txtEditName.Text.Trim(), Convert.ToInt32(ddlEditCategory.SelectedItem.Value), filename.ToString(), thumbfilename.ToString(), Convert.ToDecimal(txtPrice.Text), Convert.ToDecimal(txtEditPrice.Text), ftbEditDesc.Text, ftbEditSpec.Text, Convert.ToInt32(txtEditQuantity.Text.Trim()),
erdBL.Checked, erdBT.Checked, erdDK.Checked, erdMA.Checked, erdNN.Checked, erdBNT.Checked, erdBNN.Checked, epr_rd1.Text, epr_rd2.Text,
epr_rd3.Text, ext_rd1.Text, ext_rd2.Text, ext_rd3.Text, emm_rd1.Text, emm_rd2.Text, emm_rd3.Text, eth_rd1.Text, eth_rd2.Text, eth_rd3.Text);
if (result > 0)
{
try
{
// if there was an image added, upload image, otherwise, redirect back to AdminProduct.aspx
if (Path.GetFileName(txtEditImage.PostedFile.FileName).Trim() != "")
{
// upload the image to its folder
System.Web.HttpPostedFile postedFile = txtEditImage.PostedFile;
string contentType = postedFile.ContentType;
int contentLength = postedFile.ContentLength;
postedFile.SaveAs(ImageSavePath + filename); // cause err when update the product that has not got image
}
if (Path.GetFileName(txtEditThumbImage.PostedFile.FileName) != "")
{
// upload the thumbnail image to its folder
System.Web.HttpPostedFile postedFileThumb = txtEditThumbImage.PostedFile;
string contentTypeThumb = postedFileThumb.ContentType;
int contentLengthThumb = postedFileThumb.ContentLength;
postedFileThumb.SaveAs(ThumbSavePath + thumbfilename);
}
}
catch { }
Response.Redirect("Quanlyhanghoa.aspx?updated=1");
}
else
{
lblmsg.Text = "Có vấn đề trong việc cập nhật sản phẩm. Xin hãy thử lại.";
}
// this is the end if statment to make sure they are uploading .gif, .jpg or .jpeg images
}
}
// *****************************************************************************************************
// btnGo_Click() method
// This method handles btnGo.Click event to do search Product Information with search keyword
// The method is set the isSearch checkbox to True and calling dgList_BindData()method to show the search result
// *****************************************************************************************************
private void btnGo_Click(object sender, System.EventArgs e)
{
// show the proper panel
pnlAll.Visible = true;
pnlAdd.Visible = false;
pnlEdit.Visible = false;
// set the dgList to bind search result
isSearch.Checked = true;
dgList.CurrentPageIndex = 0;
dgList.SelectedIndex = -1;
// bind category info to datagrid
dgList_BindData();
lnkSrchBack.Visible = true;
// Hide the list and display a message if no orders have ever been made
if (dgList.Items.Count == 0 )
{
lblmsg.Text = "Không có sản phẩm nào được tìm thấy.";
dgList.Visible = false;
txtsearch.Visible = false;
btnGo.Visible = false;
lblSearch.Visible = false;
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgList.PageIndexChanged += new DataGridPageChangedEventHandler(dgList_PageIndexChanged);
this.dgList.ItemCommand +=new DataGridCommandEventHandler(dgList_ItemCommand);
this.btnSaveProduct.Click += new EventHandler(btnSaveProduct_Click);
this.btnGo.Click +=new EventHandler(btnGo_Click);
this.btnEditProduct.Click += new EventHandler(btnEditProduct_Click);
// Chung thuc
HttpCookie oCookie;
oCookie = this.Request.Cookies["Ecommerce_AID"];
if(oCookie==null)
{
Response.Redirect("login.aspx?id=false");
}
MobileEcommerce.AdminDB objRequest = new MobileEcommerce.AdminDB(getDSN());
if (objRequest.RequestLogin(Int32.Parse(Request.Cookies["Ecommerce_AID"].Value), "Administrator Management") == false)
{
Response.Redirect("ErrorPage.aspx");
}
}
#endregion
}
}
Còn đây là lỗi khi admin thêm sản phẩm:
Vệ sinh cá nhân là một phần quan yếu của sức khỏe nam giới. Dùng dung dịch vệ sinh mỗi ngày mang lại nhiều ích, giúp loại bỏ vi khuẩn và vi sinh vật gây hại, giúp khu vực cơ quan sinh dục luôn sạch...
Gel vệ sinh cu, bạn trai có nên...