[bcl] Remove ONLY_1_1 defines from class libs
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / ListBox.cs
blob5018cadc97f775d907c05ab380748bff88269828
1 //
2 // System.Web.UI.WebControls.Literal.cs
3 //
4 // Authors:
5 // Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.ComponentModel;
31 using System.Drawing;
32 using System.Globalization;
33 using System.Collections.Specialized;
34 using System.Security.Permissions;
35 using System.Web.Util;
37 namespace System.Web.UI.WebControls {
39 // CAS
40 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 // attributes
43 [ValidationProperty("SelectedItem")]
44 [SupportsEventValidation]
45 public class ListBox : ListControl, IPostBackDataHandler {
47 public ListBox ()
51 [Browsable(false)]
52 #if HAVE_CONTROL_ADAPTERS
53 public virtual new
54 #else
55 public override
56 #endif
57 Color BorderColor {
58 get { return base.BorderColor; }
59 set { base.BorderColor = value; }
62 [Browsable(false)]
63 #if HAVE_CONTROL_ADAPTERS
64 public virtual new
65 #else
66 public override
67 #endif
68 BorderStyle BorderStyle {
69 get { return base.BorderStyle; }
70 set { base.BorderStyle = value; }
73 [Browsable(false)]
74 #if HAVE_CONTROL_ADAPTERS
75 public virtual new
76 #else
77 public override
78 #endif
79 Unit BorderWidth {
80 get { return base.BorderWidth; }
81 set { base.BorderWidth = value; }
84 [DefaultValue(4)]
85 [WebSysDescription ("")]
86 [WebCategory ("Appearance")]
87 public virtual int Rows {
88 get {
89 return ViewState.GetInt ("Rows", 4);
91 set {
92 if (value < 1 || value > 2000)
93 throw new ArgumentOutOfRangeException ("value");
94 ViewState ["Rows"] = value;
98 [DefaultValue(ListSelectionMode.Single)]
99 [WebSysDescription ("")]
100 [WebCategory ("Behavior")]
101 public virtual ListSelectionMode SelectionMode {
102 get {
103 return (ListSelectionMode) ViewState.GetInt ("SelectionMode",
104 (int) ListSelectionMode.Single);
106 set {
107 if (!Enum.IsDefined (typeof (ListSelectionMode), value))
108 throw new ArgumentOutOfRangeException ("value");
109 ViewState ["SelectionMode"] = value;
114 public virtual int[] GetSelectedIndices ()
116 return (int []) GetSelectedIndicesInternal ().ToArray (typeof (int));
119 protected override void AddAttributesToRender (HtmlTextWriter writer)
121 if (Page != null)
122 Page.VerifyRenderingInServerForm (this);
124 if (ID != null)
125 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
127 if (AutoPostBack) {
128 string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
129 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
130 writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
133 if (SelectionMode == ListSelectionMode.Multiple)
134 writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
135 "multiple", false);
136 writer.AddAttribute (HtmlTextWriterAttribute.Size,
137 Rows.ToString (Helpers.InvariantCulture));
139 base.AddAttributesToRender (writer);
142 PostBackOptions GetPostBackOptions () {
143 PostBackOptions options = new PostBackOptions (this);
144 options.ActionUrl = null;
145 options.ValidationGroup = null;
146 options.Argument = String.Empty;
147 options.RequiresJavaScriptProtocol = false;
148 options.ClientSubmit = true;
149 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
150 if (options.PerformValidation)
151 options.ValidationGroup = ValidationGroup;
153 return options;
157 protected internal
158 override void OnPreRender (EventArgs e)
160 base.OnPreRender (e);
161 Page page = Page;
162 if (page != null && IsEnabled)
163 page.RegisterRequiresPostBack (this);
166 protected virtual
167 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
169 EnsureDataBound ();
170 string [] values = postCollection.GetValues (postDataKey);
171 if (values == null || values.Length == 0) {
172 int prev_index = SelectedIndex;
173 SelectedIndex = -1;
174 return (prev_index != -1);
176 ValidateEvent (UniqueID, values [0]);
178 if (SelectionMode == ListSelectionMode.Single)
179 return SelectSingle (values);
180 return SelectMultiple (values);
183 bool SelectSingle (string [] values)
185 string val = values [0];
186 int idx = Items.IndexOf (val);
187 int prev_index = SelectedIndex;
188 if (idx != prev_index) {
189 // This will set both the index value and the item.Selected property
190 SelectedIndex = idx;
191 return true;
193 return false;
196 bool SelectMultiple (string [] values)
198 ArrayList prev_selected = GetSelectedIndicesInternal ();
199 ClearSelection ();
200 foreach (string val in values) {
201 ListItem item = Items.FindByValue (val);
202 if (item != null)
203 item.Selected = true;
206 ArrayList new_selection = GetSelectedIndicesInternal ();
207 int i = prev_selected.Count;
208 if (new_selection.Count != i)
209 return true;
211 while (--i >= 0) {
212 if ((int) prev_selected [i] != (int) new_selection [i])
213 return true;
216 return false;
219 protected virtual
220 void RaisePostDataChangedEvent ()
222 if (CausesValidation)
223 Page.Validate (ValidationGroup);
224 OnSelectedIndexChanged (EventArgs.Empty);
227 bool IPostBackDataHandler.LoadPostData (string postDataKey,
228 NameValueCollection postCollection)
230 return LoadPostData (postDataKey, postCollection);
233 void IPostBackDataHandler.RaisePostDataChangedEvent ()
235 RaisePostDataChangedEvent ();
237 internal override bool MultiSelectOk ()
239 return this.SelectionMode == ListSelectionMode.Multiple;