2010-03-06 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DropDownList.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    (pbartok@novell.com)
24 //
25 //
26
27 using System.Collections.Specialized;
28 using System.ComponentModel;
29 using System.Drawing;
30 using System.Security.Permissions;
31
32 namespace System.Web.UI.WebControls {
33
34         // CAS
35         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         // attributes
38         [ValidationProperty("SelectedItem")]
39 #if NET_2_0
40         [SupportsEventValidation]
41 #endif
42         public class DropDownList : ListControl, IPostBackDataHandler {
43                 #region Public Constructors
44                 public DropDownList() {
45                 }
46                 #endregion      // Public Constructors
47
48                 #region Public Instance Properties
49                 [Browsable(false)]
50                 public override Color BorderColor {
51                         get {
52                                 return base.BorderColor;
53                         }
54
55                         set {
56                                 base.BorderColor = value;
57                         }
58                 }
59
60                 [Browsable(false)]
61                 public override BorderStyle BorderStyle {
62                         get {
63                                 return base.BorderStyle;
64                         }
65
66                         set {
67                                 base.BorderStyle = value;
68                         }
69                 }
70
71                 [Browsable(false)]
72                 public override Unit BorderWidth {
73                         get {
74                                 return base.BorderWidth;
75                         }
76
77                         set {
78                                 base.BorderWidth = value;
79                         }
80                 }
81
82                 [DefaultValue(0)]
83                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
84                 [WebSysDescription ("")]
85                 [WebCategory ("Misc")]
86                 public override int SelectedIndex {
87                         get {
88                                 int selected;
89
90                                 selected = base.SelectedIndex;
91                                 if ((selected != -1) || (Items.Count == 0)) {
92                                         return selected;
93                                 }
94
95                                 Items[0].Selected = true;
96                                 return 0;
97                         }
98
99                         set {
100                                 base.SelectedIndex = value;
101                         }
102                 }
103
104 #if ONLY_1_1
105                 [Bindable(false)]
106                 [Browsable(false)]
107                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
108                 [EditorBrowsable(EditorBrowsableState.Never)]
109                 public override string ToolTip {
110                         get {
111                                 return string.Empty;
112                         }
113
114                         set {
115                         }
116                 }
117 #endif          
118                 #endregion      // Public Instance Properties
119
120                 #region Protected Instance Methods
121                 protected override void AddAttributesToRender(HtmlTextWriter writer)
122                 {
123                         if (Page != null)
124                                 Page.VerifyRenderingInServerForm (this);
125 #if NET_2_0
126                         if (writer == null)
127                                 return;
128                         if (!String.IsNullOrEmpty (UniqueID))
129                                 writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID, true);
130
131                         if (!Enabled && SelectedIndex == -1)
132                                 SelectedIndex = 1;
133 #else
134                         writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID, true);
135 #endif
136                         if (AutoPostBack) {
137 #if NET_2_0
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));
141 #else
142                                 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
143                                                      BuildScriptAttribute ("onchange", Page.ClientScript.GetPostBackClientHyperlink (this, "")));
144 #endif
145                         }
146
147                         base.AddAttributesToRender(writer);
148                 }
149
150 #if NET_2_0
151                 PostBackOptions GetPostBackOptions () {
152                         PostBackOptions options = new PostBackOptions (this);
153                         options.ActionUrl = null;
154                         options.ValidationGroup = null;
155                         options.Argument = String.Empty;
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;
161
162                         return options;
163                 }
164 #endif          
165
166                 protected override ControlCollection CreateControlCollection() {
167                         return base.CreateControlCollection();
168                 }
169
170 #if ONLY_1_1
171                 protected override void RenderContents(HtmlTextWriter writer) {
172                         int             count;
173                         ListItem        item;
174                         bool            selected;
175
176                         if (writer == null) {
177                                 return;
178                         }
179
180                         count = Items.Count;
181                         selected = false;
182
183                         for (int i = 0; i < count; i++) {
184                                 item = Items[i];
185                                 writer.WriteBeginTag("option");
186                                 if (item.Selected) {
187                                         if (selected) {
188                                                 throw new HttpException("DropDownList only may have a single selected item");
189                                         }
190                                         writer.WriteAttribute("selected", "selected", false);
191                                         selected = true;
192                                 }
193                                 writer.WriteAttribute("value", item.Value, true);
194                                 if (item.HasAttributes)
195                                         item.Attributes.Render (writer);
196                                 
197                                 writer.Write(">");
198                                 string text = HttpUtility.HtmlEncode (item.Text);
199                                 writer.Write (text);
200                                 writer.WriteEndTag("option");
201                                 writer.WriteLine();
202                         }
203                 }
204 #endif
205
206 #if NET_2_0
207                 protected internal override void VerifyMultiSelect ()
208                 {
209                         throw new HttpException ("DropDownList only may have a single selected item");
210                 }
211 #endif          
212                 
213                 #endregion      // Protected Instance Methods
214
215                 #region Interface Methods
216 #if NET_2_0
217                 protected virtual
218 #endif
219                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
220                 {
221 #if NET_2_0
222                         EnsureDataBound ();
223 #endif
224                         int     index;
225
226                         index = Items.IndexOf(postCollection[postDataKey]);
227 #if NET_2_0
228                         ValidateEvent (postDataKey, postCollection [postDataKey]);
229 #endif
230                         if (index != this.SelectedIndex) {
231                                 SelectedIndex = index;
232                                 return true;
233                         }
234                         
235                         return false;
236                 }
237
238 #if NET_2_0
239                 protected virtual
240 #endif
241                 void RaisePostDataChangedEvent ()
242                 {
243 #if NET_2_0
244                         if (CausesValidation)
245                                 Page.Validate (ValidationGroup);
246 #endif
247                         OnSelectedIndexChanged(EventArgs.Empty);
248                 }
249                 
250                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
251                 {
252                         return LoadPostData (postDataKey, postCollection);
253                 }
254
255                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
256                 {
257                         RaisePostDataChangedEvent ();
258                 }
259                 #endregion      // Interface Methods
260         }
261 }