ASP.NET利用反射进行物体与控件之间绑定
2014-08-28 22:59
302 查看
文单来自:http://bbs.blueidea.com/thread-2707666-1-1.html
可正常使用的:
此源码扩展的两个控件
来自老外的讲解:
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);
可正常使用的:
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);
相关文章推荐
- 利用反射实现ASP.NET控件和数据实体之间的双向绑定,并且在客户端自动验证输入的内容是否合法
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- 使用反射将业务对象绑定到 ASP.NET 窗体控件2
- [导入]使用反射将业务对象绑定到 ASP.NET 窗体控件(修改篇)
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- ASP.Net中通过Jquery前端对Repeater控件绑定的数据进行操作
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- 读使用反射将业务对象绑定到 ASP.NET 窗体控件有感
- C# Asp.net中的AOP框架 Microsoft.CCI, Mono.Cecil, Typemock Open-AOP API, PostSharp -摘自网络 (可以利用反射 Attribute 进行面向切面编程 可以用在记录整个方法的Log方面)
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- ASP.NET中对DataList控件进行数据绑定并分页
- 【ASP.NET】数据绑定控件之间的差别
- [ASP.NET]利用DataTable对DropDownList进行数据绑定
- 使用反射将业务对象绑定到 ASP.NET 窗体控件(转自MSDN)
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- 使用反射将业务对象绑定到 ASP.NET 窗体控件
- 读使用反射将业务对象绑定到 ASP.NET 窗体控件有感(二)
- ASP.NET中对DataList控件进行数据绑定并分页
- ASP.NET(2):在Init方法中进行控件数据源的绑定