**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / DropDownList.cs
blob7fee4f46f2144ef6d925b878a3beba45025501b6
1 //
2 // System.Web.UI.WebControls.DropDownList.cs
3 //
4 // Authors:
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Drawing;
37 using System.Web;
38 using System.Web.UI;
40 namespace System.Web.UI.WebControls
42 [ValidationProperty("SelectedItem")]
43 public class DropDownList : ListControl, IPostBackDataHandler
45 public DropDownList(): base()
49 [Browsable (false)]
50 public override Color BorderColor
52 get
54 return base.BorderColor;
56 set
58 base.BorderColor = value;
62 [Browsable (false)]
63 public override BorderStyle BorderStyle
65 get
67 return base.BorderStyle;
69 set
71 base.BorderStyle = value;
75 [Browsable (false)]
76 public override Unit BorderWidth
78 get
80 return base.BorderWidth;
82 set
84 base.BorderWidth = value;
88 [DefaultValue (0), WebCategory ("Misc")]
89 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
90 [WebSysDescription ("The index number of the currently selected ListItem.")]
91 public override int SelectedIndex
93 get
95 int index = base.SelectedIndex;
96 if (index < 0 && Items.Count > 0) {
97 index = 0;
98 Items [0].Selected = true;
100 return index;
104 base.SelectedIndex = value;
108 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
109 [Bindable (false), EditorBrowsable (EditorBrowsableState.Never)]
110 public override string ToolTip
112 // MS ignores the tooltip for this one
113 get {
114 return String.Empty;
116 set {
120 protected override void AddAttributesToRender(HtmlTextWriter writer)
122 if(Page != null)
124 Page.VerifyRenderingInServerForm(this);
126 writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
127 base.AddAttributesToRender(writer);
129 if(AutoPostBack && Page != null)
131 writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.GetPostBackClientEvent(this,""));
132 writer.AddAttribute("language", "javascript");
136 protected override ControlCollection CreateControlCollection()
138 return new EmptyControlCollection(this);
141 protected override void RenderContents(HtmlTextWriter writer)
143 if(Items != null)
145 bool selected = false;
146 foreach(ListItem current in Items)
148 writer.WriteBeginTag("option");
149 if(current.Selected)
151 if(selected)
153 throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Multiselect_In_DropDownList"));
155 selected = true;
156 writer.WriteAttribute("selected", "selected", false);
158 writer.WriteAttribute("value", current.Value, true);
159 writer.Write('>');
160 HttpUtility.HtmlEncode(current.Text, writer);
161 writer.WriteEndTag("option");
162 writer.WriteLine();
167 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
169 string[] vals = postCollection.GetValues(postDataKey);
170 if(vals != null)
172 int index = Items.FindByValueInternal(vals[0]);
173 if(index != SelectedIndex)
175 SelectedIndex = index;
176 return true;
179 return false;
182 void IPostBackDataHandler.RaisePostDataChangedEvent()
184 OnSelectedIndexChanged(EventArgs.Empty);