CheckBoxList.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // System.Web.UI.WebControls.CheckBoxList.cs
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2005 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;
  29. using System.Globalization;
  30. using System.Collections.Specialized;
  31. using System.ComponentModel;
  32. namespace System.Web.UI.WebControls {
  33. public class CheckBoxList : ListControl, IRepeatInfoUser,
  34. INamingContainer, IPostBackDataHandler {
  35. private CheckBox check_box;
  36. public CheckBoxList ()
  37. {
  38. check_box = new CheckBox ();
  39. Controls.Add (check_box);
  40. }
  41. #if ONLY_1_1
  42. [Bindable(true)]
  43. #endif
  44. [DefaultValue(-1)]
  45. [WebSysDescription ("")]
  46. [WebCategory ("Layout")]
  47. public virtual int CellPadding {
  48. get { return TableStyle.CellPadding; }
  49. set { TableStyle.CellPadding = value; }
  50. }
  51. #if ONLY_1_1
  52. [Bindable(true)]
  53. #endif
  54. [DefaultValue(-1)]
  55. [WebSysDescription ("")]
  56. [WebCategory ("Layout")]
  57. public virtual int CellSpacing {
  58. get { return TableStyle.CellSpacing; }
  59. set { TableStyle.CellSpacing = value; }
  60. }
  61. #if ONLY_1_1
  62. [Bindable(true)]
  63. #endif
  64. [DefaultValue(0)]
  65. [WebSysDescription ("")]
  66. [WebCategory ("Layout")]
  67. public virtual int RepeatColumns {
  68. get { return ViewState.GetInt ("RepeatColumns", 0); }
  69. set {
  70. if (value < 0)
  71. throw new ArgumentOutOfRangeException ("value");
  72. ViewState ["RepeatColumns"] = value;
  73. }
  74. }
  75. #if ONLY_1_1
  76. [Bindable(true)]
  77. #endif
  78. [DefaultValue(RepeatDirection.Vertical)]
  79. [WebSysDescription ("")]
  80. [WebCategory ("Layout")]
  81. public virtual RepeatDirection RepeatDirection {
  82. get {
  83. return (RepeatDirection) ViewState.GetInt ("RepeatDirection",
  84. (int) RepeatDirection.Vertical);
  85. }
  86. set {
  87. if (value < RepeatDirection.Horizontal ||
  88. value > RepeatDirection.Vertical)
  89. throw new ArgumentOutOfRangeException ("value");
  90. ViewState ["RepeatDirection"] = value;
  91. }
  92. }
  93. #if ONLY_1_1
  94. [Bindable(true)]
  95. #endif
  96. [DefaultValue(RepeatLayout.Table)]
  97. [WebSysDescription ("")]
  98. [WebCategory ("Layout")]
  99. public virtual RepeatLayout RepeatLayout {
  100. get {
  101. return (RepeatLayout) ViewState.GetInt ("RepeatLayout",
  102. (int) RepeatLayout.Table);
  103. }
  104. set {
  105. if (value < RepeatLayout.Table ||
  106. value > RepeatLayout.Flow)
  107. throw new ArgumentOutOfRangeException ("value");
  108. ViewState ["RepeatLayout"] = value;
  109. }
  110. }
  111. #if ONLY_1_1
  112. [Bindable(true)]
  113. #endif
  114. [DefaultValue(TextAlign.Right)]
  115. [WebSysDescription ("")]
  116. [WebCategory ("Appearance")]
  117. public virtual TextAlign TextAlign {
  118. get {
  119. return (TextAlign) ViewState.GetInt ("TextAlign",
  120. (int) TextAlign.Right);
  121. }
  122. set {
  123. if (value < TextAlign.Left || value > TextAlign.Right)
  124. throw new ArgumentOutOfRangeException ("value");
  125. ViewState ["TextAlign"] = value;
  126. }
  127. }
  128. #if NET_2_0
  129. [MonoTODO]
  130. protected virtual bool HasFooter
  131. {
  132. get {
  133. throw new NotImplementedException ();
  134. }
  135. }
  136. [MonoTODO]
  137. protected virtual bool HasHeader
  138. {
  139. get {
  140. throw new NotImplementedException ();
  141. }
  142. }
  143. [MonoTODO]
  144. protected virtual bool HasSeparators
  145. {
  146. get {
  147. throw new NotImplementedException ();
  148. }
  149. }
  150. [MonoTODO]
  151. protected virtual int RepeatedItemCount
  152. {
  153. get {
  154. throw new NotImplementedException ();
  155. }
  156. }
  157. #endif
  158. private TableStyle TableStyle {
  159. get { return (TableStyle) ControlStyle; }
  160. }
  161. protected override Style CreateControlStyle ()
  162. {
  163. return new TableStyle (ViewState);
  164. }
  165. protected override Control FindControl (string id, int pathOffset)
  166. {
  167. // Always, or in just all my tests?
  168. return this;
  169. }
  170. #if NET_2_0
  171. [MonoTODO]
  172. protected virtual Style GetItemStyle (ListItemType itemType,
  173. int repeatIndex)
  174. {
  175. throw new NotImplementedException ();
  176. }
  177. #endif
  178. #if NET_2_0
  179. protected internal
  180. #else
  181. protected
  182. #endif
  183. override void OnPreRender (EventArgs e)
  184. {
  185. base.OnPreRender (e);
  186. // Register all of the checked controls so we can
  187. // find out when they are unchecked.
  188. for (int i = 0; i < Items.Count; i++) {
  189. if (Items [i].Selected) {
  190. check_box.ID = i.ToString (CultureInfo.InvariantCulture);
  191. Page.RegisterRequiresPostBack (check_box);
  192. }
  193. }
  194. }
  195. #if NET_2_0
  196. protected internal
  197. #else
  198. protected
  199. #endif
  200. override void Render (HtmlTextWriter writer)
  201. {
  202. #if NET_2_0
  203. if (Items.Count == 0)
  204. return;
  205. #endif
  206. RepeatInfo ri = new RepeatInfo ();
  207. ri.RepeatColumns = RepeatColumns;
  208. ri.RepeatDirection = RepeatDirection;
  209. ri.RepeatLayout = RepeatLayout;
  210. short ti = 0;
  211. if (TabIndex != 0) {
  212. check_box.TabIndex = TabIndex;
  213. ti = TabIndex;
  214. TabIndex = 0;
  215. }
  216. ri.RenderRepeater (writer, this, TableStyle, this);
  217. if (ti != 0)
  218. TabIndex = ti;
  219. }
  220. #if NET_2_0
  221. [MonoTODO]
  222. protected virtual void RenderItem (ListItemType itemType,
  223. int repeatIndex,
  224. RepeatInfo repeatInfo,
  225. HtmlTextWriter w)
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. [MonoTODO]
  230. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  231. {
  232. throw new NotImplementedException ();
  233. }
  234. [MonoTODO]
  235. protected virtual void RaisePostDataChangedEvent ()
  236. {
  237. throw new NotImplementedException ();
  238. }
  239. #endif
  240. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  241. NameValueCollection postCollection)
  242. {
  243. int checkbox = -1;
  244. try {
  245. string id = postDataKey.Substring (ClientID.Length + 1);
  246. if (Char.IsDigit (id [0]))
  247. checkbox = Int32.Parse (id, CultureInfo.InvariantCulture);
  248. } catch {
  249. return false;
  250. }
  251. if (checkbox == -1)
  252. return false;
  253. string val = postCollection [postDataKey];
  254. bool ischecked = val == "on";
  255. ListItem item = Items [checkbox];
  256. if (item.Selected != ischecked) {
  257. item.Selected = ischecked;
  258. return true;
  259. }
  260. return false;
  261. }
  262. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  263. {
  264. OnSelectedIndexChanged (EventArgs.Empty);
  265. }
  266. bool IRepeatInfoUser.HasFooter {
  267. get { return false; }
  268. }
  269. bool IRepeatInfoUser.HasHeader {
  270. get { return false; }
  271. }
  272. bool IRepeatInfoUser.HasSeparators {
  273. get { return false; }
  274. }
  275. int IRepeatInfoUser.RepeatedItemCount {
  276. get { return Items.Count; }
  277. }
  278. Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,
  279. int repeatIndex)
  280. {
  281. return null;
  282. }
  283. void IRepeatInfoUser.RenderItem (ListItemType itemType,
  284. int repeatIndex, RepeatInfo repeatInfo,
  285. HtmlTextWriter writer)
  286. {
  287. ListItem item = Items [repeatIndex];
  288. check_box.ID = repeatIndex.ToString (CultureInfo.InvariantCulture);
  289. check_box.Text = item.Text;
  290. check_box.AutoPostBack = AutoPostBack;
  291. check_box.Checked = item.Selected;
  292. check_box.RenderControl (writer);
  293. }
  294. }
  295. }