关闭

【强烈推荐】开源C#工具类MSCL系列(二)控件赋值取值帮助类

495人阅读 评论(0) 收藏 举报
分类:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Data;
using System.Collections;

namespace MSCL
{
    /// <summary>
    /// 控件绑定赋值类
    /// </summary>
    public class ControlsHelper
    {
        #region 获取相应控件的值
        /// <summary>
        /// 获取控件值
        /// </summary>
        /// <param name="c">控件</param>
        /// <returns></returns>
        public static string GetControlValue(Control c)
        {
            string name = c.GetType().Name;
            string str2 = "";
            switch (name)
            {
                case "label":
                    {
                        Label lbl = (Label)c;
                        return lbl.Text;
                    }
                case "TextBox":
                    {
                        TextBox box = (TextBox)c;
                        return box.Text;
                    }
                case "CheckBox":
                    {
                        CheckBox box2 = (CheckBox)c;
                        return box2.Checked.ToString();
                    }
                case "CheckBoxList":
                    {
                        CheckBoxList list = (CheckBoxList)c;
                        foreach (ListItem item in list.Items)
                        {
                            if (item.Selected)
                            {
                                if (str2.Length > 0)
                                {
                                    str2 = str2 + ",";
                                }
                                str2 = str2 + item.Value;
                            }
                        }
                        return str2;
                    }
                case "DropDownList":
                    {
                        DropDownList list2 = (DropDownList)c;
                        foreach (ListItem item2 in list2.Items)
                        {
                            if (item2.Selected)
                            {
                                if (str2.Length > 0)
                                {
                                    str2 = str2 + ",";
                                }
                                str2 = str2 + item2.Value;
                            }
                        }
                        return str2;
                    }
                case "ListBox":
                    {
                        ListBox box3 = (ListBox)c;
                        foreach (ListItem item3 in box3.Items)
                        {
                            if (item3.Selected)
                            {
                                if (str2.Length > 0)
                                {
                                    str2 = str2 + ",";
                                }
                                str2 = str2 + item3.Value;
                            }
                        }
                        return str2;
                    }
                case "RadioButton":
                    {
                        RadioButton button = (RadioButton)c;
                        return button.Checked.ToString();
                    }
                case "RadioButtonList":
                    {
                        RadioButtonList list3 = (RadioButtonList)c;
                        foreach (ListItem item4 in list3.Items)
                        {
                            if (item4.Selected)
                            {
                                if (str2.Length > 0)
                                {
                                    str2 = str2 + ",";
                                }
                                str2 = str2 + item4.Value;
                            }
                        }
                        return str2;
                    }
                case "HtmlInputCheckBox":
                    {
                        HtmlInputCheckBox box4 = (HtmlInputCheckBox)c;
                        return box4.Checked.ToString();
                    }
                case "HtmlInputHidden":
                    {
                        HtmlInputHidden hidden = (HtmlInputHidden)c;
                        return hidden.Value;
                    }
                case "HtmlInputRadioButton":
                    {
                        HtmlInputRadioButton button2 = (HtmlInputRadioButton)c;
                        return button2.Checked.ToString();
                    }
                case "HtmlInputText":
                    {
                        HtmlInputText text = (HtmlInputText)c;
                        return text.Value;
                    }
                case "HtmlSelect":
                    {
                        HtmlSelect select = (HtmlSelect)c;
                        foreach (ListItem item5 in select.Items)
                        {
                            if (item5.Selected)
                            {
                                if (str2.Length > 0)
                                {
                                    str2 = str2 + ",";
                                }
                                str2 = str2 + item5.Value;
                            }
                        }
                        return str2;
                    }
                case "HtmlTextArea":
                    {
                        HtmlTextArea area = (HtmlTextArea)c;
                        return area.Value;
                    }
            }
            return str2;
        }
        #endregion

