发布于 2016-06-02 10:17:50 | 134 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


昨天在网上看到了一个利用反射表单赋值到实体类对象的一个方法,自己就在加了个方法,从实体对象到表单,觉的很不错非常省事,所以把他写成了一个类,供以后使用
有一个问题就是 :表单名称和对象的属性名(我是属性赋值 你也可以用字段)要保持一样,,有点不安全,不过后台用挺好的,在说填写表单数据后台用的比较多
 
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); 


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务