ChangeLog: Updated.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DropDownList.cs
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
10 //\r
11
12 //
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.
31 //
32 \r
33 using System;\r
34 using System.Collections.Specialized;\r
35 using System.ComponentModel;\r
36 using System.Drawing;\r
37 using System.Web;\r
38 using System.Web.UI;\r
39 \r
40 namespace System.Web.UI.WebControls\r
41 {\r
42         [ValidationProperty("SelectedItem")]\r
43         public class DropDownList : ListControl, IPostBackDataHandler\r
44         {\r
45                 public DropDownList(): base()\r
46                 {\r
47                 }\r
48
49                 [Browsable (false)]\r
50                 public override Color BorderColor\r
51                 {\r
52                         get\r
53                         {\r
54                                 return base.BorderColor;\r
55                         }\r
56                         set\r
57                         {\r
58                                 base.BorderColor = value;\r
59                         }\r
60                 }\r
61
62                 [Browsable (false)]\r
63                 public override BorderStyle BorderStyle\r
64                 {\r
65                         get\r
66                         {\r
67                                 return base.BorderStyle;\r
68                         }\r
69                         set\r
70                         {\r
71                                 base.BorderStyle = value;\r
72                         }\r
73                 }\r
74
75                 [Browsable (false)]\r
76                 public override Unit BorderWidth\r
77                 {\r
78                         get\r
79                         {\r
80                                 return base.BorderWidth;\r
81                         }\r
82                         set\r
83                         {\r
84                                 base.BorderWidth = value;\r
85                         }\r
86                 }\r
87
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
92                 {\r
93                         get\r
94                         {\r
95                                 int index  = base.SelectedIndex;\r
96                                 if (index < 0 && Items.Count > 0) {\r
97                                         index = 0;\r
98                                         Items [0].Selected = true;\r
99                                 }\r
100                                 return index;\r
101                         }\r
102                         set\r
103                         {\r
104                                 base.SelectedIndex = value;\r
105                         }\r
106                 }\r
107
108                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
109                 [Bindable (false), EditorBrowsable (EditorBrowsableState.Never)]\r
110                 public override string ToolTip\r
111                 {\r
112                         // MS ignores the tooltip for this one\r
113                         get {\r
114                                 return String.Empty;\r
115                         }\r
116                         set {\r
117                         }\r
118                 }\r
119 \r
120                 protected override void AddAttributesToRender(HtmlTextWriter writer)\r
121                 {\r
122                         if(Page != null)\r
123                         {\r
124                                 Page.VerifyRenderingInServerForm(this);\r
125                         }\r
126                         writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);\r
127                         base.AddAttributesToRender(writer);\r
128 \r
129                         if(AutoPostBack && Page != null)\r
130                         {\r
131                                 writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.GetPostBackClientEvent(this,""));\r
132                                 writer.AddAttribute("language", "javascript");\r
133                         }\r
134                 }\r
135 \r
136                 protected override ControlCollection CreateControlCollection()\r
137                 {\r
138                         return new EmptyControlCollection(this);\r
139                 }\r
140 \r
141                 protected override void RenderContents(HtmlTextWriter writer)\r
142                 {\r
143                         if(Items != null)\r
144                         {\r
145                                 bool selected = false;\r
146                                 foreach(ListItem current in Items)\r
147                                 {\r
148                                         writer.WriteBeginTag("option");\r
149                                         if(current.Selected)\r
150                                         {\r
151                                                 if(selected)\r
152                                                 {\r
153                                                         throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Multiselect_In_DropDownList"));\r
154                                                 }\r
155                                                 selected = true;\r
156                                                 writer.WriteAttribute("selected", "selected", false);\r
157                                         }\r
158                                         writer.WriteAttribute("value", current.Value, true);\r
159                                         writer.Write('>');\r
160                                         HttpUtility.HtmlEncode(current.Text, writer);\r
161                                         writer.WriteEndTag("option");\r
162                                         writer.WriteLine();\r
163                                 }\r
164                         }\r
165                 }\r
166 \r
167                 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)\r
168                 {\r
169                         string[] vals = postCollection.GetValues(postDataKey);\r
170                         if(vals != null)\r
171                         {\r
172                                 int index = Items.FindByValueInternal(vals[0]);\r
173                                 if(index != SelectedIndex)\r
174                                 {\r
175                                         SelectedIndex = index;\r
176                                         return true;\r
177                                 }\r
178                         }\r
179                         return false;\r
180                 }\r
181 \r
182                 void IPostBackDataHandler.RaisePostDataChangedEvent()\r
183                 {\r
184                         OnSelectedIndexChanged(EventArgs.Empty);\r
185                 }\r
186         }\r
187 }\r