//新增 protected void dtCars_ItemInserting(object sender, DetailsViewInsertEventArgs e) { FileUpload fuAddPic = (FileUpload)dtCars.FindControl("fuAddPic"); TextBox txtAddCarName = (TextBox)dtCars.FindControl("txtAddCarName"); DropDownList ddlBrand = (DropDownList)dtCars.FindControl("ddlAddBrand"); TextBox txtAddPrice = (TextBox)dtCars.FindControl("txtAddPrice"); if (fuAddPic.FileName != "") { bool isPic = false; string fileExtension = System.IO.Path.GetExtension(fuAddPic.FileName); string[] exetis = { ".jpg", ".png", ".gif", ".bmp", ".jpeg" }; foreach (string exte in exetis) { if (exte == fileExtension) { isPic = true; break; } } if (isPic) { string path = Server.MapPath("~/Images/Car/"); if (!System.IO.File.Exists(path + fuAddPic.FileName)) { fuAddPic.PostedFile.SaveAs(path + fuAddPic.FileName); 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 = txtAddCarName.Text; c.Picture = fuAddPic.FileName; c.OffcialPrice = Convert.ToDecimal(txtAddPrice.Text); cm.AddCar3(c); dtCars.ChangeMode(DetailsViewMode.ReadOnly); bindCar(c.CarId); } catch (Exception ex) { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + ex.Message + "')", true); } } else { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('更新失败,实物图已存在')", true); } } else { ClientScript.RegisterStartupScript(this.GetType(), "", "alert('更新失败,只能上传jpg、png、gif、bmp、jpeg格式的图片')", true); } } }
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; } }