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

文单来自: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);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值