最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net中實(shí)體類(lèi)對(duì)象賦值到表單的實(shí)現(xiàn)代碼

 更新時(shí)間:2010年11月23日 22:44:43   作者:  
昨天在網(wǎng)上看到了一個(gè)利用反射表單賦值到實(shí)體類(lèi)對(duì)象的一個(gè)方法,自己就在加了個(gè)方法,從實(shí)體對(duì)象到表單,覺(jué)的很不錯(cuò)非常省事,所以把他寫(xiě)成了一個(gè)類(lèi),供以后使用
有一個(gè)問(wèn)題就是 :表單名稱(chēng)和對(duì)象的屬性名(我是屬性賦值 你也可以用字段)要保持一樣,,有點(diǎn)不安全,不過(guò)后臺(tái)用挺好的,在說(shuō)填寫(xiě)表單數(shù)據(jù)后臺(tái)用的比較多
復(fù)制代碼 代碼如下:

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>
/// 通過(guò)對(duì)象設(shè)置獲取表單值
/// </summary>
namespace Com.Fun
{
public static class SetFormToModel<T>
{
/// <summary>
/// 將表單賦予對(duì)對(duì)象
/// </summary>
/// <param name="t">實(shí)體對(duì)象</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>
/// 將對(duì)象賦予表單
/// </summary>
/// <param name="t">實(shí)體對(duì)象</param>
/// <param name="c">頁(yè)面對(duì)象</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();
}
}

}
}
}


//調(diào)用
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);

相關(guān)文章

最新評(píng)論

徐汇区| 铜川市| 霍山县| 察隅县| 五峰| 莫力| 马关县| 西乌珠穆沁旗| 大竹县| 政和县| 汉寿县| 莫力| 资兴市| 武城县| 四子王旗| 崇州市| 石台县| 尼木县| 嘉定区| 龙山县| 鸡西市| 和平县| 华池县| 集安市| 宜宾市| 望奎县| 芮城县| 安远县| 仪陇县| 合江县| 平阴县| 舒兰市| 滁州市| 武夷山市| 乐业县| 乐至县| 舟曲县| 新和县| 西城区| 丰原市| 清水县|