2 // System.Web.UI.WebControls.Literal.cs
5 // Jackson Harper (jackson@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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;
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 {
40 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 [ValidationProperty("SelectedItem")]
45 [SupportsEventValidation]
47 public class ListBox : ListControl, IPostBackDataHandler {
54 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
60 get { return base.BorderColor; }
61 set { base.BorderColor = value; }
65 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
70 BorderStyle BorderStyle {
71 get { return base.BorderStyle; }
72 set { base.BorderStyle = value; }
76 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
82 get { return base.BorderWidth; }
83 set { base.BorderWidth = value; }
90 [WebSysDescription ("")]
91 [WebCategory ("Appearance")]
92 public virtual int Rows {
94 return ViewState.GetInt ("Rows", 4);
97 if (value < 1 || value > 2000)
98 throw new ArgumentOutOfRangeException ("value");
99 ViewState ["Rows"] = value;
103 [DefaultValue(ListSelectionMode.Single)]
104 [WebSysDescription ("")]
105 [WebCategory ("Behavior")]
106 public virtual ListSelectionMode SelectionMode {
108 return (ListSelectionMode) ViewState.GetInt ("SelectionMode",
109 (int) ListSelectionMode.Single);
112 if (!Enum.IsDefined (typeof (ListSelectionMode), value))
113 throw new ArgumentOutOfRangeException ("value");
114 ViewState ["SelectionMode"] = value;
121 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
122 [EditorBrowsable(EditorBrowsableState.Never)]
123 public override string ToolTip {
124 get { return String.Empty; }
125 set { /* Tooltip is always String.Empty */ }
130 public virtual int[] GetSelectedIndices ()
132 return (int []) GetSelectedIndicesInternal ().ToArray (typeof (int));
136 protected override void AddAttributesToRender (HtmlTextWriter writer)
139 Page.VerifyRenderingInServerForm (this);
143 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
145 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
150 string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
151 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
152 writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
154 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
155 BuildScriptAttribute ("onchange",
156 Page.ClientScript.GetPostBackClientHyperlink (this, "")));
160 if (SelectionMode == ListSelectionMode.Multiple)
161 writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
163 writer.AddAttribute (HtmlTextWriterAttribute.Size,
164 Rows.ToString (Helpers.InvariantCulture));
166 base.AddAttributesToRender (writer);
170 PostBackOptions GetPostBackOptions () {
171 PostBackOptions options = new PostBackOptions (this);
172 options.ActionUrl = null;
173 options.ValidationGroup = null;
174 options.Argument = String.Empty;
175 options.RequiresJavaScriptProtocol = false;
176 options.ClientSubmit = true;
177 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
178 if (options.PerformValidation)
179 options.ValidationGroup = ValidationGroup;
186 protected override void RenderContents (HtmlTextWriter writer)
188 foreach (ListItem item in Items) {
190 writer.AddAttribute (HtmlTextWriterAttribute.Selected, "selected", false);
192 writer.AddAttribute (HtmlTextWriterAttribute.Value, item.Value, true);
193 writer.RenderBeginTag (HtmlTextWriterTag.Option);
195 string encoded = HttpUtility.HtmlEncode (item.Text);
196 writer.Write (encoded);
197 writer.RenderEndTag ();
208 override void OnPreRender (EventArgs e)
210 base.OnPreRender (e);
212 if (page != null && IsEnabled)
213 page.RegisterRequiresPostBack (this);
219 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
224 string [] values = postCollection.GetValues (postDataKey);
225 if (values == null || values.Length == 0) {
226 int prev_index = SelectedIndex;
228 return (prev_index != -1);
231 ValidateEvent (UniqueID, values [0]);
234 if (SelectionMode == ListSelectionMode.Single)
235 return SelectSingle (values);
236 return SelectMultiple (values);
239 bool SelectSingle (string [] values)
241 string val = values [0];
242 int idx = Items.IndexOf (val);
243 int prev_index = SelectedIndex;
244 if (idx != prev_index) {
245 // This will set both the index value and the item.Selected property
252 bool SelectMultiple (string [] values)
254 ArrayList prev_selected = GetSelectedIndicesInternal ();
256 foreach (string val in values) {
257 ListItem item = Items.FindByValue (val);
259 item.Selected = true;
262 ArrayList new_selection = GetSelectedIndicesInternal ();
263 int i = prev_selected.Count;
264 if (new_selection.Count != i)
268 if ((int) prev_selected [i] != (int) new_selection [i])
278 void RaisePostDataChangedEvent ()
281 if (CausesValidation)
282 Page.Validate (ValidationGroup);
284 OnSelectedIndexChanged (EventArgs.Empty);
287 bool IPostBackDataHandler.LoadPostData (string postDataKey,
288 NameValueCollection postCollection)
290 return LoadPostData (postDataKey, postCollection);
293 void IPostBackDataHandler.RaisePostDataChangedEvent ()
295 RaisePostDataChangedEvent ();
298 internal override bool MultiSelectOk ()
300 return this.SelectionMode == ListSelectionMode.Multiple;