国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net中實體類對象賦值到表單的實現代碼

2024-07-10 13:25:36
字體:
來源:轉載
供稿:網友
有一個問題就是 :表單名稱和對象的屬性名(我是屬性賦值 你也可以用字段)要保持一樣,,有點不安全,不過后臺用挺好的,在說填寫表單數據后臺用的比較多

復制代碼 代碼如下:


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>實體對象</param>
/// <param>表單集合</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>實體對象</param>
/// <param>頁面對象</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);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉祥县| 青冈县| 巧家县| 屯昌县| 济源市| 彭水| 宜章县| 印江| 永州市| 清镇市| 呼玛县| 吕梁市| 神农架林区| 北宁市| 大竹县| 新和县| 苍南县| 济阳县| 建昌县| 合阳县| 顺平县| 治多县| 松滋市| 上林县| 汾阳市| 张家口市| 平塘县| 房产| 濮阳县| 旺苍县| 南宫市| 北安市| 泰顺县| 鄂温| 临洮县| 芦山县| 元谋县| 永城市| 望都县| 淅川县| 合水县|