Files
SGGL_HBAZ/SGGL/FineUIPro.Web/HSSE/PowerMG/PowerBoxEdit.aspx.cs
T

163 lines
5.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
namespace FineUIPro.Web.HSSE.PowerMG
{
/// <summary>
/// 编辑配电箱页面(模拟数据阶段,数据存于 Session,接入数据库后替换为数据库操作)
/// </summary>
public partial class PowerBoxEdit : PageBase
{
#region
/// <summary>
/// 主键
/// </summary>
private string PowerBoxId
{
get
{
return (string)ViewState["PowerBoxId"];
}
set
{
ViewState["PowerBoxId"] = value;
}
}
#endregion
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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<PowerBoxInfo> 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
/// <summary>
/// 获取模拟数据列表(来自Session)
/// </summary>
private List<PowerBoxInfo> GetMockList()
{
return Session[PowerBox.MockSessionKey] as List<PowerBoxInfo>;
}
/// <summary>
/// 自动生成配电箱编号(PDX-XXX,取现有最大序号加1)
/// </summary>
private string GetNewCode()
{
List<PowerBoxInfo> 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
/// <summary>
/// 保存按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
List<PowerBoxInfo> list = GetMockList();
if (list == null)
{
list = new List<PowerBoxInfo>();
}
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
}
}