CheckBoxList.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: CheckBoxList
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 80%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections.Specialized;
  15. using System.Globalization;
  16. using System.Web;
  17. using System.Web.UI;
  18. namespace System.Web.UI.WebControls
  19. {
  20. public class CheckBoxList: ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
  21. {
  22. CheckBox checkBoxRepeater;
  23. bool isChangeNotified;
  24. public CheckBoxList()
  25. {
  26. checkBoxRepeater = new CheckBox();
  27. checkBoxRepeater.ID = "0";
  28. checkBoxRepeater.EnableViewState = false;
  29. checkBoxRepeater.Controls.Add(this);
  30. isChangeNotified = false;
  31. }
  32. public virtual int CellPadding
  33. {
  34. get
  35. {
  36. return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellPadding : -1);
  37. }
  38. set
  39. {
  40. ((TableStyle)ControlStyle).CellPadding = value;
  41. }
  42. }
  43. public virtual int CellSpacing
  44. {
  45. get
  46. {
  47. return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellSpacing : -1);
  48. }
  49. set
  50. {
  51. ((TableStyle)ControlStyle).CellSpacing = value;
  52. }
  53. }
  54. public virtual int RepeatColumns
  55. {
  56. get
  57. {
  58. object o = ViewState["RepeatColumns"];
  59. if(o!=null)
  60. return (int)o;
  61. return 0;
  62. }
  63. set
  64. {
  65. if(value < 0)
  66. throw new ArgumentOutOfRangeException();
  67. ViewState["RepeatColumns"] = value;
  68. }
  69. }
  70. public virtual RepeatDirection RepeatDirection
  71. {
  72. get
  73. {
  74. object o = ViewState["RepeatDirection"];
  75. if(o!=null)
  76. return (RepeatDirection)o;
  77. return RepeatDirection.Vertical;
  78. }
  79. set
  80. {
  81. if(!System.Enum.IsDefined(typeof(RepeatDirection),value))
  82. throw new ArgumentException();
  83. ViewState["RepeatDirection"] = value;
  84. }
  85. }
  86. public virtual RepeatLayout RepeatLayout
  87. {
  88. get
  89. {
  90. object o = ViewState["RepeatLayout"];
  91. if(o!=null)
  92. return (RepeatLayout)o;
  93. return RepeatLayout.Table;
  94. }
  95. set
  96. {
  97. if(!System.Enum.IsDefined(typeof(RepeatLayout), value))
  98. throw new ArgumentException();
  99. ViewState["RepeatLayout"] = value;
  100. }
  101. }
  102. public virtual TextAlign TextAlign
  103. {
  104. get
  105. {
  106. object o = ViewState["TextAlign"];
  107. if(o!=null)
  108. return (TextAlign)o;
  109. return TextAlign.Right;
  110. }
  111. set
  112. {
  113. if(!Enum.IsDefined(typeof(TextAlign), value))
  114. throw new ArgumentException();
  115. ViewState["TextAlign"] = value;
  116. }
  117. }
  118. protected override Style CreateControlStyle()
  119. {
  120. // I have to return a TableStyle
  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. // register each selected with the ID
  137. checkBoxRepeater.ID = i.ToString(NumberFormatInfo.InvariantInfo);
  138. Page.RegisterRequiresPostBack(checkBoxRepeater);
  139. }
  140. }
  141. }
  142. }
  143. protected override void Render(HtmlTextWriter writer)
  144. {
  145. throw new NotImplementedException();
  146. }
  147. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  148. {
  149. int index = Int32.Parse(postDataKey.Substring(UniqueID.Length + 1));
  150. if(index >= 0 && index < Items.Count)
  151. {
  152. bool exists = (postCollection[postDataKey]!=null);
  153. if(Items[index].Selected != exists)
  154. {
  155. Items[index].Selected = exists;
  156. if(!isChangeNotified)
  157. {
  158. isChangeNotified = true;
  159. return true;
  160. }
  161. }
  162. }
  163. return false;
  164. }
  165. void IPostBackDataHandler.RaisePostDataChangedEvent()
  166. {
  167. throw new NotImplementedException();
  168. }
  169. bool IRepeatInfoUser.HasFooter
  170. {
  171. get
  172. {
  173. return false;
  174. }
  175. }
  176. bool IRepeatInfoUser.HasHeader
  177. {
  178. get
  179. {
  180. return false;
  181. }
  182. }
  183. bool IRepeatInfoUser.HasSeparators
  184. {
  185. get
  186. {
  187. return false;
  188. }
  189. }
  190. int IRepeatInfoUser.RepeatedItemCount
  191. {
  192. get
  193. {
  194. return Items.Count;
  195. }
  196. }
  197. // I don't need this
  198. Style IRepeatInfoUser.GetItemStyle(ListItemType itemType, int repeatIndex)
  199. {
  200. return null;
  201. }
  202. void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  203. {
  204. checkBoxRepeater.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);
  205. checkBoxRepeater.Text = Items[repeatIndex].Text;
  206. checkBoxRepeater.TextAlign = TextAlign;
  207. checkBoxRepeater.Checked = Items[repeatIndex].Selected;
  208. checkBoxRepeater.RenderControl(writer);
  209. }
  210. }
  211. }