当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中实体类对象赋值到表单的实现代码

asp.net中实体类对象赋值到表单的实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

中药压片机,蔷薇公主之吻,米米养车

有一个问题就是 :表单名称和对象的属性名(我是属性赋值 你也可以用字段)要保持一样,,有点不安全,不过后台用挺好的,在说填写表单数据后台用的比较多
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
using system.collections.generic;
using system.reflection;
using system.collections.specialized;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
/// <summary>
/// 通过对象设置获取表单值
/// </summary>
namespace com.fun
{
public static class setformtomodel<t>
{
/// <summary>
/// 将表单赋予对对象
/// </summary>
/// <param name="t">实体对象</param>
/// <param name="form">表单集合</param>
public static void getvalue(t t, namevaluecollection form)
{
type type = t.gettype();
propertyinfo[] pi = type.getproperties();
foreach (propertyinfo p in pi)
{
if (form[p.name] != null)
{
p.setvalue(t, convert.changetype(form[p.name], p.propertytype), null);
}
}
}

/// <summary>
/// 将对象赋予表单
/// </summary>
/// <param name="t">实体对象</param>
/// <param name="c">页面对象</param>
public static void setvalue(t t,page page)
{
type type = t.gettype();
propertyinfo[] pi = type.getproperties();
foreach (propertyinfo p in pi)
{
system.web.ui.htmlcontrols.htmlinputtext text = page.findcontrol(p.name) as system.web.ui.htmlcontrols.htmlinputtext;
if (text != null)
{
text.value = p.getvalue(t, null).tostring();
}
}

}
}
}


//调用
mhousereco mh = new dhousereco().getmodel(id);
com.fun.setformtomodel<mhousereco>.setvalue(mh,this.page);

mhousereco mh = new mhousereco();
com.fun.setformtomodel<mhousereco>.getvalue(mh, this.request.form);

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网