码迷,mamicode.com
首页 > 其他好文 > 详细

设置或者得到CheckBoxList选中了的值

时间:2014-09-21 19:22:01      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   使用   ar   for   sp   art   

    #region

        /// <summary>

        /// 初始化CheckBoxList中哪些是选中了的        

        /// </summary>

        /// <param name="checkList">CheckBoxList</param>

        /// <param name="selval">选中了的值串例如:"0,1,1,2,1"</param>

        /// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param>

        public string SetChecked(CheckBoxList checkList,string selval,string separator)

        {

            selval = separator + selval + separator;        //例如:"0,1,1,2,1"->",0,1,1,2,1,"

            for(int i=0; i<checkList.Items.Count; i++)

            {

                checkList.Items[i].Selected = false;

                string val = separator + checkList.Items[i].Value + separator;

                if(selval.IndexOf(val)!=-1)

                {

                    checkList.Items[i].Selected = true;

                    selval = selval.Replace(val,separator);        //然后从原来的值串中删除已经选中了的

                    if(selval == separator)        //selval的最后一项也被选中的话,此时经过Replace后,只会剩下一个分隔符

                    {       

                        selval += separator;        //添加一个分隔符

                    }

                }

            }

            selval = selval.Substring(1,selval.Length-2);        //除去前后加的分割符号

            return selval;

        }

        ///// <summary>

        /// 得到CheckBoxList中选中了的值

        /// </summary>

        /// <param name="checkList">CheckBoxList</param>

        /// <param name="separator">分割符号</param>

        /// <returns>01,02,03</returns>

        public string GetChecked(CheckBoxList checkList, string separator)

        {

            string selval = "";

            for (int i = 0; i < checkList.Items.Count; i++)

            {

                if (checkList.Items[i].Selected)

                {

                    selval += checkList.Items[i].Value + separator;

                }

            }

            if (selval.Length > 1)

            {

                selval = selval.Substring(0, selval.Length - 1);

            }

            return selval;

        }

        #endregion

 

#region 设置或者得到CheckBoxList选中了的值

bubuko.com,布布扣bubuko.com,布布扣        /**//// <summary>

bubuko.com,布布扣        /// 初始化CheckBoxList中哪些是选中了的 bubuko.com,布布扣        /// </summary>

bubuko.com,布布扣        /// <param name="checkList">CheckBoxList</param>

bubuko.com,布布扣        /// <param name="selval">选中了的值串例如:"0,1,1,2,1"</param>

bubuko.com,布布扣        /// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param>

bubuko.com,布布扣        public static string SetChecked(CheckBoxList checkList,string selval,string separator)

bubuko.com,布布扣bubuko.com,布布扣        ...{

bubuko.com,布布扣            selval = separator + selval + separator;        //例如:"0,1,1,2,1"->",0,1,1,2,1,"

bubuko.com,布布扣            for(int i=0; i<checkList.Items.Count; i++)

bubuko.com,布布扣bubuko.com,布布扣            ...{

bubuko.com,布布扣                checkList.Items[i].Selected = false;

bubuko.com,布布扣                string val = separator + checkList.Items[i].Value + separator;

bubuko.com,布布扣                if(selval.IndexOf(val)!=-1)

bubuko.com,布布扣bubuko.com,布布扣                ...{

bubuko.com,布布扣                    checkList.Items[i].Selected = true;

bubuko.com,布布扣                    selval = selval.Replace(val,separator);        //然后从原来的值串中删除已经选中了的

bubuko.com,布布扣                    if(selval == separator)        //selval的最后一项也被选中的话,此时经过Replace后,只会剩下一个分隔符

bubuko.com,布布扣bubuko.com,布布扣                    ...{       

bubuko.com,布布扣                        selval += separator;        //添加一个分隔符

bubuko.com,布布扣                    }

bubuko.com,布布扣                }

bubuko.com,布布扣            }

bubuko.com,布布扣            selval = selval.Substring(1,selval.Length-2);        //除去前后加的分割符号

bubuko.com,布布扣            return selval;

bubuko.com,布布扣        }

bubuko.com,布布扣

bubuko.com,布布扣bubuko.com,布布扣        /**//// <summary>

bubuko.com,布布扣        /// 得到CheckBoxList中选中了的值

bubuko.com,布布扣        /// </summary>

bubuko.com,布布扣        /// <param name="checkList">CheckBoxList</param>

bubuko.com,布布扣        /// <param name="separator">分割符号</param>

bubuko.com,布布扣        /// <returns></returns>

bubuko.com,布布扣        public static string GetChecked(CheckBoxList checkList, string separator)

bubuko.com,布布扣bubuko.com,布布扣        ...{

bubuko.com,布布扣            string selval = "";

bubuko.com,布布扣            for(int i=0;i<checkList.Items.Count;i++)

bubuko.com,布布扣bubuko.com,布布扣            ...{

bubuko.com,布布扣                if(checkList.Items[i].Selected)

                ...{

bubuko.com,布布扣                    selval += checkList.Items[i].Value + separator;

bubuko.com,布布扣                }

bubuko.com,布布扣            }

bubuko.com,布布扣            return selval;

bubuko.com,布布扣        }

bubuko.com,布布扣        #endregion

轉自http://caigeno1.blog.163.com/blog/static/761981822008423101935303/

设置或者得到CheckBoxList选中了的值

标签:style   blog   http   io   使用   ar   for   sp   art   

原文地址:http://www.cnblogs.com/lh123/p/3984710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!