CheckBoxList.cs 5.3 KB

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