DropDownList.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. using System.Collections.Specialized;
  27. using System.ComponentModel;
  28. using System.Drawing;
  29. using System.Security.Permissions;
  30. namespace System.Web.UI.WebControls {
  31. // CAS
  32. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. // attributes
  35. [ValidationProperty("SelectedItem")]
  36. #if NET_2_0
  37. [SupportsEventValidation]
  38. #endif
  39. public class DropDownList : ListControl, IPostBackDataHandler {
  40. #region Public Constructors
  41. public DropDownList() {
  42. }
  43. #endregion // Public Constructors
  44. #region Public Instance Properties
  45. [Browsable(false)]
  46. public override Color BorderColor {
  47. get {
  48. return base.BorderColor;
  49. }
  50. set {
  51. base.BorderColor = value;
  52. }
  53. }
  54. [Browsable(false)]
  55. public override BorderStyle BorderStyle {
  56. get {
  57. return base.BorderStyle;
  58. }
  59. set {
  60. base.BorderStyle = value;
  61. }
  62. }
  63. [Browsable(false)]
  64. public override Unit BorderWidth {
  65. get {
  66. return base.BorderWidth;
  67. }
  68. set {
  69. base.BorderWidth = value;
  70. }
  71. }
  72. [DefaultValue(0)]
  73. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  74. [WebSysDescription ("")]
  75. [WebCategory ("Misc")]
  76. public override int SelectedIndex {
  77. get {
  78. int selected;
  79. selected = base.SelectedIndex;
  80. if ((selected != -1) || (Items.Count == 0)) {
  81. return selected;
  82. }
  83. Items[0].Selected = true;
  84. return 0;
  85. }
  86. set {
  87. base.SelectedIndex = value;
  88. }
  89. }
  90. #if ONLY_1_1
  91. [Bindable(false)]
  92. [Browsable(false)]
  93. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  94. [EditorBrowsable(EditorBrowsableState.Never)]
  95. public override string ToolTip {
  96. get {
  97. return string.Empty;
  98. }
  99. set {
  100. }
  101. }
  102. #endif
  103. #endregion // Public Instance Properties
  104. #region Protected Instance Methods
  105. protected override void AddAttributesToRender(HtmlTextWriter writer)
  106. {
  107. if (Page != null)
  108. Page.VerifyRenderingInServerForm (this);
  109. #if NET_2_0
  110. if (writer == null)
  111. return;
  112. if (!String.IsNullOrEmpty (UniqueID))
  113. writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID, true);
  114. #else
  115. writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID, true);
  116. #endif
  117. if (AutoPostBack) {
  118. #if NET_2_0
  119. writer.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true));
  120. #else
  121. writer.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  122. #endif
  123. }
  124. base.AddAttributesToRender(writer);
  125. }
  126. #if NET_2_0
  127. PostBackOptions GetPostBackOptions () {
  128. PostBackOptions options = new PostBackOptions (this);
  129. options.ActionUrl = null;
  130. options.ValidationGroup = null;
  131. options.Argument = "";
  132. options.RequiresJavaScriptProtocol = false;
  133. options.ClientSubmit = true;
  134. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  135. if (options.PerformValidation)
  136. options.ValidationGroup = ValidationGroup;
  137. return options;
  138. }
  139. #endif
  140. protected override ControlCollection CreateControlCollection() {
  141. return base.CreateControlCollection();
  142. }
  143. #if !NET_2_0
  144. protected override void RenderContents(HtmlTextWriter writer) {
  145. int count;
  146. ListItem item;
  147. bool selected;
  148. if (writer == null) {
  149. return;
  150. }
  151. count = Items.Count;
  152. selected = false;
  153. for (int i = 0; i < count; i++) {
  154. item = Items[i];
  155. writer.WriteBeginTag("option");
  156. if (item.Selected) {
  157. if (selected) {
  158. throw new HttpException("DropDownList only may have a single selected item");
  159. }
  160. writer.WriteAttribute("selected", "selected", false);
  161. selected = true;
  162. }
  163. writer.WriteAttribute("value", item.Value, true);
  164. writer.Write(">");
  165. string text = HttpUtility.HtmlEncode (item.Text);
  166. writer.Write (text);
  167. writer.WriteEndTag("option");
  168. writer.WriteLine();
  169. }
  170. }
  171. #endif
  172. #if NET_2_0
  173. protected internal override void VerifyMultiSelect ()
  174. {
  175. throw new HttpException ("DropDownList only may have a single selected item");
  176. }
  177. #endif
  178. #endregion // Protected Instance Methods
  179. #region Interface Methods
  180. #if NET_2_0
  181. protected virtual
  182. #endif
  183. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  184. {
  185. #if NET_2_0
  186. EnsureDataBound ();
  187. #endif
  188. int index;
  189. index = Items.IndexOf(postCollection[postDataKey]);
  190. if (index != this.SelectedIndex) {
  191. SelectedIndex = index;
  192. return true;
  193. }
  194. return false;
  195. }
  196. #if NET_2_0
  197. protected virtual
  198. #endif
  199. void RaisePostDataChangedEvent ()
  200. {
  201. #if NET_2_0
  202. if (CausesValidation)
  203. Page.Validate (ValidationGroup);
  204. #endif
  205. OnSelectedIndexChanged(EventArgs.Empty);
  206. }
  207. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  208. {
  209. return LoadPostData (postDataKey, postCollection);
  210. }
  211. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  212. {
  213. RaisePostDataChangedEvent ();
  214. }
  215. #endregion // Interface Methods
  216. }
  217. }