RSS
热门关键字:  数据挖掘  数据仓库  商业智能  人工智能  搜索引擎
当前位置 :| 首页>编程技术>UML教程>

数据对象类表单自动绑定

来源: 作者: 时间:2007-01-27 点击:

DarkangleFormBinder

本文附带的代码在Asp.Net 2.0下面调试通过 数据挖掘实验室

先讲一下背景数据挖掘研究院

在.Net多层结构程序中,我们会使用一些O/R MAPPING的工具将数据库表或视图映射成实体数据模形用于封装数据,开发中我们可能经常会写这样的代码,例如,我们有一个Student的表,包含一些字段,如Name, Sex, Age, Grade, Height, Weight, ChooseClass.....

数据挖掘研究院

我们假设有一个录入学生信息的表单,我们可能会写这样的一些代码,在前台上: 数据挖掘研究院

姓名:<asp:TextBox ID="Name" runat="Server" />
年龄:<asp:TextBox ID="Age" runat = "Server" />
年级:<aspropDownList ID="Grade" runat = "server" />
课外活动: <asp:CheckBoxList ID="ChooseClass" runat= "server" /> 数据挖掘研究院

后台呢: 数据挖掘实验室

我们需要先构造一个新的对象: 数据挖掘研究院

Student s= new Student();

数据挖掘研究院

然后设置它的属性: 数据挖掘研究院

s.Name = Name.Text;
s.Age = Age.Text;
s.Grade = Grade.SelectedValue; 数据挖掘研究院

然后保存到后台数据库

数据挖掘研究院

如果是读取一条记录出来显示在页面上,我们可能先要取得一个对象实例:

数据挖掘研究院

Student s = bll.GetStudentById(id);
Name.Text = s.Name;
Age.Text = s.Age;
Grade.Items.FindByValue(s.Grade).Selected = true;

数据挖掘实验室

上面标红的部分代码可能会非常多,尤其是在字段比较多的时候,设定这些值就非常麻烦,不但不能漏掉,还要做类型转换等,稍有不小心就会出错.

现在我们可以利用反射的方法将这些属性自动与控件值关联起来,这样就不用手动地编写这些代码了.

具体内容请看我的示例工程文件.注意要在Web.Config中加上控件的引用路径. 数据挖掘研究院

这些代码参考了MSDN上面的内容,但是原来的代码有BUG,并且不支持可以多项选择的控件.我在这里加入了两个控件,分别是DarkangleCheckBoxList和DarkangleListBox 这两个控件支持将多项选择的属性以逗号分开的方式存储于数据库字段中.  比如,学生.课余活动 = "照象,游泳,足球",不需要写额外的代码即可直接绑定到多项选择控件. 数据挖掘研究院

目前支持的绑定控件有:Label,TextBox,CheckBox,DarkangleCheckBoxList,DarkangleListBox(如果您只需要单项选择,则可以使用自带的ListBox控件),ListBox,DropDownList, RadioButton,RadioButtonList,日历控件. 数据挖掘研究院

希望我写的东西对您的开发有所帮助. 数据挖掘实验室

Good luck~~~~;)

数据挖掘研究院

源代码与示例打包下载 数据挖掘研究院

应版主大人要求,把源码贴出来的,其实我提供的下载包里面是有源码的,就在类工程里面,随便也把原来那个老外的也帖上来,我修改了部分内容,原来那个代码运行起来也是有问题的。 数据挖掘实验室

先帖我的: 数据挖掘研究院

这两个是我护展出来的控件: 数据挖掘研究院

数据挖掘研究院

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();
        }
    }
}

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
                    {
                        objProperty.SetValue(obj, Convert.ChangeType(controlProperty.GetValue(control, null), objProperty.PropertyType), null);
                        return true; 数据挖掘实验室
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
            return false;
        }
    }
} 数据挖掘研究院

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
匿名?