        #region 赋值给相应的控件
        /// <summary>
        /// 赋值给相应的控件
        /// </summary>
        /// <param name="c">控件</param>
        /// <param name="value">赋值</param>
        /// <param name="target">是否可编辑</param>
        public static void SetControlValue(Control c, string value, bool target)
        {
            switch (c.GetType().Name)
            {
                case "Label":
                    {
                        Label lbl = (Label)c;
                        lbl.Text = value;
                        lbl.Enabled = target ? true : false;
                        return;
                    }
                case "TextBox":
                    {
                        TextBox box = (TextBox)c;
                        box.Text = value;
                        box.Enabled = target ? true : false;
                        return;
                    }
                case "CheckBox":
                    {
                        CheckBox box2 = (CheckBox)c;
                        box2.Checked = value.ToLower() == "true";
                        box2.Enabled = target ? true : false;
                        return;
                    }
                case "CheckBoxList":
                    {
                        CheckBoxList list = (CheckBoxList)c;
                        foreach (ListItem item in list.Items)
                        {
                            if (value == item.Value)
                            {
                                item.Selected = true;
                            }
                            else
                            {
                                item.Selected = false;
                            }
                        }
                        list.Enabled = target ? true : false;
                        break;
                    }
                case "DropDownList":
                    {
                        DropDownList list2 = (DropDownList)c;
                        list2.SelectedValue = value;
                        list2.Enabled = target ? true : false;
                        return;
                    }
                case "ListBox":
                    {
                        ListBox box3 = (ListBox)c;
                        foreach (ListItem item2 in box3.Items)
                        {
                            if (value == item2.Value)
                            {
                                item2.Selected = true;
                            }
                            else
                            {
                                item2.Selected = false;
                            }
                        }
                        box3.Enabled = target ? true : false;
                        break;
                    }
                case "RadioButton":
                    {
                        RadioButton button = (RadioButton)c;
                        button.Checked = value.ToLower() == "true";
                        button.Enabled = target ? true : false;
                        return;
                    }
                case "RadioButtonList":
                    {
                        RadioButtonList list3 = (RadioButtonList)c;
                        foreach (ListItem item3 in list3.Items)
                        {
                            if (value == item3.Value)
                            {
                                item3.Selected = true;
                            }
                            else
                            {
                                item3.Selected = false;
                            }
                        }
                        list3.Enabled = target ? true : false;
                        break;
                    }
                case "HtmlInputCheckBox":
                    {
                        HtmlInputCheckBox box4 = (HtmlInputCheckBox)c;
                        box4.Checked = value.ToLower() == "true";
                        box4.Disabled = target ? true : false;
                        return;
                    }
                case "HtmlInputHidden":
                    {
                        HtmlInputHidden hidden = (HtmlInputHidden)c;
                        hidden.Value = value;
                        hidden.Disabled = target ? true : false;
                        return;
                    }
                case "HtmlInputRadioButton":
                    {
                        HtmlInputRadioButton button2 = (HtmlInputRadioButton)c;
                        button2.Checked = value.ToLower() == "true";
                        button2.Disabled = target ? true : false;
                        return;
                    }
                case "HtmlInputText":
                    {
                        HtmlInputText text = (HtmlInputText)c;
                        text.Value = value;
                        text.Disabled = target ? true : false;
                        return;
                    }
                case "HtmlSelect":
                    {
                        HtmlSelect select = (HtmlSelect)c;
                        foreach (ListItem item4 in select.Items)
                        {
                            if (value == item4.Value)
                            {
                                item4.Selected = true;
                            }
                            else
                            {
                                item4.Selected = false;
                            }
                        }
                        select.Disabled = target ? true : false;
                        break;
                    }
                case "HtmlTextArea":
                    {
                        HtmlTextArea area = (HtmlTextArea)c;
                        area.Value = value;
                        area.Disabled = target ? true : false;
                        break;
                    }
                default:
                    return;
            }
        }
        #endregion

