ASP.NET偷懒大法四(动态生成表单对象)
做表单提交时,经常的做法是拖TextBox等控件到页面,然后点提交按钮,cs代码把控件的值一个一个的赋给一个对象,然后调用一个Save的方法 提交成功!烦琐的操作,基本在每一个表单添加修改的时候做一遍。我们这些烦琐的事情交给.net做吧。
从Panel继承一个自定义控件,当成一个区间,写一个方法遍历这个区间所有的控件,自动给类赋值
首页给这个控件一个属性
public string Entity
{
get
{
String s = (String)ViewState["Entity"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Entity"] = value;
}
}
属性的值就是这个表单所操作的实体类
然后写个方法,反射实体类,变量区间中所有的控件,赋值
public object GetEntity()
{
Type t = Type.GetType(Entity, true);
object entity = Activator.CreateInstance(t);
foreach (object var in this.Controls)
{
Type ControlType = var.GetType();
switch (ControlType.Name)
{
case "SMTextBox":
if (!string.IsNullOrEmpty((var as FrameworkWeb.Web.SMTextBox).ID))
{
System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as FrameworkWeb.Web.SMTextBox).ID);
if (propertyInfo != null)
{
string tempText = (var as FrameworkWeb.Web.SMTextBox).Text;
if (!string.IsNullOrEmpty(tempText))
propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
}
}
break;
case "TextBox":
if (!string.IsNullOrEmpty((var as TextBox).ID))
{
System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as TextBox).ID);
if (propertyInfo != null)
{
string tempText = (var as TextBox).Text;
if (!string.IsNullOrEmpty(tempText))
propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
}
}
break;
case "SMDTPicker":
if (!string.IsNullOrEmpty((var as SMDTPicker).ID))
{
System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as SMDTPicker).ID);
if (propertyInfo != null)
{
string tempText = (var as SMDTPicker).Text;
if (!string.IsNullOrEmpty(tempText))
propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
}
}
break;
case "DropDownList":
if (!string.IsNullOrEmpty((var as DropDownList).ID))
{
System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as DropDownList).ID);
if (propertyInfo != null)
{
string tempText = (var as DropDownList).SelectedValue;
propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
}
}
break;
case "HiddenField":
if (!string.IsNullOrEmpty((var as HiddenField).ID))
{
System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as HiddenField).ID);
if (propertyInfo != null)
{
string tempText = (var as HiddenField).Value;
propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
}
}
break;
}
}
return entity;
}
前台调用的时候直接执行这个方法就可以得到这个对象了,如果有一些特殊的情况,再二次处理就ok啦。
然后就是修改啦,修改的时候需要先给控件赋值,然后用户修改后提交。这个也可以用类似的原来简化操作。
给我们的控件增加一个数据库源的属性
public virtual object DataSource
{
get
{
return this.dataSource;
}
set
{
this.dataSource = value;
}
}
增加一个数据绑定的时候的事件
public delegate void SMFormDataBindEventHandler(object source, SMFormEventArgs e);
[Category("Action"), Description("DataBind")]
public event SMFormDataBindEventHandler DataBindCommand;
///这里存的是自动赋好值的一个对象,方便二次处理
public class SMFormEventArgs : EventArgs
{
public readonly object tempObject;
public SMFormEventArgs(object tempObject)
{
this.tempObject = tempObject;
}
}
这是为了处理特殊的情况,有时候有些控件可能不是直接取数据库的值,就可以利用这个事件进行二次处理啦
然后给控件赋值
protected new void DataBind()
{
PropertyInfo p;
foreach (Control var in this.Controls)
{
switch (var.GetType().Name.ToUpper())
{
case "TEXTBOX":
System.Web.UI.WebControls.TextBox textBox = var as System.Web.UI.WebControls.TextBox;
p = DataSource.GetType().GetProperty(var.ID);
if (p != null)
textBox.Text = p.GetValue(DataSource, null).ToString().Trim();
break;
case "SMTEXTBOX":
FrameworkWeb.Web.SMTextBox smtextBox = var as FrameworkWeb.Web.SMTextBox;
p = DataSource.GetType().GetProperty(var.ID);
if (p != null)
smtextBox.Text = p.GetValue(DataSource, null).ToString().Trim();
break;
case "DROPDOWNLIST":
System.Web.UI.WebControls.DropDownList ddl = var as System.Web.UI.WebControls.DropDownList;
p = DataSource.GetType().GetProperty(var.ID);
if (p != null)
{
object o = p.GetValue(DataSource, null);
if (o!=null)
ddl.SelectedValue = p.GetValue(DataSource, null).ToString().Trim();
}
break;
case "LITERAL":
System.Web.UI.WebControls.Literal lit = var as System.Web.UI.WebControls.Literal;
p = DataSource.GetType().GetProperty(lit.ID);
if (p != null)
lit.Text = p.GetValue(DataSource, null).ToString().Trim();
break;
case "HIDDENFIELD":
System.Web.UI.WebControls.HiddenField hf = var as System.Web.UI.WebControls.HiddenField;
p = DataSource.GetType().GetProperty(hf.ID);
if (p != null)
hf.Value = p.GetValue(DataSource, null).ToString().Trim();
break;
case "SMDTPICKER":
FrameworkWeb.Web.SMDTPicker smDTPicker = var as FrameworkWeb.Web.SMDTPicker;
p = DataSource.GetType().GetProperty(var.ID);
if (p != null)
smDTPicker.Text = p.GetValue(DataSource, null).ToString().Trim();
break;
break;
}
}
}
这样这个控件就基本完成了,调用的时候基本不用写什么代码啦
拖好控件,在提交的事件中只需要写
Model.BTS item = smform.GetEntity() as Model.BTS;
DAL.BTS.Save(item);
修改的时候
Model.BTS btsModel== DAL.BTS.GetModel("sCode");
smform.DataSource = btsModel;
然后在事件中做二次处理
protected void smform_DataBindCommand(object source, FrameworkWeb.Form.SMFormEventArgs e)
{
Model.BTS item = e.tempObject as Model.BTS;
if (item != null)
{
DropDownList dp = smform.FindControl("isEnable") as DropDownList;
dp.Items.FindByValue(item.isEnable.ToString()).Selected = true;
if (item.dicSectorType>0)
(smform.FindControl("dicSectorType") as DropDownList).Items.FindByValue(item.dicSectorType.ToString()).Selected = true;
if (item.dicAreaType > 0)
(smform.FindControl("dicAreaType") as DropDownList).Items.FindByValue(item.dicAreaType.ToString()).Selected = true;
//if (item.fkDeviceID > 0)
// (smform.FindControl("fkDeviceID") as DropDownList).Items.FindByValue(item.fkDeviceID.ToString()).Selected = true;
}
}
然后保存
Model.BTS item = smform.GetEntity() as Model.BTS;
DAL.BTS.Update(item);
一切就是如此easy :)