DropDownList.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // System.Web.UI.WebControls.DropDownList.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections.Specialized;
  33. using System.ComponentModel;
  34. using System.Drawing;
  35. using System.Web;
  36. using System.Web.UI;
  37. namespace System.Web.UI.WebControls
  38. {
  39. [ValidationProperty("SelectedItem")]
  40. public class DropDownList : ListControl, IPostBackDataHandler
  41. {
  42. public DropDownList(): base()
  43. {
  44. }
  45. [Browsable (false)]
  46. public override Color BorderColor
  47. {
  48. get
  49. {
  50. return base.BorderColor;
  51. }
  52. set
  53. {
  54. base.BorderColor = value;
  55. }
  56. }
  57. [Browsable (false)]
  58. public override BorderStyle BorderStyle
  59. {
  60. get
  61. {
  62. return base.BorderStyle;
  63. }
  64. set
  65. {
  66. base.BorderStyle = value;
  67. }
  68. }
  69. [Browsable (false)]
  70. public override Unit BorderWidth
  71. {
  72. get
  73. {
  74. return base.BorderWidth;
  75. }
  76. set
  77. {
  78. base.BorderWidth = value;
  79. }
  80. }
  81. [DefaultValue (0), WebCategory ("Misc")]
  82. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  83. [WebSysDescription ("The index number of the currently selected ListItem.")]
  84. public override int SelectedIndex
  85. {
  86. get
  87. {
  88. int index = base.SelectedIndex;
  89. if (index < 0 && Items.Count > 0) {
  90. index = 0;
  91. Items [0].Selected = true;
  92. }
  93. return index;
  94. }
  95. set
  96. {
  97. base.SelectedIndex = value;
  98. }
  99. }
  100. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  101. [Bindable (false), EditorBrowsable (EditorBrowsableState.Never)]
  102. public override string ToolTip
  103. {
  104. // MS ignores the tooltip for this one
  105. get {
  106. return String.Empty;
  107. }
  108. set {
  109. }
  110. }
  111. protected override void AddAttributesToRender(HtmlTextWriter writer)
  112. {
  113. if(Page != null)
  114. {
  115. Page.VerifyRenderingInServerForm(this);
  116. }
  117. writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
  118. base.AddAttributesToRender(writer);
  119. if(AutoPostBack && Page != null)
  120. {
  121. writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.GetPostBackClientEvent(this,""));
  122. writer.AddAttribute("language", "javascript");
  123. }
  124. }
  125. protected override ControlCollection CreateControlCollection()
  126. {
  127. return new EmptyControlCollection(this);
  128. }
  129. protected override void RenderContents(HtmlTextWriter writer)
  130. {
  131. if(Items != null)
  132. {
  133. bool selected = false;
  134. foreach(ListItem current in Items)
  135. {
  136. writer.WriteBeginTag("option");
  137. if(current.Selected)
  138. {
  139. if(selected)
  140. {
  141. throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Multiselect_In_DropDownList"));
  142. }
  143. selected = true;
  144. writer.WriteAttribute("selected", "selected", false);
  145. }
  146. writer.WriteAttribute("value", current.Value, true);
  147. writer.Write('>');
  148. HttpUtility.HtmlEncode(current.Text, writer);
  149. writer.WriteEndTag("option");
  150. writer.WriteLine();
  151. }
  152. }
  153. }
  154. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  155. {
  156. string[] vals = postCollection.GetValues(postDataKey);
  157. if(vals != null)
  158. {
  159. int index = Items.FindByValueInternal(vals[0]);
  160. if(index != SelectedIndex)
  161. {
  162. SelectedIndex = index;
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. void IPostBackDataHandler.RaisePostDataChangedEvent()
  169. {
  170. OnSelectedIndexChanged(EventArgs.Empty);
  171. }
  172. }
  173. }