CheckBoxList.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.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. return new TableStyle(ViewState);
  121. }
  122. protected override Control FindControl(string id, int pathOffset)
  123. {
  124. return this;
  125. }
  126. protected override void OnPreRender(EventArgs e)
  127. {
  128. checkBoxRepeater.AutoPostBack = AutoPostBack;
  129. if(Page!=null)
  130. {
  131. for(int i=0; i < Items.Count; i++)
  132. {
  133. if(Items[i].Selected)
  134. {
  135. checkBoxRepeater.ID = i.ToString(NumberFormatInfo.InvariantInfo);
  136. Page.RegisterRequiresPostBack(checkBoxRepeater);
  137. }
  138. }
  139. }
  140. }
  141. protected override void Render(HtmlTextWriter writer)
  142. {
  143. RepeatInfo ri = new RepeatInfo();
  144. checkBoxRepeater.TabIndex = TabIndex;
  145. bool dirtyFlag = false;
  146. short tTabIndex = TabIndex;
  147. Style s = (ControlStyleCreated ? ControlStyle : null);
  148. if(TabIndex > 0)
  149. {
  150. if(!ViewState.IsItemDirty("TabIndex"))
  151. dirtyFlag = true;
  152. TabIndex = 0;
  153. }
  154. ri.RepeatColumns = RepeatColumns;
  155. ri.RepeatLayout = RepeatLayout;
  156. ri.RepeatDirection = RepeatDirection;
  157. ri.RenderRepeater(writer, this, s, this);
  158. if(tTabIndex > 0)
  159. {
  160. TabIndex = tTabIndex;
  161. }
  162. if(dirtyFlag)
  163. {
  164. ViewState.SetItemDirty("TabIndex", false);
  165. }
  166. }
  167. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  168. {
  169. int index = Int32.Parse(postDataKey.Substring(UniqueID.Length + 1));
  170. if(index >= 0 && index < Items.Count)
  171. {
  172. bool exists = (postCollection[postDataKey]!=null);
  173. if(Items[index].Selected != exists)
  174. {
  175. Items[index].Selected = exists;
  176. if(!isChangeNotified)
  177. {
  178. isChangeNotified = true;
  179. return true;
  180. }
  181. }
  182. }
  183. return false;
  184. }
  185. void IPostBackDataHandler.RaisePostDataChangedEvent()
  186. {
  187. OnSelectedIndexChanged(EventArgs.Empty);
  188. }
  189. bool IRepeatInfoUser.HasFooter
  190. {
  191. get
  192. {
  193. return false;
  194. }
  195. }
  196. bool IRepeatInfoUser.HasHeader
  197. {
  198. get
  199. {
  200. return false;
  201. }
  202. }
  203. bool IRepeatInfoUser.HasSeparators
  204. {
  205. get
  206. {
  207. return false;
  208. }
  209. }
  210. int IRepeatInfoUser.RepeatedItemCount
  211. {
  212. get
  213. {
  214. return Items.Count;
  215. }
  216. }
  217. Style IRepeatInfoUser.GetItemStyle(ListItemType itemType, int repeatIndex)
  218. {
  219. return null;
  220. }
  221. void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  222. {
  223. checkBoxRepeater.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);
  224. checkBoxRepeater.Text = Items[repeatIndex].Text;
  225. checkBoxRepeater.TextAlign = TextAlign;
  226. checkBoxRepeater.Checked = Items[repeatIndex].Selected;
  227. checkBoxRepeater.RenderControl(writer);
  228. }
  229. }
  230. }