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
;
35 using System
.ComponentModel
;
40 namespace System
.Web
.UI
.WebControls
42 [ValidationProperty("SelectedItem")]
43 public class DropDownList
: ListControl
, IPostBackDataHandler
45 public DropDownList(): base()
50 public override Color BorderColor
54 return base.BorderColor
;
58 base.BorderColor
= value;
63 public override BorderStyle BorderStyle
67 return base.BorderStyle
;
71 base.BorderStyle
= value;
76 public override Unit BorderWidth
80 return base.BorderWidth
;
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
95 int index
= base.SelectedIndex
;
96 if (index
< 0 && Items
.Count
> 0) {
98 Items
[0].Selected
= true;
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
120 protected override void AddAttributesToRender(HtmlTextWriter writer
)
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
)
145 bool selected
= false;
146 foreach(ListItem current
in Items
)
148 writer
.WriteBeginTag("option");
153 throw new HttpException(HttpRuntime
.FormatResourceString("Cannot_Multiselect_In_DropDownList"));
156 writer
.WriteAttribute("selected", "selected", false);
158 writer
.WriteAttribute("value", current
.Value
, true);
160 HttpUtility
.HtmlEncode(current
.Text
, writer
);
161 writer
.WriteEndTag("option");
167 bool IPostBackDataHandler
.LoadPostData(string postDataKey
, NameValueCollection postCollection
)
169 string[] vals
= postCollection
.GetValues(postDataKey
);
172 int index
= Items
.FindByValueInternal(vals
[0]);
173 if(index
!= SelectedIndex
)
175 SelectedIndex
= index
;
182 void IPostBackDataHandler
.RaisePostDataChangedEvent()
184 OnSelectedIndexChanged(EventArgs
.Empty
);