        #region DropDownList无限递归显示层次关系
        /// <summary>
        /// 创建无限分级下拉列表框 
        /// </summary>
        /// <param name="ddlst">下拉控件</param>
        /// <param name="dt">源DataTable</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如parentid</param>
        public static void CreateLevelDropDown(DropDownList ddlst, DataTable dt, string text, string value, string parentid)
        {
            ArrayList allItems = new ArrayList();
            DataRow[] rows = dt.Select(parentid + "=0");
            foreach (DataRow row in rows)
                CreateLevelDropDownAssistant(dt, ref   allItems, row, string.Empty, text, value, parentid);
            ListItem[] items = new ListItem[allItems.Count];
            allItems.CopyTo(items);
            ddlst.Items.AddRange(items);
        }

        /// <summary>
        /// 递归绑定子节点
        /// </summary>
        /// <param name="dt">源DataTable</param>
        /// <param name="items">数组</param>
        /// <param name="parentRow">当前节点</param>
        /// <param name="curHeader">前缀</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如parentid</param>
        private static void CreateLevelDropDownAssistant(DataTable dt, ref   ArrayList items, DataRow parentRow, string curHeader, string text, string value, string parentid)
        {
            ListItem newItem = new ListItem(curHeader + Until.CutString(parentRow[text].ToString(), 18, true), parentRow[value].ToString());
            items.Add(newItem);
            DataRow[] rows = dt.Select(parentid + "=" + newItem.Value);
            for (int i = 0; i < rows.Length - 1; i++)
                CreateLevelDropDownAssistant(dt, ref   items, rows[i], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┣", text, value, parentid);

            if (rows.Length > 0)
                CreateLevelDropDownAssistant(dt, ref   items, rows[rows.Length - 1], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┗", text, value, parentid);
        }

        #endregion

        #region TreeView 无限递归显示层次关系目录树
        /// <summary>
        /// 创建无限分级目录树TreeView
        /// </summary>
        /// <param name="treeview">TreeView空间</param>
        /// <param name="dt">数据源DataTable</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如parentid</param>
        public static void CreateLevelTreeView(TreeView treeview, DataTable dt, string text, string value, string parentid)
        {
            DataView dv = dt.DefaultView;
            dv.RowFilter = parentid + "=0";
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                node.Text = drv[text].ToString();
                node.Value = drv[value].ToString();
                node.Expanded = false;
                treeview.Nodes.Add(node);
                CreatTreeViewChildNode(dv, node, text, value, parentid);
            }
        }

        /// <summary>
        /// 递归绑定子节点
        /// </summary>
        /// <param name="dv">源DataView</param>
        /// <param name="parentNode">当前节点</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如parentid</param>
        private static void CreatTreeViewChildNode(DataView dv, TreeNode parentNode, string text, string value, string parentid)
        {
            dv.RowFilter = parentid + "=" + parentNode.Value;
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = row[text].ToString();
                replyNode.Value = row[value].ToString();
                replyNode.Expanded = false;
                parentNode.ChildNodes.Add(replyNode);
                CreatTreeViewChildNode(dv, replyNode, text, value, parentid);
            }
        }

        /// <summary>
        /// 创建无限分级目录树TreeView
        /// </summary>
        /// <param name="treeview">TreeView空间</param>
        /// <param name="dt">数据源DataTable</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="url">url字段</param>
        /// <param name="parentid">深度字段 例如parentid</param>
        public static void CreateLevelTreeView(TreeView treeview, DataTable dt, string text, string value, string url, string parentid)
        {
            DataView dv = dt.DefaultView;
            dv.RowFilter = parentid + "=0";
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                node.Text = drv[text].ToString();
                node.Value = drv[value].ToString();
                node.NavigateUrl = drv[url].ToString();
                node.Expanded = false;
                treeview.Nodes.Add(node);
                CreatTreeViewChildNode(dv, node, text, value, url, parentid);
            }
        }

