Merge pull request #2916 from ludovic-henry/fix-40306
[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-2010 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         [SupportsEventValidation]
40         public class DropDownList : ListControl, IPostBackDataHandler
41         {
42                 #region Public Constructors
43                 public DropDownList() {
44                 }
45                 #endregion      // Public Constructors
46
47                 #region Public Instance Properties
48                 [Browsable(false)]
49                 public override Color BorderColor {
50                         get { return base.BorderColor; }
51                         set { base.BorderColor = value; }
52                 }
53
54                 [Browsable(false)]
55                 public override BorderStyle BorderStyle {
56                         get { return base.BorderStyle; }
57                         set { base.BorderStyle = value; }
58                 }
59
60                 [Browsable(false)]
61                 public override Unit BorderWidth {
62                         get { return base.BorderWidth; }
63                         set { base.BorderWidth = value; }
64                 }
65
66                 [DefaultValue(0)]
67                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
68                 [WebSysDescription ("")]
69                 [WebCategory ("Misc")]
70                 public override int SelectedIndex {
71                         get {
72                                 int selected;
73
74                                 selected = base.SelectedIndex;
75                                 if ((selected != -1) || (Items.Count == 0))
76                                         return selected;
77
78                                 Items[0].Selected = true;
79                                 return 0;
80                         }
81
82                         set { base.SelectedIndex = value; }
83                 }
84                 public override bool SupportsDisabledAttribute {
85                         get { return RenderingCompatibilityLessThan40; }
86                 }
87                 #endregion      // Public Instance Properties
88
89                 #region Protected Instance Methods
90                 protected override void AddAttributesToRender(HtmlTextWriter writer)
91                 {
92                         Page page = Page;
93                         if (page != null)
94                                 page.VerifyRenderingInServerForm (this);
95
96                         if (writer == null)
97                                 return;
98                         if (!String.IsNullOrEmpty (UniqueID))
99                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, this.UniqueID, true);
100
101                         if (!IsEnabled && SelectedIndex == -1)
102                                 SelectedIndex = 1;
103
104                         if (AutoPostBack) {
105                                 string onchange = page != null ? page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true) : String.Empty;
106                                 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
107                                 writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
108                         }
109
110                         base.AddAttributesToRender(writer);
111                 }
112
113                 PostBackOptions GetPostBackOptions ()
114                 {
115                         PostBackOptions options = new PostBackOptions (this);
116                         options.ActionUrl = null;
117                         options.ValidationGroup = null;
118                         options.Argument = String.Empty;
119                         options.RequiresJavaScriptProtocol = false;
120                         options.ClientSubmit = true;
121
122                         Page page = Page;
123                         options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
124                         if (options.PerformValidation)
125                                 options.ValidationGroup = ValidationGroup;
126
127                         return options;
128                 }
129
130                 protected override ControlCollection CreateControlCollection ()
131                 {
132                         return base.CreateControlCollection();
133                 }
134
135                 protected internal override void VerifyMultiSelect ()
136                 {
137                         throw new HttpException ("DropDownList only may have a single selected item");
138                 }               
139                 #endregion      // Protected Instance Methods
140
141                 #region Interface Methods
142                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
143                 {
144                         EnsureDataBound ();
145                         int index = Items.IndexOf(postCollection[postDataKey]);
146                         ValidateEvent (postDataKey, postCollection [postDataKey]);
147                         if (index != this.SelectedIndex) {
148                                 SelectedIndex = index;
149                                 return true;
150                         }
151                         
152                         return false;
153                 }
154
155                 protected virtual void RaisePostDataChangedEvent ()
156                 {
157                         if (CausesValidation) {
158                                 Page page = Page;
159                                 if (page != null)
160                                         page.Validate (ValidationGroup);
161                         }
162                         
163                         OnSelectedIndexChanged(EventArgs.Empty);
164                 }
165                 
166                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
167                 {
168                         return LoadPostData (postDataKey, postCollection);
169                 }
170
171                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
172                 {
173                         RaisePostDataChangedEvent ();
174                 }
175                 #endregion      // Interface Methods
176         }
177 }