个人开发框架总结(三)
2009-05-05 08:27:16 来源:WEB开发网BaseEditForm 提供数据添加与修改的窗体
属性
Info:修改时,从BaseListForm传来的信息对象
TypeID:类别Id
ParentID:信息的父对象
保护的方法
ShowErrorMessage:显示信息提示
重载方法
CreateListInstance:创建IList对象
AddInfo:数据添加时处理
ModifyInfo:数据修改时处理
ValidateData:验证数据
FillForm:将数据填充到窗体控件上
ClearForm:清理窗体控件的值
RegisterActiveControl:注册要激活的控件,Control数组是TabControl每一个TabPage中默认获得焦点的控件
以下给出一个示例代码:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
try
{
ATProductType da = (ATProductType)DAFactory.CreateDA(typeof(ATProductType));
//填充分类列表
tcbType.TableName = TProductType._TableName;
tcbType.TextField = TProductType._Name;
tcbType.IdField = TProductType._Id;
tcbType.ParentIdField = TProductType._ParentId;
tcbType.ChildCountField = TProductType._ChildCount;
tcbType.FillTypes(da.DataHelper, null);
tcbType.Value = TypeID;
//单位
ATUnit uda = (ATUnit)DAFactory.CreateDA(typeof(ATUnit));
TUnits units = uda.Select(TUnit._CompanyId + " = " + ContextArgs.Instance.CompanyId);
units.Insert(0, new TUnit());
cboUnit.DisplayMember = "Name";
cboUnit.DataSource = units;
cboUnit.Text = UnitName;
da.Dispose();
}
catch (System.Exception e1)
{
Utility.ShowErrorMessage(e1.Message);
}
}
/// <summary>
/// 创建一个实体集合
/// </summary>
/// <param name="type"></param>
protected override void CreateListInstance(Type type)
{
base.CreateListInstance(typeof(TProducts));
}
/// <summary>
/// 将实体信息填充到窗体控件
/// </summary>
/// <param name="source"></param>
protected override void FillForm(BaseModel source)
{
TProduct info = source as TProduct;
txtCode.Text = info.Code;
txtName.Text = info.Name;
ctbPrice.Decimal = info.Price;
txtSpec.Text = info.Spec;
UnitName = info.Unit;
TypeID = info.ProductTypeId;
}
/// <summary>
/// 清理窗体控件的值
/// </summary>
protected override void ClearForm()
{
txtCode.Text = "";
txtName.Text = "";
txtSpec.Text = "";
cboUnit.Text = "";
txtCode.Focus();
}
/// <summary>
/// 添加新的实体信息
/// </summary>
/// <returns></returns>
protected override BaseModel AddInfo()
{
try
{
ATProduct da = (ATProduct)DAFactory.CreateDA(typeof(ATProduct));
#region 检查以前删除的
QueryBuilder qb = new QueryBuilder();
qb.Append(QueryRelation.And, QueryCompare.Equal, TProduct._IsDelete, true);
qb.Append(QueryRelation.And, QueryCompare.Equal, TProduct._CompanyId, ContextArgs.Instance.CompanyId);
qb.Append(QueryRelation.And, QueryCompare.Equal, TProduct._Code, txtCode.Text);
TProduct oldInfo = da.Get(qb, null);
if (oldInfo != null)
{
DialogResult dresult = Utility.ShowQuestionMessageEx("历史记录中存在相同编码的产品"" + oldInfo.Name + "",n点击"是"恢复原来的资料,点击"否"删除原来的资料并创建新的。");
if (dresult == DialogResult.Yes)
{
//恢复
oldInfo.IsDelete = false;
da.DataHelper.ExecuteNonQuery("UPDATE TProduct SET IsDelete = 0 WHERE " + qb.ToString());
return oldInfo;
}
else if (dresult == DialogResult.No)
{
//删除
da.DataHelper.ExecuteNonQuery("DELETE FROM TProduct WHERE " + qb.ToString());
}
else
{
return null;
}
}
#endregion
TProduct info = da.NewEntity;
SetInfo(info);
if (da.Create(info))
{
CreateUnit();
return info;
}
}
catch (System.Exception e)
{
Utility.ShowErrorMessage(e.Message);
}
return null;
}
/// <summary>
/// 修改实体信息
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
protected override bool ModifyInfo(BaseModel source)
{
try
{
TProduct info = source as TProduct;
SetInfo(info);
ATProduct da = (ATProduct)DAFactory.CreateDA(typeof(ATProduct));
if (da.Update(info))
{
CreateUnit();
return true;
}
}
catch (System.Exception e)
{
Utility.ShowErrorMessage(e.Message);
}
return false;
}
/// <summary>
/// 验证数据
/// </summary>
/// <returns></returns>
protected override bool ValidateData()
{
bool ret = true;
if (txtCode.Text.Length == 0)
{
ShowErrorMessage(txtCode, "请输入编码");
ret = false;
}
if (txtName.Text.Length == 0)
{
ShowErrorMessage(txtName, "请输入名称");
ret = false;
}
if (tcbType.Value == null)
{
ShowErrorMessage(tcbType, "请选择类别");
ret = false;
}
try
{
//检查编码
ATProduct da = (ATProduct)DAFactory.CreateDA(typeof(ATProduct));
TProduct refer = Info == null ? null : (TProduct)Info;
if (da.IsExist(refer, TProduct._Code, txtCode.Text,
TProduct._CompanyId + " = " + ContextArgs.Instance.CompanyId + " AND " +
TProduct._IsDelete + " = 0"))
{
ShowErrorMessage(txtCode, "编码重复");
ret = false;
}
}
catch (System.Exception e)
{
Utility.ShowErrorMessage(e.Message);
}
return ret;
}
/// <summary>
/// 将窗体控件的值赋给实体
/// </summary>
/// <param name="info"></param>
private void SetInfo(TProduct info)
{
info.Code = txtCode.Text;
info.Name = txtName.Text;
info.Price = ctbPrice.Decimal;
info.ProductTypeId = int.Parse(tcbType.Value.ToString());
info.Unit = cboUnit.Text;
info.Spec = txtSpec.Text;
info.CompanyId = ContextArgs.Instance.CompanyId;
}
//添加单位
private void CreateUnit()
{
string name = cboUnit.Text;
ATUnit uda = (ATUnit)DAFactory.CreateDA(typeof(ATUnit));
if (!uda.IsExist(TUnit._Name, name, TUnit._CompanyId + " = " + ContextArgs.Instance.CompanyId))
{
TUnit unit = new TUnit();
unit.CompanyId = ContextArgs.Instance.CompanyId;
unit.Name = name;
uda.Create(unit);
}
}
更多精彩
赞助商链接