2
0

CheckBoxList.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: CheckBoxList
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections.Specialized;
  15. using System.ComponentModel;
  16. using System.Globalization;
  17. using System.Web;
  18. using System.Web.UI;
  19. namespace System.Web.UI.WebControls
  20. {
  21. [DefaultEvent("SelectedIndexChanged")]
  22. [DefaultProperty("DataSource")]
  23. [ParseChildren(true, "Items")]
  24. [PersistChildren(false)]
  25. public class CheckBoxList: ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
  26. {
  27. CheckBox checkBoxRepeater;
  28. bool isChangeNotified;
  29. public CheckBoxList()
  30. {
  31. checkBoxRepeater = new CheckBox();
  32. checkBoxRepeater.ID = "0";
  33. checkBoxRepeater.EnableViewState = false;
  34. checkBoxRepeater.Controls.Add(this);
  35. isChangeNotified = false;
  36. }
  37. public virtual int CellPadding
  38. {
  39. get
  40. {
  41. return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellPadding : -1);
  42. }
  43. set
  44. {
  45. ((TableStyle)ControlStyle).CellPadding = value;
  46. }
  47. }
  48. public virtual int CellSpacing
  49. {
  50. get
  51. {
  52. return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellSpacing : -1);
  53. }
  54. set
  55. {
  56. ((TableStyle)ControlStyle).CellSpacing = value;
  57. }
  58. }
  59. public virtual int RepeatColumns
  60. {
  61. get
  62. {
  63. object o = ViewState["RepeatColumns"];
  64. if(o!=null)
  65. return (int)o;
  66. return 0;
  67. }
  68. set
  69. {
  70. if(value < 0)
  71. throw new ArgumentOutOfRangeException();
  72. ViewState["RepeatColumns"] = value;
  73. }
  74. }
  75. public virtual RepeatDirection RepeatDirection
  76. {
  77. get
  78. {
  79. object o = ViewState["RepeatDirection"];
  80. if(o!=null)
  81. return (RepeatDirection)o;
  82. return RepeatDirection.Vertical;
  83. }
  84. set
  85. {
  86. if(!System.Enum.IsDefined(typeof(RepeatDirection),value))
  87. throw new ArgumentException();
  88. ViewState["RepeatDirection"] = value;
  89. }
  90. }
  91. public virtual RepeatLayout RepeatLayout
  92. {
  93. get
  94. {
  95. object o = ViewState["RepeatLayout"];
  96. if(o!=null)
  97. return (RepeatLayout)o;
  98. return RepeatLayout.Table;
  99. }
  100. set
  101. {
  102. if(!System.Enum.IsDefined(typeof(RepeatLayout), value))
  103. throw new ArgumentException();
  104. ViewState["RepeatLayout"] = value;
  105. }
  106. }
  107. public virtual TextAlign TextAlign
  108. {
  109. get
  110. {
  111. object o = ViewState["TextAlign"];
  112. if(o!=null)
  113. return (TextAlign)o;
  114. return TextAlign.Right;
  115. }
  116. set
  117. {
  118. if(!Enum.IsDefined(typeof(TextAlign), value))
  119. throw new ArgumentException();
  120. ViewState["TextAlign"] = value;
  121. }
  122. }
  123. protected override Style CreateControlStyle()
  124. {
  125. return new TableStyle(ViewState);
  126. }
  127. protected override Control FindControl(string id, int pathOffset)
  128. {
  129. return this;
  130. }
  131. protected override void OnPreRender(EventArgs e)
  132. {
  133. checkBoxRepeater.AutoPostBack = AutoPostBack;
  134. if(Page!=null)
  135. {
  136. for(int i=0; i < Items.Count; i++)
  137. {
  138. if(Items[i].Selected)
  139. {
  140. checkBoxRepeater.ID = i.ToString(NumberFormatInfo.InvariantInfo);
  141. Page.RegisterRequiresPostBack(checkBoxRepeater);
  142. }
  143. }
  144. }
  145. }
  146. protected override void Render(HtmlTextWriter writer)
  147. {
  148. RepeatInfo ri = new RepeatInfo();
  149. checkBoxRepeater.TabIndex = TabIndex;
  150. bool dirtyFlag = false;
  151. short tTabIndex = TabIndex;
  152. Style s = (ControlStyleCreated ? ControlStyle : null);
  153. if(TabIndex > 0)
  154. {
  155. if(!ViewState.IsItemDirty("TabIndex"))
  156. dirtyFlag = true;
  157. TabIndex = 0;
  158. }
  159. ri.RepeatColumns = RepeatColumns;
  160. ri.RepeatLayout = RepeatLayout;
  161. ri.RepeatDirection = RepeatDirection;
  162. ri.RenderRepeater(writer, this, s, this);
  163. if(tTabIndex > 0)
  164. {
  165. TabIndex = tTabIndex;
  166. }
  167. if(dirtyFlag)
  168. {
  169. ViewState.SetItemDirty("TabIndex", false);
  170. }
  171. }
  172. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  173. {
  174. int index = Int32.Parse(postDataKey.Substring(UniqueID.Length + 1));
  175. if(index >= 0 && index < Items.Count)
  176. {
  177. bool exists = (postCollection[postDataKey]!=null);
  178. if(Items[index].Selected != exists)
  179. {
  180. Items[index].Selected = exists;
  181. if(!isChangeNotified)
  182. {
  183. isChangeNotified = true;
  184. return true;
  185. }
  186. }
  187. }
  188. return false;
  189. }
  190. void IPostBackDataHandler.RaisePostDataChangedEvent()
  191. {
  192. OnSelectedIndexChanged(EventArgs.Empty);
  193. }
  194. bool IRepeatInfoUser.HasFooter
  195. {
  196. get
  197. {
  198. return false;
  199. }
  200. }
  201. bool IRepeatInfoUser.HasHeader
  202. {
  203. get
  204. {
  205. return false;
  206. }
  207. }
  208. bool IRepeatInfoUser.HasSeparators
  209. {
  210. get
  211. {
  212. return false;
  213. }
  214. }
  215. int IRepeatInfoUser.RepeatedItemCount
  216. {
  217. get
  218. {
  219. return Items.Count;
  220. }
  221. }
  222. Style IRepeatInfoUser.GetItemStyle(ListItemType itemType, int repeatIndex)
  223. {
  224. return null;
  225. }
  226. void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  227. {
  228. checkBoxRepeater.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);
  229. checkBoxRepeater.Text = Items[repeatIndex].Text;
  230. checkBoxRepeater.TextAlign = TextAlign;
  231. checkBoxRepeater.Checked = Items[repeatIndex].Selected;
  232. checkBoxRepeater.RenderControl(writer);
  233. }
  234. }
  235. }