DropDownList.cs 6.6 KB

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