//==========================
//author:Dy
//date:20080329
//==========================
namespace BLL
{
/// <summary>
/// 说明: 利用反射将业务对象和控件绑定
/// 本类有两个静态方法 <see cref="BindObjectToControls"/> 和 <see cref="BindControlsToObject"/>
/// 用来绑定控件值或对象属性 <see cref="Control"/>
/// 控件ID 要和 业务对象属性名称 相同
///
/// </summary>
public class FormBinding
{
/// <summary>
/// 绑定对象属性 的值到同名控件 <see cref="Control"/>
/// </summary>
/// <param name="obj">属性要绑定窗体控件的对象</param>
/// <param name="container">控件容器 通常是Page 页面 </param>
public static void BindObjectToControls(object obj, Control container)
{
if (obj == null) return;
// 得到业务对象属性
//
Type objType = obj.GetType();
PropertyInfo[] objPropertiesArray = objType.GetProperties();
foreach (PropertyInfo objProperty in objPropertiesArray)
{
Control control = container.FindControl(objProperty.Name);
// 列表控件 (DropDownList, CheckBoxList, RadioButtonList)
//
if (control is ListControl)
{
ListControl listControl = (ListControl)control;
foreach (ListItem lItem in listControl.Items)
{
lItem.Selected = false;
}
string propertyValue = objProperty.GetValue(obj, null).ToString();
string[] s = propertyValue.Split(',');
foreach (string i in s)
{
ListItem listItem = listControl.Items.FindByText(i);
if (listItem != null) listItem.Selected = true;
}
}
else if (control != null)
{
// 得到控件属性
//
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
//
// 测试通用属性
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));
if (!success)
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "InnerHtml", typeof(String));
}
}
}
/// <summary>
/// 查找控件属性和类型并尝试给业务对象同名属性赋值
/// </summary>
/// <param name="obj">已找到属性的对象</param>
/// <param name="objProperty">已找到对象 的 属性</param>
/// <param name="control">控件ID和业务对象属性名匹配的控件</param>
/// <param name="controlPropertiesArray">控件属性集合</param>
/// <param name="propertyName">要设置的控件属性名称</param>
/// <param name="type">控件属性类型校正</param>
/// <returns>属性是否被找到并设置</returns>
private static bool FindAndSetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
{
// 遍历控件属性
//
foreach (PropertyInfo controlProperty in controlPropertiesArray)
{
// 检查属性名称和类型
//
if (controlProperty.Name == propertyName && controlProperty.PropertyType == type)
{
// 设置控件属性的值到业务对象
//
controlProperty.SetValue(control, Convert.ChangeType(objProperty.GetValue(obj, null), type), null);
return true;
}
}
return false;
}
/// <summary>
/// 设置控件 <see cref="Control"/>s值到业务对象
/// </summary>
/// <param name="obj">要绑定的业务对象</param>
/// <param name="container">控件容器</param>
public static void BindControlsToObject(object obj, Control container)
{
if (obj == null) return;
// 获取业务对象属性
//
Type objType = obj.GetType();
PropertyInfo[] objPropertiesArray = objType.GetProperties();
foreach (PropertyInfo objProperty in objPropertiesArray)
{
Control control = container.FindControl(objProperty.Name);
if (control is ListControl)
{
ListControl listControl = (ListControl)control;
string str="";
if (listControl.SelectedItem != null)
for (int i = 0; i < listControl.Items.Count; i++)
{
if (listControl.Items[i].Selected)
{
str += listControl.Items[i].Text+",";
}
}
objProperty.SetValue(obj, Convert.ChangeType(str, objProperty.PropertyType), null);
}
else if (control is HtmlInputHidden)
{
try
{
HtmlInputHidden hidControl = (HtmlInputHidden)control;
//hidControl.Value = hidControl.Value==""?"0":hidControl.Value;
objProperty.SetValue(obj, Convert.ChangeType(hidControl.Value, objProperty.PropertyType), null);
}
catch
{
}
}
else if (control is HtmlInputText)
{
try
{
HtmlInputText txtControl = (HtmlInputText)control;
//txtControl.Value = txtControl.Value==""?"0":txtControl.Value;
objProperty.SetValue(obj, Convert.ChangeType(txtControl.Value, objProperty.PropertyType), null);
}
catch
{
}
}
else if (control != null)
{
// 获取控件属性
//
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
// 尝试通用属性
//
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));
}
}
}
/// <summary>
/// 查找控件值并尝试赋值给同名业务对象属性
///
/// </summary>
/// <param name="obj">待赋值的业务对象</param>
/// <param name="objProperty">业务对象属性</param>
/// <param name="control">同名控件</param>
/// <param name="controlPropertiesArray">控件数组</param>
/// <param name="propertyName">得到的控件属性名称</param>
/// <param name="type">控件属性类型</param>
/// <returns>属性是否找到</returns>
private static bool FindAndGetControlProperty(object obj, PropertyInfo objProperty, Control control, PropertyInfo[] controlPropertiesArray, string propertyName, Type type)
{
// 遍历控件属性
//
foreach (PropertyInfo controlProperty in controlPropertiesArray)
{
// 检查名称和类型
//
if (controlProperty.Name == propertyName && controlProperty.PropertyType == typeof(String))
{
// 控件赋值给业务对象
//
try
{
Object ctrValue = Convert.ChangeType(controlProperty.GetValue(control, null), objProperty.PropertyType);
objProperty.SetValue(obj, ctrValue, null);
return true;
}
catch
{
//
// 没有找到控件对应的业务对象属性
return false;
}
}
}
return false;
}
}
}
实体类的属性和控件的id命名一致既可以了
调用时
赋值,实体类给控件赋值:BLL.FormBinding.BindObjectToControls(model, this);//model为实体类。
取值,控件的值赋给实体类:BLL.FormBinding.BindControlsToObject(model, this);
一句话完成取值和赋值。