2 // System.Web.UI.WebControls.DropDownList.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections.Specialized;
\r
35 using System.ComponentModel;
\r
36 using System.Drawing;
\r
38 using System.Web.UI;
\r
40 namespace System.Web.UI.WebControls
\r
42 [ValidationProperty("SelectedItem")]
\r
43 public class DropDownList : ListControl, IPostBackDataHandler
\r
45 public DropDownList(): base()
\r
50 public override Color BorderColor
\r
54 return base.BorderColor;
\r
58 base.BorderColor = value;
\r
63 public override BorderStyle BorderStyle
\r
67 return base.BorderStyle;
\r
71 base.BorderStyle = value;
\r
76 public override Unit BorderWidth
\r
80 return base.BorderWidth;
\r
84 base.BorderWidth = value;
\r
88 [DefaultValue (0), WebCategory ("Misc")]
89 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
90 [WebSysDescription ("The index number of the currently selected ListItem.")]
\r
91 public override int SelectedIndex
\r
95 int index = base.SelectedIndex;
\r
96 if (index < 0 && Items.Count > 0) {
\r
98 Items [0].Selected = true;
\r
104 base.SelectedIndex = value;
\r
108 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
109 [Bindable (false), EditorBrowsable (EditorBrowsableState.Never)]
\r
110 public override string ToolTip
\r
112 // MS ignores the tooltip for this one
\r
114 return String.Empty;
\r
120 protected override void AddAttributesToRender(HtmlTextWriter writer)
\r
124 Page.VerifyRenderingInServerForm(this);
\r
126 writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
\r
127 base.AddAttributesToRender(writer);
\r
129 if(AutoPostBack && Page != null)
\r
131 writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.GetPostBackClientEvent(this,""));
\r
132 writer.AddAttribute("language", "javascript");
\r
136 protected override ControlCollection CreateControlCollection()
\r
138 return new EmptyControlCollection(this);
\r
141 protected override void RenderContents(HtmlTextWriter writer)
\r
145 bool selected = false;
\r
146 foreach(ListItem current in Items)
\r
148 writer.WriteBeginTag("option");
\r
149 if(current.Selected)
\r
153 throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Multiselect_In_DropDownList"));
\r
156 writer.WriteAttribute("selected", "selected", false);
\r
158 writer.WriteAttribute("value", current.Value, true);
\r
160 HttpUtility.HtmlEncode(current.Text, writer);
\r
161 writer.WriteEndTag("option");
\r
162 writer.WriteLine();
\r
167 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
\r
169 string[] vals = postCollection.GetValues(postDataKey);
\r
172 int index = Items.FindByValueInternal(vals[0]);
\r
173 if(index != SelectedIndex)
\r
175 SelectedIndex = index;
\r
182 void IPostBackDataHandler.RaisePostDataChangedEvent()
\r
184 OnSelectedIndexChanged(EventArgs.Empty);
\r