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

  • 相关阅读:
    Akka(2):Actor生命周期管理
    Akka(1):Actor
    Akka(0):聊聊对Akka的初步了解和想法
    FunDA:一个开源的函数式数据处理工具库,也是Slick的补充
    FunDA(17)- 示范:异常处理与事后处理
    FunDA(16)- 示范:整合并行运算
    FunDA(15)- 示范:任务并行运算
    FunDA(14)- 示范:并行运算,并行数据库读取
    FunDA(13)- 示范:用户自定义操作函数
    thinkjs中自定义sql语句
  • 原文地址:https://www.cnblogs.com/Minghao_HU/p/2699590.html
Copyright © 2011-2022 走看看