标准的CheckBoxList控件只能获取最后一个选择项,很多时候处理起来很麻烦,每次都要自己写代码获取选中项的值,今天写了一个控件,继承了CheckBoxList,添加了几个属性:SelectedValues、SelectedTexts、SelectedItems。
  1. /// <summary>
  2.     /// MyCheckBoxList,获取多个选择项
  3.     /// </summary>
  4.     [ToolboxData("<{0}:MyCheckBoxList runat=server></{0}:MyCheckBoxList>")]
  5.     public class MyCheckBoxList : CheckBoxList
  6.     {
  7.         /// <summary>
  8.         /// 获取或设置选中项的值,以半角逗号分隔
  9.         /// </summary>
  10.         [Browsable(true)]
  11.         [Bindable(true)]
  12.         [Description("获取或设置选中项的值,以半角逗号分隔")]
  13.         public string SelectedValues
  14.         {
  15.             get
  16.             {
  17.                 string ValueStr = string.Empty;
  18.                 for (int k = 0; k <= this.Items.Count - 1; k++)
  19.                 {
  20.                     if (this.Items[k].Selected)
  21.                     {
  22.                         ValueStr += this.Items[k].Value + ",";
  23.                     }
  24.                 }
  25.                 if (ValueStr != string.Empty)
  26.                 {
  27.                     ValueStr = ValueStr.Substring(0, ValueStr.Length - 1);
  28.                 }
  29.                 return ValueStr;
  30.             }
  31.             set
  32.             {
  33.                 if (value != string.Empty)
  34.                 {
  35.                     string[] values = value.Split(',');
  36.                     for (int k = 0; k <= this.Items.Count - 1; k++)
  37.                     {
  38.                         for (int j = 0; j <= values.Length - 1; j++)
  39.                         {
  40.                             if (values[j] == this.Items[k].Value)
  41.                             {
  42.                                 this.Items[k].Selected = true;
  43.                             }
  44.                         }
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         /// <summary>
  50.         /// 获取或设置选中项的文本,以半角逗号分隔
  51.         /// </summary>
  52.         [Browsable(true)]
  53.         [Bindable(true)]
  54.         [Description("获取或设置选中项的文本,以半角逗号分隔")]
  55.         public string SelectedTexts
  56.         {
  57.             get
  58.             {
  59.                 string TextStr = string.Empty;
  60.                 for (int k = 0; k <= this.Items.Count - 1; k++)
  61.                 {
  62.                     if (this.Items[k].Selected)
  63.                     {
  64.                         TextStr += this.Items[k].Text + ",";
  65.                     }
  66.                 }
  67.                 if (TextStr != string.Empty)
  68.                 {
  69.                     TextStr = TextStr.Substring(0, TextStr.Length - 1);
  70.                 }
  71.                 return TextStr;
  72.             }
  73.             set
  74.             {
  75.                 if (value != string.Empty)
  76.                 {
  77.                     string[] values = value.Split(',');
  78.                     for (int k = 0; k <= this.Items.Count - 1; k++)
  79.                     {
  80.                         for (int j = 0; j <= values.Length - 1; j++)
  81.                         {
  82.                             if (values[j] == this.Items[k].Text)
  83.                             {
  84.                                 this.Items[k].Selected = true;
  85.                             }
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.         /// <summary>
  92.         /// 获取或设置选中项,返回ListItemCollection
  93.         /// </summary>
  94.         [Browsable(true)]
  95.         [Bindable(true)]
  96.         [Description("获取或设置选中项,返回ListItemCollection")]
  97.         public ListItemCollection SelectedItems
  98.         {
  99.             get
  100.             {
  101.                 ListItemCollection Items = new ListItemCollection();
  102.                 for (int k = 0; k <= this.Items.Count - 1; k++)
  103.                 {
  104.                     if (this.Items[k].Selected)
  105.                     {
  106.                         Items.Add(this.Items[k]);
  107.                     }
  108.                 }
  109.                 return Items;
  110.             }
  111.             set
  112.             {
  113.                 ListItemCollection Items = (ListItemCollection)value;
  114.                 for (int k = 0; k <= this.Items.Count - 1; k++)
  115.                 {
  116.                     for (int i = 0; i <= Items.Count - 1;i++ )
  117.                     {
  118.                         if (this.Items[k].Equals(Items[i]))
  119.                         {
  120.                             this.Items[k].Selected = true;
  121.                         }
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.     }