首页 > 其他 > 详细

CheckBoxList 获取与设置选中的值

时间:2014-04-11 23:38:59      阅读:722      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// <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;
            }
        }
 
    }
}

  

CheckBoxList 获取与设置选中的值,布布扣,bubuko.com

CheckBoxList 获取与设置选中的值

原文:http://www.cnblogs.com/ljx2012/p/3658772.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!