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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005-2010 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
27 using System.Collections.Specialized;
28 using System.ComponentModel;
30 using System.Security.Permissions;
32 namespace System.Web.UI.WebControls
35 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [ValidationProperty("SelectedItem")]
39 [SupportsEventValidation]
40 public class DropDownList : ListControl, IPostBackDataHandler
42 #region Public Constructors
43 public DropDownList() {
45 #endregion // Public Constructors
47 #region Public Instance Properties
49 public override Color BorderColor {
50 get { return base.BorderColor; }
51 set { base.BorderColor = value; }
55 public override BorderStyle BorderStyle {
56 get { return base.BorderStyle; }
57 set { base.BorderStyle = value; }
61 public override Unit BorderWidth {
62 get { return base.BorderWidth; }
63 set { base.BorderWidth = value; }
67 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
68 [WebSysDescription ("")]
69 [WebCategory ("Misc")]
70 public override int SelectedIndex {
74 selected = base.SelectedIndex;
75 if ((selected != -1) || (Items.Count == 0))
78 Items[0].Selected = true;
82 set { base.SelectedIndex = value; }
84 public override bool SupportsDisabledAttribute {
85 get { return RenderingCompatibilityLessThan40; }
87 #endregion // Public Instance Properties
89 #region Protected Instance Methods
90 protected override void AddAttributesToRender(HtmlTextWriter writer)
94 page.VerifyRenderingInServerForm (this);
98 if (!String.IsNullOrEmpty (UniqueID))
99 writer.AddAttribute (HtmlTextWriterAttribute.Name, this.UniqueID, true);
101 if (!IsEnabled && SelectedIndex == -1)
105 string onchange = page != null ? page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true) : String.Empty;
106 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
107 writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
110 base.AddAttributesToRender(writer);
113 PostBackOptions GetPostBackOptions ()
115 PostBackOptions options = new PostBackOptions (this);
116 options.ActionUrl = null;
117 options.ValidationGroup = null;
118 options.Argument = String.Empty;
119 options.RequiresJavaScriptProtocol = false;
120 options.ClientSubmit = true;
123 options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
124 if (options.PerformValidation)
125 options.ValidationGroup = ValidationGroup;
130 protected override ControlCollection CreateControlCollection ()
132 return base.CreateControlCollection();
135 protected internal override void VerifyMultiSelect ()
137 throw new HttpException ("DropDownList only may have a single selected item");
139 #endregion // Protected Instance Methods
141 #region Interface Methods
142 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
145 int index = Items.IndexOf(postCollection[postDataKey]);
146 ValidateEvent (postDataKey, postCollection [postDataKey]);
147 if (index != this.SelectedIndex) {
148 SelectedIndex = index;
155 protected virtual void RaisePostDataChangedEvent ()
157 if (CausesValidation) {
160 page.Validate (ValidationGroup);
163 OnSelectedIndexChanged(EventArgs.Empty);
166 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
168 return LoadPostData (postDataKey, postCollection);
171 void IPostBackDataHandler.RaisePostDataChangedEvent ()
173 RaisePostDataChangedEvent ();
175 #endregion // Interface Methods