RadioButtonList.cs 5.1 KB

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