using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using CitySunlight; namespace CitySunlight.Product { public partial class ProductEdit : System.Web.UI.Page { public String msg; public String username; public String type; protected void Page_Load(object sender, EventArgs e) { if (Session["username"] == null) Response.Redirect("/Account/Login.aspx"); else username = Session["username"].ToString(); type = HttpUtils.getQueryString("type"); switch (type) { case "success": msg = HttpUtils.getQueryString("msg"); this.submit.Visible = false; break; case "delete": if (isExist(HttpUtils.getQueryString("id").ToInt()) != 0) { msg = "你确定要删除这个产品?"; this.submit.Text = "确定"; } break; } } public String CreateEditForm() { int id = HttpUtils.getQueryString("id").ToInt(); switch (type) { case "edit": if (isExist(id) != 0) { this.submit.Text = "修改"; return GetEditForm(id); } return ""; case "create": this.submit.Text = "添加"; return GetEditForm(id); } return ""; } public int isExist(int id) { if (id == 0 || !ProductManager.isExist(id)) { this.submit.Visible = false; msg = "您所操作的产品不存在!"; id = 0; } return id; } protected String GetEditForm(int id) { String name = ""; String url = "empty.jpg"; String picurl = addImg(url); String price = ""; String amount = ""; String classid = ""; if (id != 0) { name = ProductManager.GetProductInfo(id, ProductManager.Info.ItemName); url = ProductManager.GetProductInfo(id, ProductManager.Info.PicUrl); picurl = addImg(url); price = ProductManager.GetProductInfo(id, ProductManager.Info.Price); amount = ProductManager.GetProductInfo(id, ProductManager.Info.Amount); classid = ProductManager.GetProductInfo(id, ProductManager.Info.Class); if (username != ProductManager.GetProductInfo(id, ProductManager.Info.UserName)) { this.submit.Visible = false; return "

你没有修改此商品的权限!

"; } } String html = ""; html += addFormGroup("商品名称", addDiv(addText("Name", name), "8")); html += addFormGroup("商品分类", addDiv(ProductClassManager.getOptionString("Class", classid), "8") + addDiv(addA("编辑", "ProductClassEdit.aspx"), "2")); html += addFormGroup("商品图片", addDiv(picurl, "8")); html += addFormGroup("上产图片", addDiv(addFile("Picture"), "8")); html += addFormGroup("商品价格", addDiv(addText("Price", price), "8")); html += addFormGroup("库存数量", addDiv(addText("Amount", amount), "8")); return html; } protected String addA(String Text, String Url) { return String.Format("{1}", Url, Text); } protected String addText(String name, String value) { return String.Format("", name, value); } protected String addImg(String picname) { return String.Format("", picname); } protected String addFile(String name) { return String.Format("", name); } protected String addDiv(String label, String value) { return String.Format("
{0}
", label); } protected String addFormGroup(String label, String value) { String html = String.Format("", label); html += String.Format("
{0}
", value); return html; } protected void submit_Click(object sender, EventArgs e) { int id = HttpUtils.getQueryString("id").ToInt(); String picurl = ""; switch (type) { case "delete": if (ProductManager.DeleteProduct(id)) { msg = "产品删除成功!"; Response.Redirect("/Product/ProductEdit.aspx?type=success&msg=" + msg); } return; case "edit": if (id == 0) { msg = "您所操作的产品不存在!"; return; } picurl = ProductManager.GetProductInfo(id, ProductManager.Info.PicUrl); break; } String floder = "/Picture/"; String user = Session["username"].ToString(); String name = HttpUtils.getElementsbyName("Name"); HttpPostedFile file = HttpUtils.getFilebyName("Picture"); decimal price = HttpUtils.getElementsbyName("Price").Todecimal(); int amount = HttpUtils.getElementsbyName("Amount").ToInt(); int classid = HttpUtils.getElementsbyName("Class").ToInt(); //检查名称 if (name.Length == 0) { msg = "请填写名称!"; GoBack(); return; } //检查价格 if (price == 0) { msg = "请输入价格且必须为数字!"; GoBack(); return; } //检查库存 if (amount == 0) { msg = "请输入库存且必须为数字!"; GoBack(); return; } //检查图片(图片文件为空,且不存在原有图片) if (file == null && picurl == "") { msg = "请上传产品图片!"; Response.Write("windows.history.go(-1);"); return; } if (file != null) { String fileExtension = Path.GetExtension(file.FileName).ToLower(); //验证上传文件是否图片格式 if (!IsImage(fileExtension)) { msg = "请上传类型为 jpg | gif | bmp | png 图片!"; GoBack(); return; } //生成图片名称 picurl = HttpUtils.currentTimeMillis() + "_" + user + fileExtension; //转换为系统路径 String filepath = Server.MapPath(floder + picurl); //保存图片 file.SaveAs(filepath); } bool upload = false; switch (type) { case "edit": upload = ProductManager.EditProduct(id, user, name, classid, picurl, price, amount); break; case "create": upload = ProductManager.AddProduct(user, name, classid, picurl, price, amount); break; } if (upload) { msg = "产品发布成功!"; Response.Redirect("/Product/ProductEdit.aspx?type=success&msg=" + msg); } else { msg = "产品发布失败!"; GoBack(); } } public String getAlert(String msg) { String html = "
"; html += ""; html += "{1}
"; return (msg == null || msg == "") ? "" : String.Format(html, type.Equals("success") ? type : "danger", msg); } public void GoBack() { Response.Write("windows.history.go(-1);"); } /// /// 验证是否指定的图片格式 /// /// /// public bool IsImage(string str) { bool isimage = false; string thestr = str.ToLower(); //限定只能上传jpg和gif图片 string[] allowExtension = { ".jpg", ".gif", ".bmp", ".png" }; //对上传的文件的类型进行一个个匹对 for (int i = 0; i < allowExtension.Length; i++) { if (thestr == allowExtension[i]) { isimage = true; break; } } return isimage; } } }