DropDownList.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. if (!Enabled && SelectedIndex == -1)
  115. SelectedIndex = 1;
  116. #else
  117. writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID, true);
  118. #endif
  119. if (AutoPostBack) {
  120. #if NET_2_0
  121. writer.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true));
  122. #else
  123. writer.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  124. #endif
  125. }
  126. base.AddAttributesToRender(writer);
  127. }
  128. #if NET_2_0
  129. PostBackOptions GetPostBackOptions () {
  130. PostBackOptions options = new PostBackOptions (this);
  131. options.ActionUrl = null;
  132. options.ValidationGroup = null;
  133. options.Argument = "";
  134. options.RequiresJavaScriptProtocol = false;
  135. options.ClientSubmit = true;
  136. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  137. if (options.PerformValidation)
  138. options.ValidationGroup = ValidationGroup;
  139. return options;
  140. }
  141. #endif
  142. protected override ControlCollection CreateControlCollection() {
  143. return base.CreateControlCollection();
  144. }
  145. #if ONLY_1_1
  146. protected override void RenderContents(HtmlTextWriter writer) {
  147. int count;
  148. ListItem item;
  149. bool selected;
  150. if (writer == null) {
  151. return;
  152. }
  153. count = Items.Count;
  154. selected = false;
  155. for (int i = 0; i < count; i++) {
  156. item = Items[i];
  157. writer.WriteBeginTag("option");
  158. if (item.Selected) {
  159. if (selected) {
  160. throw new HttpException("DropDownList only may have a single selected item");
  161. }
  162. writer.WriteAttribute("selected", "selected", false);
  163. selected = true;
  164. }
  165. writer.WriteAttribute("value", item.Value, true);
  166. writer.Write(">");
  167. string text = HttpUtility.HtmlEncode (item.Text);
  168. writer.Write (text);
  169. writer.WriteEndTag("option");
  170. writer.WriteLine();
  171. }
  172. }
  173. #endif
  174. #if NET_2_0
  175. protected internal override void VerifyMultiSelect ()
  176. {
  177. throw new HttpException ("DropDownList only may have a single selected item");
  178. }
  179. #endif
  180. #endregion // Protected Instance Methods
  181. #region Interface Methods
  182. #if NET_2_0
  183. protected virtual
  184. #endif
  185. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  186. {
  187. #if NET_2_0
  188. EnsureDataBound ();
  189. #endif
  190. int index;
  191. index = Items.IndexOf(postCollection[postDataKey]);
  192. if (index != this.SelectedIndex) {
  193. SelectedIndex = index;
  194. return true;
  195. }
  196. return false;
  197. }
  198. #if NET_2_0
  199. protected virtual
  200. #endif
  201. void RaisePostDataChangedEvent ()
  202. {
  203. #if NET_2_0
  204. if (CausesValidation)
  205. Page.Validate (ValidationGroup);
  206. #endif
  207. OnSelectedIndexChanged(EventArgs.Empty);
  208. }
  209. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  210. {
  211. return LoadPostData (postDataKey, postCollection);
  212. }
  213. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  214. {
  215. RaisePostDataChangedEvent ();
  216. }
  217. #endregion // Interface Methods
  218. }
  219. }