DropDownList.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. public override int SelectedIndex {
  70. get {
  71. int selected;
  72. selected = base.SelectedIndex;
  73. if ((selected != -1) || (Items.Count == 0)) {
  74. return selected;
  75. }
  76. Items[0].Selected = true;
  77. return 0;
  78. }
  79. set {
  80. base.SelectedIndex = value;
  81. }
  82. }
  83. #if ONLY_1_1
  84. [Bindable(false)]
  85. [Browsable(false)]
  86. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  87. [EditorBrowsable(EditorBrowsableState.Never)]
  88. public override string ToolTip {
  89. get {
  90. return string.Empty;
  91. }
  92. set {
  93. }
  94. }
  95. #endif
  96. #endregion // Public Instance Properties
  97. #region Protected Instance Methods
  98. protected override void AddAttributesToRender(HtmlTextWriter writer) {
  99. if (Page != null)
  100. Page.VerifyRenderingInServerForm (this);
  101. writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID, true);
  102. if (AutoPostBack) {
  103. writer.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  104. }
  105. base.AddAttributesToRender(writer);
  106. }
  107. protected override ControlCollection CreateControlCollection() {
  108. return base.CreateControlCollection();
  109. }
  110. #if NET_2_0
  111. protected internal
  112. #else
  113. protected
  114. #endif
  115. override void RenderContents(HtmlTextWriter writer) {
  116. int count;
  117. ListItem item;
  118. bool selected;
  119. if (writer == null) {
  120. return;
  121. }
  122. count = Items.Count;
  123. selected = false;
  124. for (int i = 0; i < count; i++) {
  125. item = Items[i];
  126. writer.WriteBeginTag("option");
  127. if (item.Selected) {
  128. if (selected) {
  129. throw new HttpException("DropDownList only may have a single selected item");
  130. }
  131. writer.WriteAttribute("selected", "selected", false);
  132. selected = true;
  133. }
  134. writer.WriteAttribute("value", item.Value, true);
  135. writer.Write(">");
  136. writer.Write(item.Text);
  137. writer.WriteEndTag("option");
  138. writer.WriteLine();
  139. }
  140. base.RenderContents(writer);
  141. }
  142. #if NET_2_0
  143. [MonoTODO]
  144. protected internal override void VerifyMultiSelect ()
  145. {
  146. throw new NotImplementedException ();
  147. }
  148. #endif
  149. #endregion // Protected Instance Methods
  150. #region Interface Methods
  151. #if NET_2_0
  152. [MonoTODO]
  153. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. [MonoTODO]
  158. protected virtual void RaisePostDataChangedEvent ()
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. #endif
  163. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection) {
  164. int index;
  165. index = Items.IndexOf(postCollection[postDataKey]);
  166. if (index != this.SelectedIndex) {
  167. SelectedIndex = index;
  168. return true;
  169. }
  170. return false;
  171. }
  172. void IPostBackDataHandler.RaisePostDataChangedEvent() {
  173. OnSelectedIndexChanged(EventArgs.Empty);
  174. }
  175. #endregion // Interface Methods
  176. }
  177. }