CheckBoxList.cs 8.4 KB

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