protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { if (HFId.Value == "") { CarSYSModels.Car c = cm.GetSelectById(1); lblCarName.Text = c.CarName; lblBrandName.Text = c.CarBrand.BrandName; lblClick.Text = c.Click.ToString(); lblPrice.Text = c.OfficilPrice.ToString(); imaCar.ImageUrl = "~/Images/" + c.Picture; } else { CarSYSModels.Car c = cm.GetSelectById(Convert.ToInt32(HFId.Value)); lblCarName.Text = c.CarName; lblBrandName.Text = c.CarBrand.BrandName; lblClick.Text = c.Click.ToString(); lblPrice.Text = c.OfficilPrice.ToString(); imaCar.ImageUrl = "~/Images/" + c.Picture; } } catch (Exception ex) { lblCarName.Text = ex.Message; } } }
//更新 protected void dtCars_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { FileUpload fuPic = (FileUpload)dtCars.FindControl("fuPic"); HiddenField hfPic = (HiddenField)dtCars.FindControl("hfPic"); TextBox txtCarName = (TextBox)dtCars.FindControl("txtCarName"); DropDownList ddlBrand = (DropDownList)dtCars.FindControl("ddlBrand"); TextBox txtPrice = (TextBox)dtCars.FindControl("txtPrice"); if (fuPic.FileName != "") { bool isPic = false; string fileExtentsion = System.IO.Path.GetExtension(fuPic.FileName); string[] extes = { ".jpg", ".png", ".gif", ".bmp", ".jpeg" }; foreach (string each in extes) { if (fileExtentsion == each) { isPic = true; break; } } if (isPic) { string path = Server.MapPath("~/Images/Car/"); if (!System.IO.File.Exists(path + fuPic.FileName)) { fuPic.PostedFile.SaveAs(path + fuPic.FileName); hfPic.Value = fuPic.FileName; } else { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('更新失败,实物图已存在')", true); return; } } else { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('更新失败,只能上传jpg、png、gif、bmp、jpeg格式的图片')", true); return; } } try { CarSYSModels.Car c = new CarSYSModels.Car(); c.CarBrand = new Brand(); c.CarBrand.BrandId = Convert.ToInt32(ddlBrand.SelectedValue); c.CarBrand.BrandName = ddlBrand.SelectedItem.Text; c.CarId = Convert.ToInt32(dtCars.DataKey.Value); c.CarName = txtCarName.Text; c.Picture = hfPic.Value; c.OffcialPrice = Convert.ToDecimal(txtPrice.Text); cm.UpdataCarById(c); } catch (Exception ex) { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + ex.Message + "')", true); } dtCars.ChangeMode(DetailsViewMode.ReadOnly);//更新后切换状态 bindCar(Convert.ToInt32(dtCars.DataKey.Value)); }
protected void btnSave_Click(object sender, EventArgs e) { if (fuPic.FileName != "") { bool isPic = false; string pathExtName = System.IO.Path.GetExtension(fuPic.FileName); string[] extes = { ".jpg", ".svp", ".png" }; foreach (string exte in extes) { if (exte == pathExtName) { isPic = true; break; } } if (isPic) { string path = Server.MapPath("~/Images/"); if (!System.IO.File.Exists(path + fuPic.FileName)) { fuPic.PostedFile.SaveAs(path + fuPic.FileName); } else { lblInfo.Text = "增加失败,实物图已存在"; return; } } else { lblInfo.Text = "增加失败,只能上传.jpg、.svp、.png格式的图片,请重新选择图片"; return; } } try { CarSYSModels.Car c = new CarSYSModels.Car(); c.CarName = txtName.Text.Trim(); c.CarBrand = new CarSYSModels.Brand(); c.CarBrand.BrandId = Convert.ToInt32(ddlBrand.SelectedValue); c.CarBrand.BrandName = ddlBrand.SelectedItem.Text; c.Picture = fuPic.FileName; c.OffcialPrice = Convert.ToDecimal(txtPrice.Text.Trim()); c.Click = 1; cm.AddCar3(c); lblInfo.Text = "增加成功,新增的车辆编号是:" + c.CarId; ClientScript.RegisterStartupScript(this.GetType(), "CleanFrom", "", true); } catch (Exception ex) { lblInfo.Text = "增加失败,出现异常,异常信息:" + ex.Message; } }