CheckBoxList.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // System.Web.UI.WebControls.CheckBoxList.cs
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2005-2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Globalization;
  29. using System.Collections.Specialized;
  30. using System.ComponentModel;
  31. using System.Security.Permissions;
  32. using System.Web.Util;
  33. namespace System.Web.UI.WebControls
  34. {
  35. // CAS
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. public class CheckBoxList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
  39. {
  40. CheckBox check_box;
  41. public CheckBoxList ()
  42. {
  43. check_box = new CheckBox ();
  44. Controls.Add (check_box);
  45. }
  46. [DefaultValue(-1)]
  47. [WebSysDescription ("")]
  48. [WebCategory ("Layout")]
  49. public virtual int CellPadding {
  50. get { return TableStyle.CellPadding; }
  51. set { TableStyle.CellPadding = value; }
  52. }
  53. [DefaultValue(-1)]
  54. [WebSysDescription ("")]
  55. [WebCategory ("Layout")]
  56. public virtual int CellSpacing {
  57. get { return TableStyle.CellSpacing; }
  58. set { TableStyle.CellSpacing = value; }
  59. }
  60. [DefaultValue(0)]
  61. [WebSysDescription ("")]
  62. [WebCategory ("Layout")]
  63. public virtual int RepeatColumns {
  64. get { return ViewState.GetInt ("RepeatColumns", 0); }
  65. set {
  66. if (value < 0)
  67. throw new ArgumentOutOfRangeException ("value");
  68. ViewState ["RepeatColumns"] = value;
  69. }
  70. }
  71. [DefaultValue(RepeatDirection.Vertical)]
  72. [WebSysDescription ("")]
  73. [WebCategory ("Layout")]
  74. public virtual RepeatDirection RepeatDirection {
  75. get { return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical); }
  76. set {
  77. if (value < RepeatDirection.Horizontal ||
  78. value > RepeatDirection.Vertical)
  79. throw new ArgumentOutOfRangeException ("value");
  80. ViewState ["RepeatDirection"] = value;
  81. }
  82. }
  83. [DefaultValue(RepeatLayout.Table)]
  84. [WebSysDescription ("")]
  85. [WebCategory ("Layout")]
  86. public virtual RepeatLayout RepeatLayout {
  87. get { return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table); }
  88. set {
  89. if (value < RepeatLayout.Table ||
  90. value > RepeatLayout.Flow)
  91. throw new ArgumentOutOfRangeException ("value");
  92. ViewState ["RepeatLayout"] = value;
  93. }
  94. }
  95. [DefaultValue(TextAlign.Right)]
  96. [WebSysDescription ("")]
  97. [WebCategory ("Appearance")]
  98. public virtual TextAlign TextAlign {
  99. get { return (TextAlign) ViewState.GetInt ("TextAlign", (int) TextAlign.Right); }
  100. set {
  101. if (value < TextAlign.Left || value > TextAlign.Right)
  102. throw new ArgumentOutOfRangeException ("value");
  103. ViewState ["TextAlign"] = value;
  104. }
  105. }
  106. TableStyle TableStyle {
  107. get { return (TableStyle) ControlStyle; }
  108. }
  109. protected override Style CreateControlStyle ()
  110. {
  111. return new TableStyle (ViewState);
  112. }
  113. protected override Control FindControl (string id, int pathOffset)
  114. {
  115. // Always, or in just all my tests?
  116. return this;
  117. }
  118. protected internal override void OnPreRender (EventArgs e)
  119. {
  120. base.OnPreRender (e);
  121. // Register all of the checked controls so we can
  122. // find out when they are unchecked.
  123. Page page = Page;
  124. for (int i = 0; i < Items.Count; i++) {
  125. if (Items [i].Selected) {
  126. check_box.ID = i.ToString (Helpers.InvariantCulture);
  127. if (page != null)
  128. page.RegisterRequiresPostBack (check_box);
  129. }
  130. }
  131. }
  132. protected internal override void Render (HtmlTextWriter writer)
  133. {
  134. if (Items.Count == 0)
  135. return;
  136. RepeatInfo ri = new RepeatInfo ();
  137. ri.RepeatColumns = RepeatColumns;
  138. ri.RepeatDirection = RepeatDirection;
  139. ri.RepeatLayout = RepeatLayout;
  140. short ti = 0;
  141. if (TabIndex != 0) {
  142. check_box.TabIndex = TabIndex;
  143. ti = TabIndex;
  144. TabIndex = 0;
  145. }
  146. string ak = AccessKey;
  147. check_box.AccessKey = ak;
  148. this.AccessKey = null;
  149. ri.RenderRepeater (writer, this, TableStyle, this);
  150. if (ti != 0)
  151. TabIndex = ti;
  152. this.AccessKey = ak;
  153. }
  154. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  155. {
  156. if (!IsEnabled)
  157. return false;
  158. EnsureDataBound ();
  159. int checkbox = -1;
  160. try {
  161. string id = postDataKey.Substring (ClientID.Length + 1);
  162. if (Char.IsDigit (id [0]))
  163. checkbox = Int32.Parse (id, Helpers.InvariantCulture);
  164. } catch {
  165. return false;
  166. }
  167. if (checkbox == -1)
  168. return false;
  169. string val = postCollection [postDataKey];
  170. bool ischecked = val == "on";
  171. ListItem item = Items [checkbox];
  172. if (item.Enabled) {
  173. if (ischecked && !item.Selected) {
  174. item.Selected = true;
  175. return true;
  176. } else if (!ischecked && item.Selected) {
  177. item.Selected = false;
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. protected virtual void RaisePostDataChangedEvent ()
  184. {
  185. if (CausesValidation) {
  186. Page page = Page;
  187. if (page != null)
  188. page.Validate (ValidationGroup);
  189. }
  190. OnSelectedIndexChanged (EventArgs.Empty);
  191. }
  192. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  193. {
  194. return LoadPostData (postDataKey, postCollection);
  195. }
  196. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  197. {
  198. RaisePostDataChangedEvent ();
  199. }
  200. protected virtual bool HasFooter {
  201. get { return false; }
  202. }
  203. bool IRepeatInfoUser.HasFooter {
  204. get { return HasFooter; }
  205. }
  206. protected virtual bool HasHeader {
  207. get { return false; }
  208. }
  209. bool IRepeatInfoUser.HasHeader {
  210. get { return HasHeader; }
  211. }
  212. protected virtual bool HasSeparators {
  213. get { return false; }
  214. }
  215. bool IRepeatInfoUser.HasSeparators {
  216. get { return HasSeparators; }
  217. }
  218. protected virtual int RepeatedItemCount {
  219. get { return Items.Count; }
  220. }
  221. int IRepeatInfoUser.RepeatedItemCount {
  222. get { return RepeatedItemCount; }
  223. }
  224. protected virtual Style GetItemStyle (ListItemType itemType, int repeatIndex)
  225. {
  226. return null;
  227. }
  228. Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
  229. {
  230. return GetItemStyle (itemType, repeatIndex);
  231. }
  232. protected virtual void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  233. {
  234. ListItem item = Items [repeatIndex];
  235. string cssClass = check_box.CssClass;
  236. if (!String.IsNullOrEmpty (cssClass))
  237. check_box.CssClass = String.Empty;
  238. check_box.ID = repeatIndex.ToString (Helpers.InvariantCulture);
  239. check_box.Text = item.Text;
  240. check_box.AutoPostBack = AutoPostBack;
  241. check_box.Checked = item.Selected;
  242. check_box.TextAlign = TextAlign;
  243. if (!IsEnabled)
  244. check_box.Enabled = false;
  245. else
  246. check_box.Enabled = item.Enabled;
  247. check_box.ValidationGroup = ValidationGroup;
  248. check_box.CausesValidation = CausesValidation;
  249. if (check_box.HasAttributes)
  250. check_box.Attributes.Clear ();
  251. if (item.HasAttributes)
  252. check_box.Attributes.CopyFrom (item.Attributes);
  253. #if NET_4_0
  254. if (!RenderingCompatibilityLessThan40) {
  255. var attrs = check_box.InputAttributes;
  256. attrs.Clear ();
  257. attrs.Add ("value", item.Value);
  258. }
  259. #endif
  260. check_box.RenderControl (writer);
  261. }
  262. void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  263. {
  264. RenderItem (itemType, repeatIndex, repeatInfo, writer);
  265. }
  266. protected internal override void VerifyMultiSelect()
  267. {
  268. //by default the ListControl will throw an exception in this method,
  269. //therefor we should override the method if the class is supporting
  270. //MultiSelect option.
  271. }
  272. }
  273. }