RadioButtonList.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #if !NET_2_0
  47. bool need_raise;
  48. #endif
  49. short tabIndex = 0;
  50. public RadioButtonList ()
  51. {
  52. }
  53. #if ONLY_1_1
  54. [Bindable (true)]
  55. #endif
  56. [DefaultValue (-1)]
  57. [WebSysDescription ("")]
  58. [WebCategory ("Layout")]
  59. public virtual int CellPadding {
  60. get {
  61. if (ControlStyleCreated == false)
  62. return -1; // default value
  63. return ((TableStyle) ControlStyle).CellPadding;
  64. }
  65. set {
  66. ((TableStyle) ControlStyle).CellPadding = value;
  67. }
  68. }
  69. #if ONLY_1_1
  70. [Bindable (true)]
  71. #endif
  72. [DefaultValue (-1)]
  73. [WebSysDescription ("")]
  74. [WebCategory ("Layout")]
  75. public virtual int CellSpacing {
  76. get {
  77. if (ControlStyleCreated == false)
  78. return -1; // default value
  79. return ((TableStyle) ControlStyle).CellSpacing;
  80. }
  81. set {
  82. ((TableStyle) ControlStyle).CellSpacing = value;
  83. }
  84. }
  85. #if ONLY_1_1
  86. [Bindable (true)]
  87. #endif
  88. [DefaultValue (0)]
  89. [WebSysDescription ("")]
  90. [WebCategory ("Layout")]
  91. public virtual int RepeatColumns {
  92. get {
  93. return ViewState.GetInt ("RepeatColumns", 0);
  94. }
  95. set {
  96. if (value < 0)
  97. throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
  98. ViewState ["RepeatColumns"] = value;
  99. }
  100. }
  101. #if ONLY_1_1
  102. [Bindable (true)]
  103. #endif
  104. [DefaultValue (RepeatDirection.Vertical)]
  105. [WebSysDescription ("")]
  106. [WebCategory ("Layout")]
  107. public virtual RepeatDirection RepeatDirection {
  108. get {
  109. return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
  110. }
  111. set {
  112. if (value != RepeatDirection.Horizontal && value != RepeatDirection.Vertical)
  113. throw new ArgumentOutOfRangeException ("he display direction of the list is not one of the RepeatDirection values.");
  114. ViewState ["RepeatDirection"] = value;
  115. }
  116. }
  117. #if ONLY_1_1
  118. [Bindable (true)]
  119. #endif
  120. [DefaultValue (RepeatLayout.Table)]
  121. [WebSysDescription ("")]
  122. [WebCategory ("Layout")]
  123. public virtual RepeatLayout RepeatLayout {
  124. get {
  125. return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
  126. }
  127. set {
  128. if (value != RepeatLayout.Flow && value != RepeatLayout.Table)
  129. throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
  130. ViewState ["RepeatLayout"] = value;
  131. }
  132. }
  133. #if ONLY_1_1
  134. [Bindable (true)]
  135. #endif
  136. [DefaultValue (TextAlign.Right)]
  137. [WebSysDescription ("")]
  138. [WebCategory ("Appearance")]
  139. public virtual TextAlign TextAlign {
  140. get {
  141. return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
  142. }
  143. set {
  144. if (value != TextAlign.Left && value != TextAlign.Right)
  145. throw new ArgumentOutOfRangeException ("The label text alignment associated with the radio buttons is not one of the TextAlign values.");
  146. ViewState ["TextAlign"] = value;
  147. }
  148. }
  149. // Interface properties
  150. #if NET_2_0
  151. protected virtual
  152. #endif
  153. bool HasFooter {
  154. get { return false; }
  155. }
  156. #if NET_2_0
  157. protected virtual
  158. #endif
  159. bool HasHeader {
  160. get { return false; }
  161. }
  162. #if NET_2_0
  163. protected virtual
  164. #endif
  165. bool HasSeparators {
  166. get { return false; }
  167. }
  168. #if NET_2_0
  169. protected virtual
  170. #endif
  171. int RepeatedItemCount {
  172. get { return Items.Count; }
  173. }
  174. bool IRepeatInfoUser.HasFooter {
  175. get { return HasFooter; }
  176. }
  177. bool IRepeatInfoUser.HasHeader {
  178. get { return HasHeader; }
  179. }
  180. bool IRepeatInfoUser.HasSeparators {
  181. get { return HasSeparators; }
  182. }
  183. int IRepeatInfoUser.RepeatedItemCount {
  184. get { return RepeatedItemCount; }
  185. }
  186. protected override Style CreateControlStyle ()
  187. {
  188. return new TableStyle (ViewState);
  189. }
  190. #if NET_2_0
  191. // MSDN: Searches the current naming container for a server control
  192. // with the specified ID and path offset. The FindControl method
  193. // always returns the RadioButtonList object.
  194. protected override Control FindControl (string id, int pathOffset)
  195. {
  196. return this;
  197. }
  198. #endif
  199. #if NET_2_0
  200. protected virtual
  201. #endif
  202. Style GetItemStyle (ListItemType itemType, int repeatIndex)
  203. {
  204. return null;
  205. }
  206. #if NET_2_0
  207. protected virtual
  208. #endif
  209. void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  210. {
  211. RadioButton radio = new RadioButton ();
  212. radio.Text = Items [repeatIndex].Text;
  213. radio.ID = ClientID + "_" + repeatIndex;
  214. radio.TextAlign = TextAlign;
  215. radio.GroupName = UniqueID;
  216. radio.Page = Page;
  217. radio.Checked = Items [repeatIndex].Selected;
  218. radio.ValueAttribute = Items [repeatIndex].Value;
  219. radio.AutoPostBack = AutoPostBack;
  220. radio.Enabled = Enabled;
  221. radio.TabIndex = tabIndex;
  222. #if NET_2_0
  223. radio.ValidationGroup = ValidationGroup;
  224. radio.CausesValidation = CausesValidation;
  225. #endif
  226. radio.RenderControl (writer);
  227. }
  228. #if NET_2_0
  229. protected virtual
  230. #endif
  231. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  232. {
  233. #if NET_2_0
  234. EnsureDataBound ();
  235. #endif
  236. string val = postCollection [postDataKey];
  237. ListItemCollection items = Items;
  238. int end = items.Count;
  239. int selected = SelectedIndex;
  240. for (int i = 0; i < end; i++) {
  241. ListItem item = items [i];
  242. if (item == null || val != item.Value)
  243. continue;
  244. if (i != selected) {
  245. SelectedIndex = i;
  246. #if NET_2_0
  247. return true;
  248. #else
  249. need_raise = true;
  250. #endif
  251. }
  252. #if !NET_2_0
  253. return true;
  254. #endif
  255. }
  256. return false;
  257. }
  258. #if NET_2_0
  259. protected virtual
  260. #endif
  261. void RaisePostDataChangedEvent ()
  262. {
  263. #if NET_2_0
  264. if (CausesValidation)
  265. Page.Validate (ValidationGroup);
  266. #endif
  267. #if !NET_2_0
  268. if (need_raise)
  269. #endif
  270. OnSelectedIndexChanged (EventArgs.Empty);
  271. }
  272. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  273. {
  274. return LoadPostData (postDataKey, postCollection);
  275. }
  276. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  277. {
  278. RaisePostDataChangedEvent ();
  279. }
  280. Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
  281. {
  282. return GetItemStyle (itemType, repeatIndex);
  283. }
  284. void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  285. {
  286. RenderItem (itemType, repeatIndex, repeatInfo, writer);
  287. }
  288. #if NET_2_0
  289. protected internal
  290. #else
  291. protected
  292. #endif
  293. override void Render (HtmlTextWriter writer)
  294. {
  295. #if NET_2_0
  296. if (Page != null)
  297. Page.ClientScript.RegisterForEventValidation (this.UniqueID);
  298. if (Items.Count == 0)
  299. return;
  300. #endif
  301. RepeatInfo repeat = new RepeatInfo ();
  302. repeat.RepeatColumns = RepeatColumns;
  303. repeat.RepeatDirection = RepeatDirection;
  304. repeat.RepeatLayout = RepeatLayout;
  305. tabIndex = TabIndex;
  306. TabIndex = 0;
  307. repeat.RenderRepeater (writer, this, ControlStyle, this);
  308. TabIndex = tabIndex;
  309. }
  310. }
  311. }