您的位置:首页 > 编程语言 > ASP

ASP.NET利用反射进行物体与控件之间绑定

2014-08-28 22:59 302 查看
文单来自:http://bbs.blueidea.com/thread-2707666-1-1.html

可正常使用的:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Reflection;

namespace Darkangle
{
public class FormBinder
{
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);
bool success = false;
bool isCustom = false;
if (control is DarkangleListBox)
{
DarkangleListBox listBox = (DarkangleListBox)control;
string propertyValue = objProperty.GetValue(obj, null).ToString();
listBox.SetListValue(propertyValue);
isCustom = true;
}
if (control is DarkangleCheckBoxList)
{
DarkangleCheckBoxList checkBoxList = (DarkangleCheckBoxList)control;
string propertyValue = objProperty.GetValue(obj, null).ToString();
checkBoxList.SetListValue(propertyValue);
isCustom = true;
}
if (!isCustom)
{
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
{
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Checked", typeof(bool));
if (!success)
{
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "SelectedDate", typeof(DateTime));
if (success)
FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "VisibleDate", typeof(DateTime));
}
if (!success)
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Value", typeof(String));
if (!success)
success = FindAndSetControlProperty(obj, objProperty, control, controlPropertiesArray, "Text", typeof(String));
}
}
}
}
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;
}
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)
{
bool success = false;
bool isCustom = false;
Control control = container.FindControl(objProperty.Name);
if (control is DarkangleListBox)
{
DarkangleListBox listBox = (DarkangleListBox)control;
if (listBox.SelectedIndex > -1)
objProperty.SetValue(obj, Convert.ChangeType(listBox.GetListValue(), objProperty.PropertyType), null);
isCustom = true;
}
if (control is DarkangleCheckBoxList)
{
DarkangleCheckBoxList checkBoxList = (DarkangleCheckBoxList)control;
if (checkBoxList.SelectedIndex > -1)
objProperty.SetValue(obj, Convert.ChangeType(checkBoxList.GetListValue(), objProperty.PropertyType), null);
isCustom = true;
}
if (!isCustom)
{
if (control is ListControl)
{
ListControl listControl = (ListControl)control;
if (listControl.SelectedItem != null)
objProperty.SetValue(obj, Convert.ChangeType(listControl.SelectedItem.Value, objProperty.PropertyType), null);
}
else
{
Type controlType = control.GetType();
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();

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 == propertyName && controlProperty.PropertyType == type)
{
try
{
<span style="color:#3333FF;">  objProperty.SetValue(obj, Convert.ChangeType(controlProperty.GetValue(control, null), objProperty.PropertyType), null);</span>
return true;
}
catch
{
return false;
}
}
}
return false;
}
}
}


此源码扩展的两个控件

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace System.Web.UI.WebControls
{

public class DarkangleCheckBoxList : CheckBoxList
{
public void SetListValue(string listValue)
{
if (listValue == null || listValue.Trim() == string.Empty)
return;
string[] valueList = listValue.Split(new char[] { ',' });
if (valueList.Length == 0)
return;
for (int i = 0; i < valueList.Length; i++)
{
if (valueList[i].Length != 0 && valueList[i] != string.Empty)
this.Items.FindByValue(valueList[i]).Selected = true;
}
}
public string GetListValue()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.Items.Count; i++)
{
if (this.Items[i].Selected)
sb.Append(this.Items[i].Value + ",");
}
return sb.Remove(sb.Length - 1, 1).ToString();
}
}
public class DarkangleListBox : ListBox
{
public void SetListValue(string listValue)
{
if (listValue == null || listValue.Trim() == string.Empty)
return;
string[] valueList = listValue.Split(new char[] { ',' });
if (valueList.Length == 0)
return;
for (int i = 0; i < valueList.Length; i++)
{
if (valueList[i].Length != 0 && valueList[i] != string.Empty)
this.Items.FindByValue(valueList[i]).Selected = true;
}
}
public string GetListValue()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.Items.Count; i++)
{
if (this.Items[i].Selected)
sb.Append(this.Items[i].Value + ",");
}
return sb.Remove(sb.Length-1,1).ToString();
}
}
}


来自老外的讲解:

http://msdn.microsoft.com/en-us/library/aa478957.aspx

讲解没有出错,但源码在控件到对象时出错了。

源码下载地址:http://download.microsoft.com/download/0/6/7/067D02CE-35CE-4A57-9203-394A2A569C38/MSDNFormBinding.msi

备注:

使用EF框架

在page页使用范例中:

Lf_MultUser mult = northwind.Lf_MultUser.FirstOrDefault(m => m.FId == 1);

FormBinding.BindObjectToControls(mult, this);

在嵌套master中使用:

ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("MainContent");

Lf_MultUser mult = northwind.Lf_MultUser.FirstOrDefault(m => m.FId == 1);

FormBinding.BindObjectToControls(mult, cph);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: