• CheckBoxList 获取与设置选中的值


    /// <summary>
    ///CheckBoxListHelper 的摘要说明
    ///CheckBoxList获取与设置选中的值
    /// </summary>
    public class CheckBoxListHelper
    {
    	private CheckBoxListHelper()
    	{
    		//
    		//TODO: 在此处添加构造函数逻辑
    		//
    	}
        /// <summary>
        /// 值的分割符
        /// </summary>
        private const string SEPARATOR=",";
        /// <summary>
        /// 获取CheckBoxList被选中的值
        /// </summary>
        /// <param name="cblist"></param>
        /// <returns></returns>
        public static string GetCheckBoxListCheckValue(CheckBoxList cbList)
        {
            if (cbList == null) return "";
    
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
    
            foreach (ListItem item in cbList.Items)
            {
    
                if (item.Selected)
                {
                    builder.AppendFormat("{0}{1}", item.Value,SEPARATOR);
                }
            }
            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }
            return builder.ToString();
        }
        /// <summary>
        /// 设置CheckBoxList选中的值
        /// </summary>
        /// <param name="cbList"></param>
        /// <returns></returns>
        public static void SetCheckBoxListCheck(CheckBoxList cbList,string values)
        {
            //当没有选择值时,取消所有选择项
            if (string.IsNullOrEmpty(values)) values=SEPARATOR;
    
            if (cbList == null) return;
            //例如1,2,3 变为 1,2,3,
            values = values + SEPARATOR;
            foreach (ListItem item in cbList.Items)
            {
                item.Selected = false;//取消被选择
                string value = item.Value;
                if (values.IndexOf(value + SEPARATOR) > -1)
                {
                    item.Selected = true;
                }
            }
    
        }
    }
    

      

  • 相关阅读:
    springmvc
    POJ 3683 Priest John's Busiest Day
    POJ 3678 Katu Puzzle
    HDU 1815 Building roads
    CDOJ UESTC 1220 The Battle of Guandu
    HDU 3715 Go Deeper
    HDU 3622 Bomb Game
    POJ 3207 Ikki's Story IV
    POJ 3648 Wedding
    HDU 1814 Peaceful Commission
  • 原文地址:https://www.cnblogs.com/ljx2012/p/3658772.html
Copyright © 2020-2023  润新知