- 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;
- public partial class Vehicles_CarAdd : System.Web.UI.Page
- {
- //模板
- Model.Cars MC = new Model.Cars();
- //业务
- BLL.Cars BC = new BLL.Cars();
- protected void Page_Load(object sender, EventArgs e)
- {
- LoginLogic.MatchLoad("../", "Vehicles_CarAdd");
- if (!IsPostBack)
- {
- DropLoadSet();
- }
- }
- 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>
- 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("该车牌号已经存在!");
- }
- 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))
- {
- 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.Add(MC);
- MessageBox.Show("添加成功!");
- KeyValue_CPH.Text = "";
- KeyValue_CPXH.Text = "";
- KeyValue_FDJH.Text = "";
- KeyValue_JSY.Text = "";
- KeyValue_GMJG.Text = "";
- KeyValue_GMRQ.Text = "";
- KeyValue_NJRQ.Text = "";
- KeyValue_BZ.Text = "";
- DropLoadSet();
- }
- catch (Exception exp)
- {
- MessageBox.Show(exp.Message);
- }
- }
- }