2
0

RadioButtonList.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: RadioButtonList
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 95%
  10. *
  11. * (C) Gaurav Vaish (2002)
  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. [ValidationProperty("SelectedItem")]
  26. public class RadioButtonList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
  27. {
  28. private bool selectionIndexChanged;
  29. private short tabIndex;
  30. public RadioButtonList(): base()
  31. {
  32. selectionIndexChanged = false;
  33. }
  34. public virtual int CellPadding
  35. {
  36. get
  37. {
  38. if(ControlStyleCreated)
  39. {
  40. return (int)(((TableStyle)ControlStyle).CellPadding);
  41. }
  42. return -1;
  43. }
  44. set
  45. {
  46. ((TableStyle)ControlStyle).CellPadding = value;
  47. }
  48. }
  49. public virtual int CellSpacing
  50. {
  51. get
  52. {
  53. if(ControlStyleCreated)
  54. {
  55. return (int)(((TableStyle)ControlStyle).CellSpacing);
  56. }
  57. return -1;
  58. }
  59. set
  60. {
  61. ((TableStyle)ControlStyle).CellSpacing = value;
  62. }
  63. }
  64. public virtual int RepeatColumns
  65. {
  66. get
  67. {
  68. object o = ViewState["RepeatColumns"];
  69. if(o != null)
  70. return (int)o;
  71. return 0;
  72. }
  73. set
  74. {
  75. if(value < 0)
  76. throw new ArgumentOutOfRangeException("value");
  77. ViewState["RepeatColumns"] = value;
  78. }
  79. }
  80. public virtual RepeatDirection RepeatDirection
  81. {
  82. get
  83. {
  84. object o = ViewState["RepeatDirection"];
  85. if(o != null)
  86. return (RepeatDirection)o;
  87. return RepeatDirection.Vertical;
  88. }
  89. set
  90. {
  91. if(!Enum.IsDefined(typeof(RepeatDirection), value))
  92. throw new ArgumentException();
  93. ViewState["RepeatDirection"] = value;
  94. }
  95. }
  96. public virtual RepeatLayout RepeatLayout
  97. {
  98. get
  99. {
  100. object o = ViewState["RepeatLayout"];
  101. if(o != null)
  102. return (RepeatLayout)o;
  103. return RepeatLayout.Table;
  104. }
  105. set
  106. {
  107. if(!Enum.IsDefined(typeof(RepeatLayout), value))
  108. throw new ArgumentException();
  109. ViewState["RepeatLayout"] = value;
  110. }
  111. }
  112. public virtual TextAlign TextAlign
  113. {
  114. get
  115. {
  116. object o = ViewState["TextAlign"];
  117. if(o != null)
  118. return (TextAlign)o;
  119. return TextAlign.Right;
  120. }
  121. set
  122. {
  123. if(!Enum.IsDefined(typeof(TextAlign), value))
  124. throw new ArgumentException();
  125. ViewState["TextAlign"] = value;
  126. }
  127. }
  128. protected override Style CreateControlStyle()
  129. {
  130. return new TableStyle(ViewState);
  131. }
  132. protected override void Render(HtmlTextWriter writer)
  133. {
  134. RepeatInfo info = new RepeatInfo();
  135. Style cStyle = (ControlStyleCreated ? ControlStyle : null);
  136. bool dirty = false;
  137. tabIndex = TabIndex;
  138. if(tabIndex != 0)
  139. {
  140. dirty = !ViewState.IsItemDirty("TabIndex");
  141. TabIndex = 0;
  142. }
  143. info.RepeatColumns = RepeatColumns;
  144. info.RepeatDirection = RepeatDirection;
  145. info.RenderRepeater(writer, this, cStyle, this);
  146. if(tabIndex != 0)
  147. {
  148. TabIndex = tabIndex;
  149. }
  150. if(dirty)
  151. {
  152. ViewState.SetItemDirty("TabIndex", false);
  153. }
  154. }
  155. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  156. {
  157. string value = postCollection[postDataKey];
  158. for(int i=0; i < Items.Count; i++)
  159. {
  160. if(Items[i].Value == value)
  161. {
  162. if(i != SelectedIndex)
  163. {
  164. SelectedIndex = i;
  165. }
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. void IPostBackDataHandler.RaisePostDataChangedEvent()
  172. {
  173. if(selectionIndexChanged)
  174. OnSelectedIndexChanged(EventArgs.Empty);
  175. }
  176. Style IRepeatInfoUser.GetItemStyle(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex)
  177. {
  178. return null;
  179. }
  180. void IRepeatInfoUser.RenderItem (System.Web.UI.WebControls.ListItemType itemType,
  181. int repeatIndex,
  182. RepeatInfo repeatInfo,
  183. HtmlTextWriter writer)
  184. {
  185. /* Create a new RadioButton as if it was defined in the page and render it */
  186. RadioButton button = new RadioButton ();
  187. button.GroupName = UniqueID;
  188. button.TextAlign = TextAlign;
  189. button.AutoPostBack = AutoPostBack;
  190. button.ID = ClientID + "_" + repeatIndex.ToString (NumberFormatInfo.InvariantInfo);;
  191. object view_state = ViewState ["TabIndex"];
  192. if (view_state != null)
  193. button.TabIndex = (short) view_state;
  194. ListItem current = Items [repeatIndex];
  195. button.Text = current.Text;
  196. button.Attributes ["value"] = current.Value;
  197. button.Checked = current.Selected;
  198. button.RenderControl (writer);
  199. }
  200. bool IRepeatInfoUser.HasFooter
  201. {
  202. get
  203. {
  204. return false;
  205. }
  206. }
  207. bool IRepeatInfoUser.HasHeader
  208. {
  209. get
  210. {
  211. return false;
  212. }
  213. }
  214. bool IRepeatInfoUser.HasSeparators
  215. {
  216. get
  217. {
  218. return false;
  219. }
  220. }
  221. int IRepeatInfoUser.RepeatedItemCount
  222. {
  223. get
  224. {
  225. return Items.Count;
  226. }
  227. }
  228. }
  229. }