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 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")]
40 [SupportsEventValidation]
42 public class DropDownList : ListControl, IPostBackDataHandler {
43 #region Public Constructors
44 public DropDownList() {
46 #endregion // Public Constructors
48 #region Public Instance Properties
50 public override Color BorderColor {
52 return base.BorderColor;
56 base.BorderColor = value;
61 public override BorderStyle BorderStyle {
63 return base.BorderStyle;
67 base.BorderStyle = value;
72 public override Unit BorderWidth {
74 return base.BorderWidth;
78 base.BorderWidth = value;
83 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
84 [WebSysDescription ("")]
85 [WebCategory ("Misc")]
86 public override int SelectedIndex {
90 selected = base.SelectedIndex;
91 if ((selected != -1) || (Items.Count == 0)) {
95 Items[0].Selected = true;
100 base.SelectedIndex = value;
107 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
108 [EditorBrowsable(EditorBrowsableState.Never)]
109 public override string ToolTip {
118 #endregion // Public Instance Properties
120 #region Protected Instance Methods
121 protected override void AddAttributesToRender(HtmlTextWriter writer)
124 Page.VerifyRenderingInServerForm (this);
128 if (!String.IsNullOrEmpty (UniqueID))
129 writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID, true);
131 if (!Enabled && SelectedIndex == -1)
134 writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID, true);
138 string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
139 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
140 writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
142 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
143 BuildScriptAttribute ("onchange", Page.ClientScript.GetPostBackClientHyperlink (this, "")));
147 base.AddAttributesToRender(writer);
151 PostBackOptions GetPostBackOptions () {
152 PostBackOptions options = new PostBackOptions (this);
153 options.ActionUrl = null;
154 options.ValidationGroup = null;
155 options.Argument = "";
156 options.RequiresJavaScriptProtocol = false;
157 options.ClientSubmit = true;
158 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
159 if (options.PerformValidation)
160 options.ValidationGroup = ValidationGroup;
166 protected override ControlCollection CreateControlCollection() {
167 return base.CreateControlCollection();
171 protected override void RenderContents(HtmlTextWriter writer) {
176 if (writer == null) {
183 for (int i = 0; i < count; i++) {
185 writer.WriteBeginTag("option");
188 throw new HttpException("DropDownList only may have a single selected item");
190 writer.WriteAttribute("selected", "selected", false);
193 writer.WriteAttribute("value", item.Value, true);
194 if (item.HasAttributes)
195 item.Attributes.Render (writer);
198 string text = HttpUtility.HtmlEncode (item.Text);
200 writer.WriteEndTag("option");
207 protected internal override void VerifyMultiSelect ()
209 throw new HttpException ("DropDownList only may have a single selected item");
213 #endregion // Protected Instance Methods
215 #region Interface Methods
219 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
226 index = Items.IndexOf(postCollection[postDataKey]);
227 if (index != this.SelectedIndex) {
228 SelectedIndex = index;
238 void RaisePostDataChangedEvent ()
241 if (CausesValidation)
242 Page.Validate (ValidationGroup);
244 OnSelectedIndexChanged(EventArgs.Empty);
247 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
249 return LoadPostData (postDataKey, postCollection);
252 void IPostBackDataHandler.RaisePostDataChangedEvent ()
254 RaisePostDataChangedEvent ();
256 #endregion // Interface Methods