using System; using System.Collections.Generic; using System.Linq; namespace FineUIPro.Web.HSSE.PowerMG { /// /// 编辑配电箱页面(模拟数据阶段,数据存于 Session,接入数据库后替换为数据库操作) /// public partial class PowerBoxEdit : PageBase { #region 定义项 /// /// 主键 /// private string PowerBoxId { get { return (string)ViewState["PowerBoxId"]; } set { ViewState["PowerBoxId"] = value; } } #endregion #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.btnClose.OnClientClick = ActiveWindow.GetHideReference(); this.PowerBoxId = Request.Params["PowerBoxId"]; if (!string.IsNullOrEmpty(this.PowerBoxId)) { ////编辑:回填数据 List list = this.GetMockList(); PowerBoxInfo info = list.FirstOrDefault(x => x.PowerBoxId == this.PowerBoxId); if (info != null) { this.txtPowerBoxCode.Text = info.PowerBoxCode; this.txtPowerBoxName.Text = info.PowerBoxName; this.ddlBoxType.SelectedValue = info.BoxType; this.txtInstallPlace.Text = info.InstallPlace; this.txtRatedCapacity.Text = info.RatedCapacity; this.ddlVoltageLevel.SelectedValue = info.VoltageLevel; this.txtManagerUnit.Text = info.ManagerUnit; this.txtManagerMan.Text = info.ManagerMan; this.txtManagerTel.Text = info.ManagerTel; this.ddlStates.SelectedValue = info.States; if (info.LastCheckDate.HasValue) { this.dpLastCheckDate.SelectedDate = info.LastCheckDate.Value; } this.txtRemark.Text = info.Remark; } } else { ////新增:自动生成编号 this.txtPowerBoxCode.Text = this.GetNewCode(); } } } #endregion #region 模拟数据操作(接入数据库后删除本区域代码) /// /// 获取模拟数据列表(来自Session) /// private List GetMockList() { return Session[PowerBox.MockSessionKey] as List; } /// /// 自动生成配电箱编号(PDX-XXX,取现有最大序号加1) /// private string GetNewCode() { List list = GetMockList(); int max = 0; if (list != null) { foreach (PowerBoxInfo item in list) { string code = item.PowerBoxCode; if (!string.IsNullOrEmpty(code) && code.StartsWith("PDX-")) { int num = 0; if (int.TryParse(code.Substring(4), out num) && num > max) { max = num; } } } } return string.Format("PDX-{0:D3}", max + 1); } #endregion #region 保存 /// /// 保存按钮事件 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { List list = GetMockList(); if (list == null) { list = new List(); } PowerBoxInfo info = new PowerBoxInfo { PowerBoxCode = this.txtPowerBoxCode.Text.Trim(), PowerBoxName = this.txtPowerBoxName.Text.Trim(), BoxType = this.ddlBoxType.SelectedValue, InstallPlace = this.txtInstallPlace.Text.Trim(), RatedCapacity = this.txtRatedCapacity.Text.Trim(), VoltageLevel = this.ddlVoltageLevel.SelectedValue, ManagerUnit = this.txtManagerUnit.Text.Trim(), ManagerMan = this.txtManagerMan.Text.Trim(), ManagerTel = this.txtManagerTel.Text.Trim(), States = this.ddlStates.SelectedValue, LastCheckDate = this.dpLastCheckDate.SelectedDate, Remark = this.txtRemark.Text.Trim() }; if (!string.IsNullOrEmpty(this.PowerBoxId)) { ////编辑:更新原记录 info.PowerBoxId = this.PowerBoxId; int index = list.FindIndex(x => x.PowerBoxId == this.PowerBoxId); if (index >= 0) { list[index] = info; } else { list.Insert(0, info); } } else { ////新增:插入记录 info.PowerBoxId = Guid.NewGuid().ToString(); list.Insert(0, info); } Session[PowerBox.MockSessionKey] = list; PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference()); } #endregion } }