        /// <summary>
        /// 递归绑定子节点
        /// </summary>
        /// <param name="dv">源DataView</param>
        /// <param name="parentNode">当前节点</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="url">url字段</param>
        /// <param name="parentid">深度字段 例如parentid</param>
        private static void CreatTreeViewChildNode(DataView dv, TreeNode parentNode, string text, string value, string url, string parentid)
        {
            dv.RowFilter = parentid + "=" + parentNode.Value;
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = row[text].ToString();
                replyNode.Value = row[value].ToString();
                replyNode.NavigateUrl = row[url].ToString();
                replyNode.Expanded = false;
                parentNode.ChildNodes.Add(replyNode);
                CreatTreeViewChildNode(dv, replyNode, text, value, url, parentid);
            }
        }
        #endregion

        #region 创建无限分级ListBox
        /// <summary>
        /// 创建无限分级ListBox 
        /// </summary>
        /// <param name="ddlst">ListBox控件</param>
        /// <param name="dt">源DataTable</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如parentid</param>
        public static void CreateLevelListBox(ListBox ddlst, DataTable dt, string text, string value, string parentid)
        {
            ArrayList allItems = new ArrayList();
            DataRow[] rows = dt.Select(parentid + "=0");
            foreach (DataRow row in rows)
                CreateLevelListBoxAssistant(dt, ref   allItems, row, string.Empty, text, value, parentid);
            ListItem[] items = new ListItem[allItems.Count];
            allItems.CopyTo(items);
            ddlst.Items.AddRange(items);
        }

        /// <summary>
        /// 递归绑定子节点
        /// </summary>
        /// <param name="dt">源DataTable</param>
        /// <param name="items">数组</param>
        /// <param name="parentRow">当前节点</param>
        /// <param name="curHeader">前缀</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如parentid</param>
        private static void CreateLevelListBoxAssistant(DataTable dt, ref   ArrayList items, DataRow parentRow, string curHeader, string text, string value, string parentid)
        {
            ListItem newItem = new ListItem(curHeader + Until.CutString(parentRow[text].ToString(), 18, true), parentRow[value].ToString());
            items.Add(newItem);
            DataRow[] rows = dt.Select(parentid + "=" + newItem.Value);
            for (int i = 0; i < rows.Length - 1; i++)
                CreateLevelListBoxAssistant(dt, ref   items, rows[i], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┣", text, value, parentid);

            if (rows.Length > 0)
                CreateLevelListBoxAssistant(dt, ref   items, rows[rows.Length - 1], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┗", text, value, parentid);
        }
        #endregion

        #region 反射自动设置或者获取控件值 Model实体类
        /// <summary>
        /// 绑定对象属性 的值到同名控件 <see cref="Control"/>
        /// </summary>
        /// <param name="obj">属性要绑定窗体控件的对象</param>
        /// <param name="container">控件容器 通常是Page 页面  </param>
        /// <returns>赋值,实体类给控件赋值:BindObjectToControls(model, this);</returns>
        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>
        /// <returns>赋值,实体类给控件赋值:BindControlsToObject(model, this);</returns>
        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;
        }
        #endregion

        #region 数据源
        /// <summary>
        /// 获取或设置源,该源包含用于填充控件中的项的值列表。
        /// </summary>
        private static object dataSource;

        public static object DataSource
        {
            get
            {
                if (dataSource is DataSet)
                {
                    DataSet ds = dataSource as DataSet;
                    dataSource = ds.Tables[0];
                }
                else if (dataSource is DataTable)
                {
                    DataTable dt = dataSource as DataTable;
                    dataSource = dt;
                }
                else if (dataSource is DataView)
                {
                    DataView dv = dataSource as DataView;
                    dataSource = dv;
                }
                else if (dataSource is IDataReader)
                {
                    IDataReader dr = dataSource as IDataReader;
                    dataSource = dr;
                }
                return dataSource;
            }

            set
            {
                dataSource = value;
            }
        }

        #endregion

        #region WebUI绑定Control

        /// <summary>
        ///WebUI绑定Control
        /// </summary>
        /// <param name="control">定义由所有 ASP.NET 服务器控件共享的属性、方法和事件。</param>
        /// <param name="dataSource">获取或设置源,该源包含用于填充控件中的项的值列表。</param>
        public static void BindControl(System.Web.UI.Control control, object dataSource)
        {
            DataSource = dataSource;
            if (control is System.Web.UI.WebControls.DataList)
            {
                (control as System.Web.UI.WebControls.DataList).DataSource = DataSource;
                (control as System.Web.UI.WebControls.DataList).DataBind();
            }
            else if (control is System.Web.UI.WebControls.GridView)
            {
                (control as System.Web.UI.WebControls.GridView).DataSource = DataSource;
                (control as System.Web.UI.WebControls.GridView).DataBind();
            }
            else if (control is System.Web.UI.WebControls.Repeater)
            {
                (control as System.Web.UI.WebControls.Repeater).DataSource = DataSource;
                (control as System.Web.UI.WebControls.Repeater).DataBind();
            }
        }

        /// <summary>
        ///WebUI绑定Control
        /// </summary>
        /// <param name="control">定义由所有 ASP.NET 服务器控件共享的属性、方法和事件。</param>
        /// <param name="dataSource">获取或设置源,该源包含用于填充控件中的项的值列表。</param>
        /// <param name="dataTextField">获取或设置为列表项提供文本内容的数据源字段。</param>
        /// <param name="dataValueField">获取或设置为各列表项提供值的数据源字段。</param>
        public static void BindControl(System.Web.UI.Control control, object dataSource, string dataTextField, string dataValueField)
        {
            DataSource = dataSource;
            if (control is System.Web.UI.WebControls.CheckBoxList)
            {
                (control as System.Web.UI.WebControls.CheckBoxList).DataSource = DataSource;
                (control as System.Web.UI.WebControls.CheckBoxList).DataTextField = dataTextField;
                (control as System.Web.UI.WebControls.CheckBoxList).DataValueField = dataValueField;
                (control as System.Web.UI.WebControls.CheckBoxList).DataBind();
            }
            else if (control is System.Web.UI.WebControls.DropDownList)
            {
                (control as System.Web.UI.WebControls.DropDownList).DataSource = DataSource;
                (control as System.Web.UI.WebControls.DropDownList).DataTextField = dataTextField;
                (control as System.Web.UI.WebControls.DropDownList).DataValueField = dataValueField;
                (control as System.Web.UI.WebControls.DropDownList).DataBind();
            }
            else if (control is System.Web.UI.WebControls.RadioButtonList)
            {
                (control as System.Web.UI.WebControls.RadioButtonList).DataSource = DataSource;
                (control as System.Web.UI.WebControls.RadioButtonList).DataTextField = dataTextField;
                (control as System.Web.UI.WebControls.RadioButtonList).DataValueField = dataValueField;
                (control as System.Web.UI.WebControls.RadioButtonList).DataBind();
            }
            else if (control is System.Web.UI.WebControls.BulletedList)
            {
                (control as System.Web.UI.WebControls.BulletedList).DataSource = DataSource;
                (control as System.Web.UI.WebControls.BulletedList).DataTextField = dataTextField;
                (control as System.Web.UI.WebControls.BulletedList).DataValueField = dataValueField;
                (control as System.Web.UI.WebControls.BulletedList).DataBind();
            }
        }

        #endregion
    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1921692次
    • 积分:18561
    • 等级:
    • 排名:第509名
    • 原创:251篇
    • 转载:204篇
    • 译文:0篇
    • 评论:171条
    MSCL超级类库
    文章分类
    感谢打赏

    最新评论