RadioButtonList.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // System.Web.UI.WebControls.RadioButtonList.cs
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernandez ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. //
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using System.Collections.Specialized;
  34. using System.Security.Permissions;
  35. namespace System.Web.UI.WebControls {
  36. // CAS
  37. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. // attributes
  40. [ValidationProperty ("SelectedItem")]
  41. #if NET_2_0
  42. [SupportsEventValidation]
  43. #endif
  44. public class RadioButtonList : ListControl, IRepeatInfoUser,
  45. INamingContainer, IPostBackDataHandler {
  46. bool need_raise;
  47. short tabIndex = 0;
  48. public RadioButtonList ()
  49. {
  50. }
  51. #if ONLY_1_1
  52. [Bindable (true)]
  53. #endif
  54. [DefaultValue (-1)]
  55. [WebSysDescription ("")]
  56. [WebCategory ("Layout")]
  57. public virtual int CellPadding {
  58. get {
  59. if (ControlStyleCreated == false)
  60. return -1; // default value
  61. return ((TableStyle) ControlStyle).CellPadding;
  62. }
  63. set {
  64. ((TableStyle) ControlStyle).CellPadding = value;
  65. }
  66. }
  67. #if ONLY_1_1
  68. [Bindable (true)]
  69. #endif
  70. [DefaultValue (-1)]
  71. [WebSysDescription ("")]
  72. [WebCategory ("Layout")]
  73. public virtual int CellSpacing {
  74. get {
  75. if (ControlStyleCreated == false)
  76. return -1; // default value
  77. return ((TableStyle) ControlStyle).CellSpacing;
  78. }
  79. set {
  80. ((TableStyle) ControlStyle).CellSpacing = value;
  81. }
  82. }
  83. #if ONLY_1_1
  84. [Bindable (true)]
  85. #endif
  86. [DefaultValue (0)]
  87. [WebSysDescription ("")]
  88. [WebCategory ("Layout")]
  89. public virtual int RepeatColumns {
  90. get {
  91. return ViewState.GetInt ("RepeatColumns", 0);
  92. }
  93. set {
  94. if (value < 0)
  95. throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
  96. ViewState ["RepeatColumns"] = value;
  97. }
  98. }
  99. #if ONLY_1_1
  100. [Bindable (true)]
  101. #endif
  102. [DefaultValue (RepeatDirection.Vertical)]
  103. [WebSysDescription ("")]
  104. [WebCategory ("Layout")]
  105. public virtual RepeatDirection RepeatDirection {
  106. get {
  107. return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
  108. }
  109. set {
  110. if (value != RepeatDirection.Horizontal && value != RepeatDirection.Vertical)
  111. throw new ArgumentOutOfRangeException ("he display direction of the list is not one of the RepeatDirection values.");
  112. ViewState ["RepeatDirection"] = value;
  113. }
  114. }
  115. #if ONLY_1_1
  116. [Bindable (true)]
  117. #endif
  118. [DefaultValue (RepeatLayout.Table)]
  119. [WebSysDescription ("")]
  120. [WebCategory ("Layout")]
  121. public virtual RepeatLayout RepeatLayout {
  122. get {
  123. return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
  124. }
  125. set {
  126. if (value != RepeatLayout.Flow && value != RepeatLayout.Table)
  127. throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
  128. ViewState ["RepeatLayout"] = value;
  129. }
  130. }
  131. #if ONLY_1_1
  132. [Bindable (true)]
  133. #endif
  134. [DefaultValue (TextAlign.Right)]
  135. [WebSysDescription ("")]
  136. [WebCategory ("Appearance")]
  137. public virtual TextAlign TextAlign {
  138. get {
  139. return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
  140. }
  141. set {
  142. if (value != TextAlign.Left && value != TextAlign.Right)
  143. throw new ArgumentOutOfRangeException ("The label text alignment associated with the radio buttons is not one of the TextAlign values.");
  144. ViewState ["TextAlign"] = value;
  145. }
  146. }
  147. // Interface properties
  148. #if NET_2_0
  149. [MonoTODO]
  150. protected virtual bool HasFooter
  151. {
  152. get {
  153. throw new NotImplementedException ();
  154. }
  155. }
  156. [MonoTODO]
  157. protected virtual bool HasHeader
  158. {
  159. get {
  160. throw new NotImplementedException ();
  161. }
  162. }
  163. [MonoTODO]
  164. protected virtual bool HasSeparators
  165. {
  166. get {
  167. throw new NotImplementedException ();
  168. }
  169. }
  170. [MonoTODO]
  171. protected virtual int RepeatedItemCount
  172. {
  173. get {
  174. throw new NotImplementedException ();
  175. }
  176. }
  177. #endif
  178. bool IRepeatInfoUser.HasFooter {
  179. get { return false; }
  180. }
  181. bool IRepeatInfoUser.HasHeader {
  182. get { return false; }
  183. }
  184. bool IRepeatInfoUser.HasSeparators {
  185. get { return false; }
  186. }
  187. int IRepeatInfoUser.RepeatedItemCount {
  188. get { return Items.Count; }
  189. }
  190. protected override Style CreateControlStyle ()
  191. {
  192. return new TableStyle (ViewState);
  193. }
  194. #if NET_2_0
  195. [MonoTODO]
  196. protected override Control FindControl (string id, int pathOffset)
  197. {
  198. throw new NotImplementedException ();
  199. }
  200. #endif
  201. #if NET_2_0
  202. protected virtual
  203. #endif
  204. Style GetItemStyle (ListItemType itemType, int repeatIndex)
  205. {
  206. return null;
  207. }
  208. #if NET_2_0
  209. protected virtual
  210. #endif
  211. void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  212. {
  213. RadioButton radio = new RadioButton ();
  214. radio.Text = Items [repeatIndex].Text;
  215. radio.ID = ClientID + "_" + repeatIndex;
  216. radio.TextAlign = TextAlign;
  217. radio.GroupName = UniqueID;
  218. radio.Page = Page;
  219. radio.Checked = Items [repeatIndex].Selected;
  220. radio.ValueAttribute = Items [repeatIndex].Value;
  221. radio.AutoPostBack = AutoPostBack;
  222. radio.Enabled = Enabled;
  223. radio.TabIndex = tabIndex;
  224. radio.RenderControl (writer);
  225. }
  226. #if NET_2_0
  227. protected virtual
  228. #endif
  229. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  230. {
  231. string val = postCollection [postDataKey];
  232. ListItemCollection items = Items;
  233. int end = items.Count;
  234. int selected = SelectedIndex;
  235. for (int i = 0; i < end; i++) {
  236. ListItem item = items [i];
  237. if (item == null || val != item.Value)
  238. continue;
  239. if (i != selected) {
  240. SelectedIndex = i;
  241. need_raise = true;
  242. }
  243. return true;
  244. }
  245. return false;
  246. }
  247. #if NET_2_0
  248. protected virtual
  249. #endif
  250. void RaisePostDataChangedEvent ()
  251. {
  252. if (need_raise)
  253. OnSelectedIndexChanged (EventArgs.Empty);
  254. }
  255. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  256. {
  257. return LoadPostData (postDataKey, postCollection);
  258. }
  259. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  260. {
  261. RaisePostDataChangedEvent ();
  262. }
  263. Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
  264. {
  265. return GetItemStyle (itemType, repeatIndex);
  266. }
  267. void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  268. {
  269. RenderItem (itemType, repeatIndex, repeatInfo, writer);
  270. }
  271. #if NET_2_0
  272. protected internal
  273. #else
  274. protected
  275. #endif
  276. override void Render (HtmlTextWriter writer)
  277. {
  278. RepeatInfo repeat = new RepeatInfo ();
  279. repeat.RepeatColumns = RepeatColumns;
  280. repeat.RepeatDirection = RepeatDirection;
  281. repeat.RepeatLayout = RepeatLayout;
  282. tabIndex = TabIndex;
  283. TabIndex = 0;
  284. repeat.RenderRepeater (writer, this, ControlStyle, this);
  285. TabIndex = tabIndex;
  286. }
  287. }
  288. }