Advertisement
originalmoose

EnhancedCheckboxList.cs

Nov 6th, 2014
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Collections;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8.  
  9. namespace Testing
  10. {
  11.     public class EnhancedCheckboxList : System.Web.UI.WebControls.CheckBoxList
  12.     {
  13.         public string DataEnabledField
  14.         {
  15.             get
  16.             {
  17.                 string s = (string)ViewState["DataEnabledField"];
  18.                 return (s == null) ? String.Empty : s;
  19.             }
  20.             set
  21.             {
  22.                 ViewState["DataEnabledField"] = value;
  23.                 if (Initialized)
  24.                     OnDataPropertyChanged();
  25.             }
  26.         }
  27.  
  28.         public string DataCheckedField
  29.         {
  30.             get
  31.             {
  32.                 string s = (string)ViewState["DataCheckedField"];
  33.                 return (s == null) ? String.Empty : s;
  34.             }
  35.             set
  36.             {
  37.                 ViewState["DataCheckedField"] = value;
  38.                 if (Initialized)
  39.                     OnDataPropertyChanged();
  40.             }
  41.         }
  42.  
  43.         protected override void PerformDataBinding(IEnumerable dataSource)
  44.         {
  45.             if (dataSource != null)
  46.             {
  47.                 bool hasSelectedField = DataCheckedField.Length != 0;
  48.                 bool hasEnabledField = DataEnabledField.Length != 0;
  49.  
  50.                 if (!this.AppendDataBoundItems)
  51.                     this.Items.Clear();
  52.  
  53.                 if (dataSource is ICollection)
  54.                     this.Items.Capacity = (dataSource as ICollection).Count + this.Items.Count;
  55.  
  56.                 foreach (object dataItem in dataSource)
  57.                 {
  58.                     ListItem item = new ListItem()
  59.                     {
  60.                         Text = DataBinder.GetPropertyValue(dataItem, DataTextField).ToString(),
  61.                         Value = DataBinder.GetPropertyValue(dataItem, DataValueField).ToString(),
  62.                         Selected = hasSelectedField ? (bool)DataBinder.GetPropertyValue(dataItem, DataCheckedField) : false,
  63.                         Enabled = hasEnabledField ? (bool)DataBinder.GetPropertyValue(dataItem, DataEnabledField) : true
  64.                     };
  65.                     this.Items.Add(item);
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement