DropDownList.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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;
  27. using System.Collections.Specialized;
  28. using System.ComponentModel;
  29. using System.Drawing;
  30. using System.Web;
  31. using System.Web.UI;
  32. namespace System.Web.UI.WebControls {
  33. [ValidationProperty("SelectedItem")]
  34. public class DropDownList : ListControl, IPostBackDataHandler {
  35. #region Public Constructors
  36. public DropDownList() {
  37. }
  38. #endregion // Public Constructors
  39. #region Public Instance Properties
  40. [Browsable(false)]
  41. public override Color BorderColor {
  42. get {
  43. return base.BorderColor;
  44. }
  45. set {
  46. base.BorderColor = value;
  47. }
  48. }
  49. [Browsable(false)]
  50. public override BorderStyle BorderStyle {
  51. get {
  52. return base.BorderStyle;
  53. }
  54. set {
  55. base.BorderStyle = value;
  56. }
  57. }
  58. [Browsable(false)]
  59. public override Unit BorderWidth {
  60. get {
  61. return base.BorderWidth;
  62. }
  63. set {
  64. base.BorderWidth = value;
  65. }
  66. }
  67. [DefaultValue(0)]
  68. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  69. [WebSysDescription ("")]
  70. [WebCategory ("Misc")]
  71. public override int SelectedIndex {
  72. get {
  73. int selected;
  74. selected = base.SelectedIndex;
  75. if ((selected != -1) || (Items.Count == 0)) {
  76. return selected;
  77. }
  78. Items[0].Selected = true;
  79. return 0;
  80. }
  81. set {
  82. base.SelectedIndex = value;
  83. }
  84. }
  85. #if ONLY_1_1
  86. [Bindable(false)]
  87. [Browsable(false)]
  88. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  89. [EditorBrowsable(EditorBrowsableState.Never)]
  90. public override string ToolTip {
  91. get {
  92. return string.Empty;
  93. }
  94. set {
  95. }
  96. }
  97. #endif
  98. #endregion // Public Instance Properties
  99. #region Protected Instance Methods
  100. protected override void AddAttributesToRender(HtmlTextWriter writer) {
  101. if (Page != null)
  102. Page.VerifyRenderingInServerForm (this);
  103. writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID, true);
  104. if (AutoPostBack) {
  105. writer.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  106. }
  107. base.AddAttributesToRender(writer);
  108. }
  109. protected override ControlCollection CreateControlCollection() {
  110. return base.CreateControlCollection();
  111. }
  112. #if NET_2_0
  113. protected internal
  114. #else
  115. protected
  116. #endif
  117. override void RenderContents(HtmlTextWriter writer) {
  118. int count;
  119. ListItem item;
  120. bool selected;
  121. if (writer == null) {
  122. return;
  123. }
  124. count = Items.Count;
  125. selected = false;
  126. for (int i = 0; i < count; i++) {
  127. item = Items[i];
  128. writer.WriteBeginTag("option");
  129. if (item.Selected) {
  130. if (selected) {
  131. throw new HttpException("DropDownList only may have a single selected item");
  132. }
  133. writer.WriteAttribute("selected", "selected", false);
  134. selected = true;
  135. }
  136. writer.WriteAttribute("value", item.Value, true);
  137. writer.Write(">");
  138. writer.Write(item.Text);
  139. writer.WriteEndTag("option");
  140. writer.WriteLine();
  141. }
  142. base.RenderContents(writer);
  143. }
  144. #if NET_2_0
  145. [MonoTODO]
  146. protected internal override void VerifyMultiSelect ()
  147. {
  148. throw new NotImplementedException ();
  149. }
  150. #endif
  151. #endregion // Protected Instance Methods
  152. #region Interface Methods
  153. #if NET_2_0
  154. [MonoTODO]
  155. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  156. {
  157. throw new NotImplementedException ();
  158. }
  159. [MonoTODO]
  160. protected virtual void RaisePostDataChangedEvent ()
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. #endif
  165. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection) {
  166. int index;
  167. index = Items.IndexOf(postCollection[postDataKey]);
  168. if (index != this.SelectedIndex) {
  169. SelectedIndex = index;
  170. return true;
  171. }
  172. return false;
  173. }
  174. void IPostBackDataHandler.RaisePostDataChangedEvent() {
  175. OnSelectedIndexChanged(EventArgs.Empty);
  176. }
  177. #endregion // Interface Methods
  178. }
  179. }