zoukankan      html  css  js  c++  java
  • 控件绑定枚举类型的扩张方法

    // 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
            }
        }
    }

  • 相关阅读:
    PID204特种部队
    概率论,简要数学期望(转载)
    codevs1258 关路灯(☆区间dp)
    codevs3327选择数字(单调队列优化)
    codevs3002石子归并3(四边形不等式优化dp)
    codevs1154能量项链(环形dp,区间dp)
    codevs3304水果姐逛街(线段数)
    codevs3162抄书问题(划分型dp)
    浅谈一类积性函数的前缀和(转载)
    组合数取模(转载)
  • 原文地址:https://www.cnblogs.com/Minghao_HU/p/2699590.html
Copyright © 2011-2022 走看看