• 控件绑定枚举类型的扩张方法


    // Generated by Reflector from E:\CodeServer\Dll\Cfsns.CommonTools.dll
    namespace Cfsns.Common.Tools.WebControlExtension
    {
        using Cfsns.Common.Tools;
        using Cfsns.Common.Tools.EnumExtension;
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Runtime.CompilerServices;
        using System.Runtime.InteropServices;
        using System.Text;
        using System.Text.RegularExpressions;
        using System.Web.UI.WebControls;
       
        public static class WebControlExtension
        {
            public static void BindSelected(this CheckBoxList cb, string selectedString, string seperator = @"\|")
            {
                string[] strArray = Regex.Split(selectedString, seperator);
                for (int i = 0; i < cb.Items.Count; i++)
                {
                    for (int j = 0; j < strArray.Length; j++)
                    {
                        if (cb.Items[i].Value == strArray[j])
                        {
                            cb.Items[i].Selected = true;
                        }
                    }
                }
            }
           
            public static void DataBindP(this ListControl control, string firstLineName = "请选择...", string value = "")
            {
                control.DataBind();
                if (!string.IsNullOrEmpty(firstLineName))
                {
                    control.Items.Insert(0, new ListItem(firstLineName, value));
                }
            }
           
            public static void DataBindP(this ListControl control, IndexInfo toBindInfo, string firstLineName = null, string firstLineValue = "", string dataTextFormatString = null)
            {
                control.DataSource = toBindInfo;
                control.DataTextField = "Infos";
                control.DataValueField = "Name";
                control.DataTextFormatString = dataTextFormatString;
                control.DataBind();
                if (!string.IsNullOrEmpty(firstLineName))
                {
                    control.Items.Insert(0, new ListItem(firstLineName, firstLineValue));
                }
            }
           
            public static void DataBindP(this Repeater control, Type type, string firstLineName = null, string firstLineValue = "", DataBindOptions option = 0)
            {
                Func<KeyValuePair<string, string>, string> keySelector = null;
                Dictionary<string, string> first = new Dictionary<string, string>();
                if (!string.IsNullOrEmpty(firstLineName))
                {
                    first[firstLineValue] = firstLineName;
                }
                IOrderedEnumerable<KeyValuePair<string, string>> second = from p in type.GetEnumDescriptions()
                    orderby ((int) Enum.Parse(type, p.Key)).ToString()
                    select p;
                if (option == DataBindOptions.Text)
                {
                    control.DataSource = first.Concat<KeyValuePair<string, string>>(second);
                }
                else if (option == DataBindOptions.Value)
                {
                    if (keySelector == null)
                    {
                        keySelector = p => ((int) Enum.Parse(type, p.Key)).ToString();
                    }
                    control.DataSource = first.Concat<KeyValuePair<string, string>>(second).ToDictionary<KeyValuePair<string, string>, string, string>(keySelector, p => p.Value);
                }
                else
                {
                    control.DataSource = first.Concat<KeyValuePair<string, string>>(second).ToDictionary<KeyValuePair<string, string>, string, string>(p => p.Value, p => p.Value);
                }
                control.DataBind();
            }
           
            public static void DataBindP(this ListControl control, Type type, string firstLineName = null, string firstLineValue = "", string dataTextFormatString = null, DataBindOptions option = 0)
            {
                Func<KeyValuePair<string, string>, string> keySelector = null;
                Func<KeyValuePair<string, string>, string> func2 = null;
                Func<KeyValuePair<string, string>, string> func3 = null;
                control.DataTextField = "Value";
                control.DataValueField = "Key";
                control.DataTextFormatString = dataTextFormatString;
                if (option == DataBindOptions.Text)
                {
                    if (keySelector == null)
                    {
                        keySelector = p => ((int) Enum.Parse(type, p.Key)).ToString();
                    }
                    control.DataSource = type.GetEnumDescriptions().OrderBy<KeyValuePair<string, string>, string>(keySelector);
                }
                else if (option == DataBindOptions.Value)
                {
                    if (func2 == null)
                    {
                        func2 = p => ((int) Enum.Parse(type, p.Key)).ToString();
                    }
                    control.DataSource = from p in type.GetEnumDescriptions().ToDictionary<KeyValuePair<string, string>, string, string>(func2, p => p.Value)
                        orderby p.Key
                        select p;
                }
                else
                {
                    if (func3 == null)
                    {
                        func3 = p => ((int) Enum.Parse(type, p.Key)).ToString();
                    }
                    control.DataSource = type.GetEnumDescriptions().OrderBy<KeyValuePair<string, string>, string>(func3).ToDictionary<KeyValuePair<string, string>, string, string>(p => p.Value, p => p.Value);
                }
                control.DataBind();
                if (!string.IsNullOrEmpty(firstLineName))
                {
                    control.Items.Insert(0, new ListItem(firstLineName, firstLineValue));
                }
            }
           
            public static string SelectedTexts(this CheckBoxList checkboxList, string seperator = ",")
            {
                StringBuilder builder = new StringBuilder();
                foreach (ListItem item in checkboxList.Items)
                {
                    if (item.Selected)
                    {
                        builder.Append(item.Text);
                        builder.Append(",");
                    }
                }
                int length = builder.Length;
                return ((length > 0) ? builder.Remove(length - 1, 1).ToString() : string.Empty);
            }
           
            public static T SelectedValues<T>(this CheckBoxList checkBoxList, T defaultValue = null) where T: struct
            {
                object obj2 = 0;
                for (int i = 0; i < checkBoxList.Items.Count; i++)
                {
                    if (checkBoxList.Items[i].Selected)
                    {
                        obj2 = ((int) obj2) | Convert.ToInt32(checkBoxList.Items[i].Value.ToEnum<T>(defaultValue));
                    }
                }
                if (!obj2.Equals(0))
                {
                    return (T) obj2;
                }
                return defaultValue;
            }
           
            public static string SelectedValues(this CheckBoxList checkboxList, string seperator = "|")
            {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < checkboxList.Items.Count; i++)
                {
                    if (checkboxList.Items[i].Selected)
                    {
                        builder.Append(checkboxList.Items[i].Value + seperator);
                    }
                }
                if (builder.Length > 0)
                {
                    builder.Remove(builder.Length - seperator.Length, seperator.Length);
                }
                return builder.ToString();
            }
           
            public enum DataBindOptions
            {
                Text,
                Value,
                Description
            }
        }
    }

  • 相关阅读:
    编译型与解释型、动态语言与静态语言、强类型语言与弱类型语言的区别
    【转】 Oracle 用户权限管理方法
    oracle实例内存(SGA和PGA)调整
    oracle修改内存使用和性能调节,SGA
    下星期计划——9.7~9.13
    医疗数据分析——过高费用的异常检测
    Groovy介绍
    Java 7代码层面上的更新
    Java陷阱之assert关键字
    支持向量机(SVM)、支持向量回归(SVR)
  • 原文地址:https://www.cnblogs.com/Minghao_HU/p/2699590.html
Copyright © 2020-2023  润新知