RadioButtonList.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // System.Web.UI.WebControls.RadioButtonList.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections.Specialized;
  33. using System.ComponentModel;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. namespace System.Web.UI.WebControls
  38. {
  39. [ValidationProperty("SelectedItem")]
  40. public class RadioButtonList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
  41. {
  42. private bool selectionIndexChanged;
  43. private short tabIndex;
  44. public RadioButtonList(): base()
  45. {
  46. selectionIndexChanged = false;
  47. }
  48. [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
  49. [WebSysDescription ("The border left within a RadioButton.")]
  50. public virtual int CellPadding
  51. {
  52. get
  53. {
  54. if(ControlStyleCreated)
  55. {
  56. return (int)(((TableStyle)ControlStyle).CellPadding);
  57. }
  58. return -1;
  59. }
  60. set {
  61. if (value < -1)
  62. throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");
  63. ((TableStyle)ControlStyle).CellPadding = value;
  64. }
  65. }
  66. [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
  67. [WebSysDescription ("The border left between RadioButtons.")]
  68. public virtual int CellSpacing
  69. {
  70. get
  71. {
  72. if(ControlStyleCreated)
  73. {
  74. return (int)(((TableStyle)ControlStyle).CellSpacing);
  75. }
  76. return -1;
  77. }
  78. set {
  79. if (value < -1)
  80. throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");
  81. ((TableStyle)ControlStyle).CellSpacing = value;
  82. }
  83. }
  84. [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
  85. [WebSysDescription ("The number of columns that should be used to display the RadioButtons.")]
  86. public virtual int RepeatColumns
  87. {
  88. get
  89. {
  90. object o = ViewState["RepeatColumns"];
  91. if(o != null)
  92. return (int)o;
  93. return 0;
  94. }
  95. set {
  96. if (value < 0)
  97. throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");
  98. ViewState["RepeatColumns"] = value;
  99. }
  100. }
  101. [DefaultValue (typeof (RepeatDirection), "Vertical"), Bindable (true), WebCategory ("Layout")]
  102. [WebSysDescription ("The direction that is followed when doing the layout.")]
  103. public virtual RepeatDirection RepeatDirection
  104. {
  105. get
  106. {
  107. object o = ViewState["RepeatDirection"];
  108. if(o != null)
  109. return (RepeatDirection)o;
  110. return RepeatDirection.Vertical;
  111. }
  112. set
  113. {
  114. if(!Enum.IsDefined(typeof(RepeatDirection), value))
  115. throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
  116. ViewState["RepeatDirection"] = value;
  117. }
  118. }
  119. [DefaultValue (typeof (RepeatLayout), "Table"), Bindable (true), WebCategory ("Layout")]
  120. [WebSysDescription ("The method used to create the layout.")]
  121. public virtual RepeatLayout RepeatLayout
  122. {
  123. get
  124. {
  125. object o = ViewState["RepeatLayout"];
  126. if(o != null)
  127. return (RepeatLayout)o;
  128. return RepeatLayout.Table;
  129. }
  130. set
  131. {
  132. if(!Enum.IsDefined(typeof(RepeatLayout), value))
  133. throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
  134. ViewState["RepeatLayout"] = value;
  135. }
  136. }
  137. [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
  138. [WebSysDescription ("The alignment of the RadioButton text.")]
  139. public virtual TextAlign TextAlign
  140. {
  141. get
  142. {
  143. object o = ViewState["TextAlign"];
  144. if(o != null)
  145. return (TextAlign)o;
  146. return TextAlign.Right;
  147. }
  148. set
  149. {
  150. if(!Enum.IsDefined(typeof(TextAlign), value))
  151. throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
  152. ViewState["TextAlign"] = value;
  153. }
  154. }
  155. protected override Style CreateControlStyle()
  156. {
  157. return new TableStyle(ViewState);
  158. }
  159. protected override void Render(HtmlTextWriter writer)
  160. {
  161. RepeatInfo info = new RepeatInfo();
  162. Style cStyle = (ControlStyleCreated ? ControlStyle : null);
  163. bool dirty = false;
  164. tabIndex = TabIndex;
  165. if(tabIndex != 0)
  166. {
  167. dirty = !ViewState.IsItemDirty("TabIndex");
  168. TabIndex = 0;
  169. }
  170. info.RepeatColumns = RepeatColumns;
  171. info.RepeatDirection = RepeatDirection;
  172. info.RepeatLayout = RepeatLayout;
  173. info.RenderRepeater(writer, this, cStyle, this);
  174. if(tabIndex != 0)
  175. {
  176. TabIndex = tabIndex;
  177. }
  178. if(dirty)
  179. {
  180. ViewState.SetItemDirty("TabIndex", false);
  181. }
  182. }
  183. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  184. {
  185. string value = postCollection [postDataKey];
  186. int c = Items.Count;
  187. for (int i = 0; i < c; i++) {
  188. if (Items [i].Value != value)
  189. continue;
  190. if (i != SelectedIndex) {
  191. SelectedIndex = i;
  192. selectionIndexChanged = true;
  193. }
  194. return true;
  195. }
  196. return false;
  197. }
  198. void IPostBackDataHandler.RaisePostDataChangedEvent()
  199. {
  200. if(selectionIndexChanged)
  201. OnSelectedIndexChanged(EventArgs.Empty);
  202. }
  203. Style IRepeatInfoUser.GetItemStyle(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex)
  204. {
  205. return null;
  206. }
  207. void IRepeatInfoUser.RenderItem (System.Web.UI.WebControls.ListItemType itemType,
  208. int repeatIndex,
  209. RepeatInfo repeatInfo,
  210. HtmlTextWriter writer)
  211. {
  212. /* Create a new RadioButton as if it was defined in the page and render it */
  213. RadioButton button = new RadioButton ();
  214. button.Page = Page;
  215. button.GroupName = UniqueID;
  216. button.TextAlign = TextAlign;
  217. button.AutoPostBack = AutoPostBack;
  218. button.ID = ClientID + "_" + repeatIndex.ToString (NumberFormatInfo.InvariantInfo);;
  219. button.TabIndex = tabIndex;
  220. ListItem current = Items [repeatIndex];
  221. button.Text = current.Text;
  222. button.Attributes ["value"] = current.Value;
  223. button.Checked = current.Selected;
  224. button.Enabled = Enabled;
  225. button.RenderControl (writer);
  226. }
  227. bool IRepeatInfoUser.HasFooter
  228. {
  229. get
  230. {
  231. return false;
  232. }
  233. }
  234. bool IRepeatInfoUser.HasHeader
  235. {
  236. get
  237. {
  238. return false;
  239. }
  240. }
  241. bool IRepeatInfoUser.HasSeparators
  242. {
  243. get
  244. {
  245. return false;
  246. }
  247. }
  248. int IRepeatInfoUser.RepeatedItemCount
  249. {
  250. get
  251. {
  252. return Items.Count;
  253. }
  254. }
  255. }
  256. }