CheckBoxList.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // System.Web.UI.WebControls.CheckBoxList.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections.Specialized;
  33. using System.ComponentModel;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. namespace System.Web.UI.WebControls
  38. {
  39. public class CheckBoxList: ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
  40. {
  41. CheckBox checkBoxRepeater;
  42. bool isChangeNotified;
  43. public CheckBoxList()
  44. {
  45. checkBoxRepeater = new CheckBox();
  46. checkBoxRepeater.ID = "0";
  47. checkBoxRepeater.EnableViewState = false;
  48. Controls.Add (checkBoxRepeater);
  49. isChangeNotified = false;
  50. }
  51. [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
  52. [WebSysDescription ("The border left within a CheckBox.")]
  53. public virtual int CellPadding
  54. {
  55. get
  56. {
  57. return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellPadding : -1);
  58. }
  59. set
  60. {
  61. ((TableStyle)ControlStyle).CellPadding = value;
  62. }
  63. }
  64. [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
  65. [WebSysDescription ("The border left between CheckBoxes.")]
  66. public virtual int CellSpacing
  67. {
  68. get
  69. {
  70. return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellSpacing : -1);
  71. }
  72. set
  73. {
  74. ((TableStyle)ControlStyle).CellSpacing = value;
  75. }
  76. }
  77. [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
  78. [WebSysDescription ("The number of columns that should be used to display the CheckBoxes.")]
  79. public virtual int RepeatColumns
  80. {
  81. get
  82. {
  83. object o = ViewState["RepeatColumns"];
  84. if(o!=null)
  85. return (int)o;
  86. return 0;
  87. }
  88. set
  89. {
  90. if(value < 0)
  91. throw new ArgumentOutOfRangeException();
  92. ViewState["RepeatColumns"] = value;
  93. }
  94. }
  95. [DefaultValue (typeof (RepeatDirection), "Vertical"), Bindable (true), WebCategory ("Layout")]
  96. [WebSysDescription ("The direction that is followed when doing the layout.")]
  97. public virtual RepeatDirection RepeatDirection
  98. {
  99. get
  100. {
  101. object o = ViewState["RepeatDirection"];
  102. if(o!=null)
  103. return (RepeatDirection)o;
  104. return RepeatDirection.Vertical;
  105. }
  106. set
  107. {
  108. if(!System.Enum.IsDefined(typeof(RepeatDirection),value))
  109. throw new ArgumentException();
  110. ViewState["RepeatDirection"] = value;
  111. }
  112. }
  113. [DefaultValue (typeof (RepeatLayout), "Table"), Bindable (true), WebCategory ("Layout")]
  114. [WebSysDescription ("The method used to create the layout.")]
  115. public virtual RepeatLayout RepeatLayout
  116. {
  117. get
  118. {
  119. object o = ViewState["RepeatLayout"];
  120. if(o!=null)
  121. return (RepeatLayout)o;
  122. return RepeatLayout.Table;
  123. }
  124. set
  125. {
  126. if(!System.Enum.IsDefined(typeof(RepeatLayout), value))
  127. throw new ArgumentException();
  128. ViewState["RepeatLayout"] = value;
  129. }
  130. }
  131. [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
  132. [WebSysDescription ("The alignment of the CheckBox text.")]
  133. public virtual TextAlign TextAlign
  134. {
  135. get
  136. {
  137. object o = ViewState["TextAlign"];
  138. if(o!=null)
  139. return (TextAlign)o;
  140. return TextAlign.Right;
  141. }
  142. set
  143. {
  144. if(!Enum.IsDefined(typeof(TextAlign), value))
  145. throw new ArgumentException();
  146. ViewState["TextAlign"] = value;
  147. }
  148. }
  149. protected override Style CreateControlStyle()
  150. {
  151. return new TableStyle(ViewState);
  152. }
  153. protected override Control FindControl(string id, int pathOffset)
  154. {
  155. return this;
  156. }
  157. protected override void OnPreRender(EventArgs e)
  158. {
  159. checkBoxRepeater.AutoPostBack = AutoPostBack;
  160. if(Page!=null)
  161. {
  162. for(int i=0; i < Items.Count; i++)
  163. {
  164. if(Items[i].Selected)
  165. {
  166. checkBoxRepeater.ID = i.ToString(NumberFormatInfo.InvariantInfo);
  167. Page.RegisterRequiresPostBack(checkBoxRepeater);
  168. }
  169. }
  170. }
  171. }
  172. protected override void Render(HtmlTextWriter writer)
  173. {
  174. RepeatInfo ri = new RepeatInfo();
  175. checkBoxRepeater.TabIndex = TabIndex;
  176. bool dirtyFlag = false;
  177. short tTabIndex = TabIndex;
  178. Style s = (ControlStyleCreated ? ControlStyle : null);
  179. if(TabIndex != 0)
  180. {
  181. if(!ViewState.IsItemDirty("TabIndex"))
  182. dirtyFlag = true;
  183. TabIndex = 0;
  184. }
  185. ri.RepeatColumns = RepeatColumns;
  186. ri.RepeatLayout = RepeatLayout;
  187. ri.RepeatDirection = RepeatDirection;
  188. ri.RenderRepeater(writer, this, s, this);
  189. if(tTabIndex != 0)
  190. {
  191. TabIndex = tTabIndex;
  192. }
  193. if(dirtyFlag)
  194. {
  195. ViewState.SetItemDirty("TabIndex", false);
  196. }
  197. }
  198. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  199. {
  200. if (!Enabled)
  201. return false;
  202. int index = Int32.Parse(postDataKey.Substring(UniqueID.Length + 1));
  203. if(index >= 0 && index < Items.Count)
  204. {
  205. string v = postCollection [postDataKey];
  206. bool exists = (v != null);
  207. if(Items[index].Selected != exists)
  208. {
  209. Items[index].Selected = exists;
  210. if(!isChangeNotified)
  211. {
  212. isChangeNotified = true;
  213. return true;
  214. }
  215. }
  216. }
  217. return false;
  218. }
  219. void IPostBackDataHandler.RaisePostDataChangedEvent()
  220. {
  221. OnSelectedIndexChanged(EventArgs.Empty);
  222. }
  223. bool IRepeatInfoUser.HasFooter
  224. {
  225. get
  226. {
  227. return false;
  228. }
  229. }
  230. bool IRepeatInfoUser.HasHeader
  231. {
  232. get
  233. {
  234. return false;
  235. }
  236. }
  237. bool IRepeatInfoUser.HasSeparators
  238. {
  239. get
  240. {
  241. return false;
  242. }
  243. }
  244. int IRepeatInfoUser.RepeatedItemCount
  245. {
  246. get
  247. {
  248. return Items.Count;
  249. }
  250. }
  251. Style IRepeatInfoUser.GetItemStyle(ListItemType itemType, int repeatIndex)
  252. {
  253. return null;
  254. }
  255. void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  256. {
  257. checkBoxRepeater.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);
  258. checkBoxRepeater.Text = Items[repeatIndex].Text;
  259. checkBoxRepeater.TextAlign = TextAlign;
  260. checkBoxRepeater.Checked = Items[repeatIndex].Selected;
  261. checkBoxRepeater.Enabled = Enabled;
  262. checkBoxRepeater.RenderControl(writer);
  263. }
  264. }
  265. }