- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Text.RegularExpressions;
- using System.IO;
- using System.Text;
- public partial class Vehicles_CarEditor : System.Web.UI.Page
- {
- //图片
- public string StrHtmlImage;
- //模板
- Model.Cars MC = new Model.Cars();
- //业务
- BLL.Cars BC = new BLL.Cars();
- protected void Page_Load(object sender, EventArgs e)
- {
- LoginLogic.MatchLoad("../", "Vehicles_CarEditor");
- if (!IsPostBack)
- {
- object objid = Request.QueryString["id"];
- if (objid != null)
- {
- DropLoadSet();
- int ri = int.Parse(objid.ToString());
- MC = BC.GetModel(ri);
- KeyValue_CPH.Text = MC.CPH;
- KeyValue_CPXH.Text = MC.CPXH;
- KeyValue_FDJH.Text = MC.FDJH;
- KeyValue_CLLX.SelectedValue = MC.CLLX;
- KeyValue_JSY.Text = MC.JSY;
- KeyValue_GMJG.Text = MC.GMJG.ToString();
- KeyValue_GMRQ.Text = MC.GMRQ.ToString();
- KeyValue_NJRQ.Text = MC.NJRQ.ToString();
- KeyValue_DQZT.SelectedValue = MC.DQZT;
- KeyValue_BZ.Text = MC.BZ;
- StrHtmlImage = BindImage(MC.CLTPPath);
- }
- }
- }
- public void DropLoadSet()
- {
- //车辆类型
- KeyValue_CLLX.DataTextField = "Text";
- KeyValue_CLLX.DataValueField = "Value";
- KeyValue_CLLX.DataSource = EnumGet.CarTypeGetEnum();
- KeyValue_CLLX.DataBind();
- ListItem linone = new ListItem();
- linone.Text = "--请选择--";
- linone.Value = "0";
- KeyValue_CLLX.Items.Add(linone);
- KeyValue_CLLX.SelectedIndex = KeyValue_CLLX.Items.Count - 1;
- //当前状态
- KeyValue_DQZT.DataTextField = "Text";
- KeyValue_DQZT.DataValueField = "Value";
- KeyValue_DQZT.DataSource = EnumGet.CurrentStateGetEnum();
- KeyValue_DQZT.DataBind();
- ListItem linone2 = new ListItem();
- linone2.Text = "--请选择--";
- linone2.Value = "0";
- KeyValue_DQZT.Items.Add(linone2);
- KeyValue_DQZT.SelectedIndex = KeyValue_DQZT.Items.Count - 1;
- }
- /// <summary>
- /// 图片显示
- /// </summary>
- /// <param name="filename"></param>
- /// <returns></returns>
- public string BindImage(string filename)
- {
- MC.ID = int.Parse(Request.QueryString["id"].ToString());
- int id = MC.ID;
- StringBuilder sb = new StringBuilder();
- if (BC.GetCount("CLTPPath = '" + filename + "' and ID = " + id.ToString()) <= 0)
- {
- sb.Append("<center>暂无照片</center>");
- return sb.ToString();
- }
- if (String.IsNullOrEmpty(filename))
- {
- sb.Append("<center>暂无照片</center>");//5|1|a|s|p|x
- }
- else
- {
- sb.Append("<img src='upload/" + filename + "' width='250' height='150' />");
- }
- return sb.ToString();
- }
- /// <summary>
- /// 上传图片
- /// </summary>
- public void Upload()
- {
- try
- {
- string file_path = KeyValue_CLTPPath.PostedFile.FileName;
- int file_length = KeyValue_CLTPPath.PostedFile.ContentLength;
- string fileType = file_path.Substring(file_path.LastIndexOf(".") + 1).ToLower();
- if (fileType != "jpeg" && fileType != "gif" && fileType != "jpg")
- {
- throw new Exception("图片格式不正确!");
- }
- if (file_length > 80000)
- {
- throw new Exception("图片太大!");
- }
- string ToFileImgStr = "~/Vehicles/upload/car" + KeyValue_CPH.Text;
- string fullpath = Server.MapPath(ToFileImgStr + Path.GetFileName(file_path));
- fullpath = fullpath.Substring(0, fullpath.LastIndexOf("\")) + "\car" + KeyValue_CPH.Text + ".jpg";
- KeyValue_CLTPPath.PostedFile.SaveAs(fullpath);
- MessageBox.Show("上传图片成功!");
- }
- catch (Exception exp)
- {
- MessageBox.Show(exp.Message);
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- try
- {
- ////验证车牌号不为空
- //if (String.IsNullOrEmpty(KeyValue_CPH.Text.Trim()))
- //{
- // throw new Exception("车牌号不能为空!");
- //}
- //#region 验证车牌号的正则表达式
- ////Regex rg = new Regex(@"^[u4e00-u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_u4e00-u9fa5]$");
- ////if (!rg.IsMatch(KeyValue_CPH.Text))
- ////{
- //// throw new Exception("车牌号不符合规范!");
- ////}
- //#endregion
- ////验证车牌号是否存在
- //if (BC.GetCount("CPH = '" + KeyValue_CPH.Text + "'") > 0)
- //{
- // throw new Exception("该车牌号已经存在!");
- //}
- MC = BC.GetModel(int.Parse(Request.QueryString["id"].ToString()));
- if (String.IsNullOrEmpty(KeyValue_CPXH.Text.Trim()))
- {
- throw new Exception("厂牌型号不能为空!");
- }
- if (KeyValue_CLLX.SelectedValue == "0")
- {
- throw new Exception("请选择车辆类型!");
- }
- decimal DKeyValue_GMJG = 0;
- if (!decimal.TryParse(KeyValue_GMJG.Text, out DKeyValue_GMJG))
- {
- throw new Exception("购买价格不正确!");
- }
- if (String.IsNullOrEmpty(KeyValue_GMRQ.Text))
- {
- throw new Exception("购买日期不能为空!");
- }
- if (KeyValue_DQZT.SelectedValue == "0")
- {
- throw new Exception("请选择当前状态!");
- }
- if (!String.IsNullOrEmpty(KeyValue_CLTPPath.PostedFile.FileName))
- {
- Upload();
- }
- MC.CPH = KeyValue_CPH.Text;
- MC.FDJH = KeyValue_FDJH.Text;
- MC.CPXH = KeyValue_CPXH.Text;
- MC.CLLX = KeyValue_CLLX.SelectedValue;
- MC.JSY = KeyValue_JSY.Text;
- MC.GMJG = DKeyValue_GMJG;
- if (String.IsNullOrEmpty(KeyValue_CLTPPath.PostedFile.FileName))
- {
- if (String.IsNullOrEmpty(MC.CLTPPath))
- {
- MC.CLTPPath = "";
- }
- else
- {
- MC.CLTPPath = MC.CLTPPath;
- }
- }
- else
- {
- MC.CLTPPath = "car" + KeyValue_CPH.Text + ".jpg";
- }
- MC.GMRQ = Convert.ToDateTime(KeyValue_GMRQ.Text);
- MC.NJRQ = Convert.ToDateTime(KeyValue_NJRQ.Text);
- MC.DQZT = KeyValue_DQZT.SelectedValue;
- MC.BZ = KeyValue_BZ.Text;
- BC.Update(MC);
- MessageBox.Show("更新成功!");
- StrHtmlImage = BindImage(MC.CLTPPath);
- }
- catch (Exception exp)
- {
- MessageBox.Show(exp.Message);
- }
- }
- }