asp.net中實(shí)體類對象賦值到表單的實(shí)現(xiàn)代碼
更新時間:2010年11月23日 22:44:43 作者:
昨天在網(wǎng)上看到了一個利用反射表單賦值到實(shí)體類對象的一個方法,自己就在加了個方法,從實(shí)體對象到表單,覺的很不錯非常省事,所以把他寫成了一個類,供以后使用
有一個問題就是 :表單名稱和對象的屬性名(我是屬性賦值 你也可以用字段)要保持一樣,,有點(diǎn)不安全,不過后臺用挺好的,在說填寫表單數(shù)據(jù)后臺用的比較多
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>
/// 通過對象設(shè)置獲取表單值
/// </summary>
namespace Com.Fun
{
public static class SetFormToModel<T>
{
/// <summary>
/// 將表單賦予對對象
/// </summary>
/// <param name="t">實(shí)體對象</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">實(shí)體對象</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();
}
}
}
}
}
//調(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);
復(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>
/// 通過對象設(shè)置獲取表單值
/// </summary>
namespace Com.Fun
{
public static class SetFormToModel<T>
{
/// <summary>
/// 將表單賦予對對象
/// </summary>
/// <param name="t">實(shí)體對象</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">實(shí)體對象</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();
}
}
}
}
}
//調(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);
您可能感興趣的文章:
- Asp.net中使用DapperExtensions和反射來實(shí)現(xiàn)一個通用搜索
- asp.net反射簡單應(yīng)用實(shí)例
- asp.net實(shí)現(xiàn)利用反射,泛型,靜態(tài)方法快速獲取表單值到Model的方法
- asp.net 反射減少代碼書寫量
- asp.net簡單頁面控件賦值實(shí)現(xiàn)方法
- asp.net水晶報表參數(shù)字段在代碼中賦值的方法
- ASP.NET服務(wù)器端控件RadioButtonList,DropDownList,CheckBoxList的取值、賦值用法
- asp.net 用戶控件讀取以及賦值
- asp.net利用反射實(shí)現(xiàn)給model類賦值的方法
相關(guān)文章
asp.net core mvc實(shí)現(xiàn)文件上傳實(shí)例
本篇文章主要介紹了asp.net core mvc實(shí)現(xiàn)文件上傳實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-12-12詳解在ASP.NET Core下使用SignalR技術(shù)
本篇文章主要介紹了在ASP.NET Core下使用SignalR技術(shù) ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02ASP.NET Core擴(kuò)展庫之日志功能的使用詳解
這篇文章主要介紹了ASP.NET Core擴(kuò)展庫之日志功能的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用.NET技術(shù),感興趣的朋友可以了解下2021-03-03使用Visual Studio創(chuàng)建ASP.NET Web API項(xiàng)目
這篇文章介紹了使用Visual Studio創(chuàng)建ASP.NET Web API項(xiàng)目的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03ASP.NET組件System.Web.Optimization原理及緩存問題詳解
這篇文章主要介紹了ASP.NET組件System.Web.Optimization的運(yùn)行原理,及基本的緩存問題,感興趣的小伙伴們可以參考一下2016-05-05asp.net 學(xué)習(xí)之路 項(xiàng)目整體框架簡單的搭建
最近剛學(xué)了些關(guān)于asp.net mvc方面的知識,于是了要拿個小項(xiàng)目來練練手,提高下自己的code能力跟思維能力2012-12-12Asp.Net?Core7?preview4限流中間件新特性詳解
這篇文章主要為大家介紹了Asp.Net?Core7?preview4限流中間件的新特性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05