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;
36 namespace System.Web.UI.WebControls {
39 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [ValidationProperty("SelectedItem")]
44 [SupportsEventValidation]
46 public class ListBox : ListControl, IPostBackDataHandler {
53 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
59 get { return base.BorderColor; }
60 set { base.BorderColor = value; }
64 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
69 BorderStyle BorderStyle {
70 get { return base.BorderStyle; }
71 set { base.BorderStyle = value; }
75 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
81 get { return base.BorderWidth; }
82 set { base.BorderWidth = value; }
89 [WebSysDescription ("")]
90 [WebCategory ("Appearance")]
91 public virtual int Rows {
93 return ViewState.GetInt ("Rows", 4);
96 if (value < 1 || value > 2000)
97 throw new ArgumentOutOfRangeException ("value");
98 ViewState ["Rows"] = value;
102 [DefaultValue(ListSelectionMode.Single)]
103 [WebSysDescription ("")]
104 [WebCategory ("Behavior")]
105 public virtual ListSelectionMode SelectionMode {
107 return (ListSelectionMode) ViewState.GetInt ("SelectionMode",
108 (int) ListSelectionMode.Single);
111 if (!Enum.IsDefined (typeof (ListSelectionMode), value))
112 throw new ArgumentOutOfRangeException ("value");
113 ViewState ["SelectionMode"] = value;
120 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
121 [EditorBrowsable(EditorBrowsableState.Never)]
122 public override string ToolTip {
123 get { return String.Empty; }
124 set { /* Tooltip is always String.Empty */ }
129 public virtual int[] GetSelectedIndices ()
131 return (int []) GetSelectedIndicesInternal ().ToArray (typeof (int));
135 protected override void AddAttributesToRender (HtmlTextWriter writer)
138 Page.VerifyRenderingInServerForm (this);
142 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
144 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
149 string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
150 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
151 writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
153 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
154 BuildScriptAttribute ("onchange",
155 Page.ClientScript.GetPostBackClientHyperlink (this, "")));
159 if (SelectionMode == ListSelectionMode.Multiple)
160 writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
162 writer.AddAttribute (HtmlTextWriterAttribute.Size,
163 Rows.ToString (CultureInfo.InvariantCulture));
165 base.AddAttributesToRender (writer);
169 PostBackOptions GetPostBackOptions () {
170 PostBackOptions options = new PostBackOptions (this);
171 options.ActionUrl = null;
172 options.ValidationGroup = null;
173 options.Argument = "";
174 options.RequiresJavaScriptProtocol = false;
175 options.ClientSubmit = true;
176 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
177 if (options.PerformValidation)
178 options.ValidationGroup = ValidationGroup;
185 protected override void RenderContents (HtmlTextWriter writer)
187 foreach (ListItem item in Items) {
189 writer.AddAttribute (HtmlTextWriterAttribute.Selected, "selected", false);
191 writer.AddAttribute (HtmlTextWriterAttribute.Value, item.Value, true);
192 writer.RenderBeginTag (HtmlTextWriterTag.Option);
194 string encoded = HttpUtility.HtmlEncode (item.Text);
195 writer.Write (encoded);
196 writer.RenderEndTag ();
207 override void OnPreRender (EventArgs e)
209 base.OnPreRender (e);
210 if (Page != null && Enabled)
211 Page.RegisterRequiresPostBack (this);
217 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
222 string [] values = postCollection.GetValues (postDataKey);
223 if (values == null || values.Length == 0) {
224 int prev_index = SelectedIndex;
226 return (prev_index != -1);
229 if (SelectionMode == ListSelectionMode.Single)
230 return SelectSingle (values);
231 return SelectMultiple (values);
234 bool SelectSingle (string [] values)
236 string val = values [0];
237 int idx = Items.IndexOf (val);
238 int prev_index = SelectedIndex;
239 if (idx != prev_index) {
240 // This will set both the index value and the item.Selected property
247 bool SelectMultiple (string [] values)
249 ArrayList prev_selected = GetSelectedIndicesInternal ();
251 foreach (string val in values) {
252 ListItem item = Items.FindByValue (val);
254 item.Selected = true;
257 ArrayList new_selection = GetSelectedIndicesInternal ();
258 int i = prev_selected.Count;
259 if (new_selection.Count != i)
263 if ((int) prev_selected [i] != (int) new_selection [i])
273 void RaisePostDataChangedEvent ()
276 if (CausesValidation)
277 Page.Validate (ValidationGroup);
279 OnSelectedIndexChanged (EventArgs.Empty);
282 bool IPostBackDataHandler.LoadPostData (string postDataKey,
283 NameValueCollection postCollection)
285 return LoadPostData (postDataKey, postCollection);
288 void IPostBackDataHandler.RaisePostDataChangedEvent ()
290 RaisePostDataChangedEvent ();
293 protected internal override void VerifyMultiSelect()
295 //by default the ListControl will throw an exception in this method,
296 //therefor we should override the method if the class is supporting
297 //MultiSelect option.
298 if (this.SelectionMode != ListSelectionMode.Multiple)
299 throw new HttpException("Multiple select option is not supported");