using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.reflection;
using system.io;
using system.text;
namespace projectdemo.common
{
public static class entitycopy
{
#region
///
/// 通过搜索页面对应控件来构造model对象(要求控件必须以“_model的属性名”来命名(如:_name),并大小写一致)
///
/// 要构造的model对象()
/// 控件的容器(比如page或者master的站位控件)
/// 返回参数里model
public static t getmodel(t model, control parentcontrol)
{
type t = model.gettype();
propertyinfo[] pi = t.getproperties();
foreach (propertyinfo p in pi)
{
setcontrolvaluetomodel(model, p, parentcontrol);
}
return model;
}
///
/// 把页面控件上的值赋值给model对象(要求控件必须以“_model的属性名”来命名(如:_name),并大小写一致)
///
/// 要赋值的model对象
/// 某个属性
/// 控件的容器(比如page或者master的站位控件)
private static void setcontrolvaluetomodel(object model, propertyinfo p, control parentcontrol)
{
control control = parentcontrol.findcontrol("_" p.name);
if (control != null)
{
type t_c = control.gettype();
switch (t_c.fullname)
{
case "system.web.ui.webcontrols.textbox": setvalue(model, p, ((textbox)control).text); break;
case "system.web.ui.webcontrols.checkbox": setvalue(model, p, ((checkbox)control).checked); break;
case "system.web.ui.webcontrols.checkboxlist":
checkboxlist cbl = ((checkboxlist)control);
stringbuilder sb = new stringbuilder();
for (int i = 0; i < cbl.items.count; i )
{
if (cbl.items[i].selected)
{
sb.append(i);
sb.append(",");
}
}
setvalue(model, p, sb.tostring().trimend(',')); break;
case "system.web.ui.webcontrols.image": setvalue(model, p, ((image)control).imageurl); break;
case "system.web.ui.webcontrols.dropdownlist": setvalue(model, p, ((dropdownlist)control).selectedvalue); break;
case "system.web.ui.webcontrols.radiobuttonlist": setvalue(model, p, ((radiobuttonlist)control).selectedvalue); break;
case "system.web.ui.webcontrols.hiddenfield": setvalue(model, p, ((hiddenfield)control).value); break;
default: break;
}
}
}
///
/// 把值赋给指定model对象指定属性上
///
/// 需要赋值的model对象
/// 某个属性
/// 要赋给model的属性的值
private static void setvalue(object model, propertyinfo p, object value)
{
if (p.propertytype.fullname == "system.guid")
{
p.setvalue(model, new guid(value.tostring()), null);
}
else
{
p.setvalue(model, convert.changetype(value, p.propertytype), null);
}
}
#endregion
#region 反射model绑定页面控件
///
/// 绑定model的值到页面上对应控件(要求控件必须以“_model的属性名”来命名(如:_name),并大小写一致)
///
/// 赋好值的model
/// 控件的容器(比如page或者master的站位控件)
public static void bindcontrols(object model, control parentcontrol)
{
type t = model.gettype();
propertyinfo[] pi = t.getproperties();
foreach (propertyinfo p in pi)
{
setmodelvaluetocontrol(model, p, parentcontrol);
}
}
///
/// 把model的值赋给页面上的控件(目前只针对web)
///
/// 赋好值的model
/// model的某个属性
/// 控件的容器(比如page或者master的站位控件)
private static void setmodelvaluetocontrol(object model, propertyinfo p, control parentcontrol)
{
control control = parentcontrol.findcontrol("_" p.name);
if (control != null)
{
type t_c = control.gettype();
switch (t_c.fullname)
{
case "system.web.ui.webcontrols.textbox": ((textbox)control).text = p.getvalue(model, null).tostring(); break;
case "system.web.ui.webcontrols.label": ((label)control).text = p.getvalue(model, null).tostring(); break;
case "system.web.ui.webcontrols.literal": ((literal)control).text = p.getvalue(model, null).tostring(); break;
case "system.web.ui.webcontrols.image": ((image)control).imageurl = p.getvalue(model, null).tostring(); break;
case "system.web.ui.webcontrols.dropdownlist": ((dropdownlist)control).selectedvalue = p.getvalue(model, null).tostring(); break;
case "system.web.ui.webcontrols.radiobuttonlist": ((radiobuttonlist)control).selectedvalue = p.getvalue(model, null).tostring(); break;
case "system.web.ui.webcontrols.checkbox": ((checkbox)control).checked = (bool)p.getvalue(model, null); break;
case "system.web.ui.webcontrols.checkboxlist":
string[] arr = ((string)p.getvalue(model, null)).split(',');
checkboxlist cbl = ((checkboxlist)control);
for (int i = 0; i < arr.length; i )
{
cbl.items[int.parse(arr[i])].selected = true;
}
break;
case "system.web.ui.webcontrols.hiddenfield": ((hiddenfield)control).value = p.getvalue(model, null).tostring(); break;
default: break;
}
}
}
#endregion
}
public class person
{
public string name { set; get; }
public string sex { set; get; }
}
}
posted on 2013-06-09 17:13
sanmao 阅读(607)
评论(0) 编辑 收藏