分享
 
 
 

使用反射将业务对象绑定到 ASP.NET 窗体控件(修改篇)

王朝asp·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

MSDN上面的代码和下载的代码包也有那么多问题,晕死。

综合下载的代码和MSDN上的说明,修改了这段代码,现在能正常使用,特别是很多字段放在窗体上的时候,使用起来非常方便。

具体用法:把窗体上的控件ID属性与实体层的属性设置成一致。

绑定实体层到窗体的时候用:

web.model .test Test = new web.model.test();

Test = bll.GetModel(id);

BindObjectToControls(model,this);

要把窗体数据绑定到实体层的时候用:

BindControlsToObject(model,this);

using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Reflection;

namespace WEB

{

/// <summary>

/// A class with two static methods <see cref="BindObjectToControls"/> and <see cref="BindControlsToObject"/>

/// used to bind the values of and object's properties with web <see cref="Control"/>s that have their

/// ID property set to the name of the object property.

/// </summary>

public class FormBinding

{

/// <summary>

/// Binds an object's properties to <see cref="Control"/>s with the same ID as the propery name.

/// </summary>

/// <param name="obj">The object whose properties are being bound to forms Controls</param>

/// <param name="container">The control in which the form Controls reside (usually a Page or ContainerControl)</param>

public static void BindObjectToControls(object obj, Control container)

{

if (obj == null) return;

// Get the properties of the business object

//

Type objType = obj.GetType();

PropertyInfo[] objPropertiesArray = objType.GetProperties();

foreach (PropertyInfo objProperty in objPropertiesArray)

{

Control control = container.FindControl(objProperty.Name);

if (control == null) continue;

// handle ListControls (DropDownList, CheckBoxList, RadioButtonList)

//

if (control is ListControl)

{

ListControl listControl = (ListControl)control;

string propertyValue = objProperty.GetValue(obj, null).ToString();

ListItem listItem = listControl.Items.FindByValue(propertyValue);

if (listItem != null) listItem.Selected = true;

}

else

{

// get the properties of the control

//

Type controlType = control.GetType();

PropertyInfo[] controlPropertiesArray = controlType.GetProperties();

// test for common properties

//

bool success = false;

success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));

if (!success)

success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));

if (!success)

success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));

if (!success)

success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));

}

}

}

/// <summary>

/// Looks for a property name and type on a control and attempts to set it to the value in an object's property

/// of the same name.

/// </summary>

/// <param name="obj">The object whose properties are being retrieved</param>

/// <param name="objProperty">The property of the object being retrieved</param>

/// <param name="control">The control whose ID matches the object's property name.</param>

/// <param name="controlPropertiesArray">An array of the control's properties</param>

/// <param name="propertyName">The name of the Control property being set</param>

/// <param name="type">The correct type for the Control property</param>

/// <returns>Boolean for whether the property was found and set</returns>

private static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)

{

// iterate through control properties

//

foreach (PropertyInfo controlProperty in controlPropertiesArray)

{

// check for matching name and type

//

if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)

{

// set the control's property to the business object property value

//

controlProperty.SetValue(control, Convert.ChangeType(objProperty.GetValue(obj, null), type), null);

return true;

}

}

return false;

}

/// <summary>

/// Binds your the values in <see cref="Control"/>s to a business object.

/// </summary>

/// <param name="obj">The object whose properties are being bound to Control values</param>

/// <param name="container">The control in which the form Controls reside (usually a Page or ContainerControl)</param>

public static void BindControlsToObject(object obj, Control container)

{

if (obj == null) return;

// Get the properties of the business object

//

Type objType = obj.GetType();

PropertyInfo[] objPropertiesArray = objType.GetProperties();

foreach (PropertyInfo objProperty in objPropertiesArray)

{

Control control = container.FindControl(objProperty.Name);

if (control == null) continue;

if (control is ListControl)

{

ListControl listControl = (ListControl)control;

if (listControl.SelectedItem != null)

objProperty.SetValue(obj, Convert.ChangeType(listControl.SelectedItem.Value, objProperty.PropertyType), null);

}

else

{

// get the properties of the control

//

Type controlType = control.GetType();

PropertyInfo[] controlPropertiesArray = controlType.GetProperties();

// test for common properties

//

bool success = false;

success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));

if (!success)

success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));

if (!success)

success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));

if (!success)

success = FindAndGetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));

}

}

}

private static bool FindAndGetControlProperty(object obj,PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)

{

// 在整个控件属性中进行迭代

foreach (PropertyInfo controlProperty in controlPropertiesArray)

{

// 检查匹配的名称和类型

if (controlProperty.Name == "Text" && controlProperty.PropertyType == typeof(String))

{

// 将控件的属性设置为

// 业务对象属性值

try

{

objProperty.SetValue(obj, Convert.ChangeType(controlProperty.GetValue(control, null), objProperty.PropertyType), null);

return true;

}

catch

{

// 无法将来自窗体控件

// 的数据转换为

// objProperty.PropertyType

return false;

}

}

}

return true;

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有