RadioButtonList.cs 5.0 KB

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