有一个问题就是 :表单名称和对象的属性名(我是属性赋值 你也可以用字段)要保持一样,,有点不安全,不过后台用挺好的,在说填写表单数据后台用的比较多
复制代码 代码如下:
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);
复制代码 代码如下:
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);
标签:
实体类对象,表单
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“asp.net中实体类对象赋值到表单的实现代码”评论...