2 // System.Web.UI.WebControls.ListBox.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Drawing;
\r
35 using System.Collections;
\r
36 using System.Collections.Specialized;
\r
37 using System.ComponentModel;
\r
38 using System.Globalization;
\r
40 using System.Web.UI;
\r
42 namespace System.Web.UI.WebControls
\r
44 [ValidationProperty("SelectedItem")]
\r
45 public class ListBox : ListControl, IPostBackDataHandler
\r
47 public ListBox () : base ()
\r
52 public override Color BorderColor
\r
54 get { return base.BorderColor; }
\r
55 set { base.BorderColor = value; }
\r
59 public override BorderStyle BorderStyle
\r
61 get { return base.BorderStyle; }
\r
62 set { base.BorderStyle = value; }
\r
66 public override Unit BorderWidth
\r
68 get { return base.BorderWidth; }
\r
69 set { base.BorderWidth = value; }
\r
73 [DefaultValue (4), Bindable (true), WebCategory ("Appearance")]
74 [WebSysDescription ("The number of rows displayed by the control.")]
\r
75 public virtual int Rows
\r
78 object o = ViewState ["Rows"];
\r
79 return (o == null) ? 4 : (int) o;
\r
83 if (value < 1 || value > 2000)
\r
84 throw new ArgumentOutOfRangeException ("value", "Rows value has to be >= 0 and <= 2000.");
\r
86 ViewState ["Rows"] = value;
\r
90 [DefaultValue (typeof (ListSelectionMode), "Single"), WebCategory ("Behavior")]
91 [WebSysDescription ("The mode describing how the entries can be selected.")]
\r
92 public virtual ListSelectionMode SelectionMode
\r
96 object o = ViewState ["SelectionMode"];
\r
97 return (o == null) ? ListSelectionMode.Single : (ListSelectionMode) o;
\r
101 if (!Enum.IsDefined (typeof (ListSelectionMode), value))
\r
102 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
103 ViewState ["SelectionMode"] = value;
\r
107 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
108 [Bindable (false), EditorBrowsable (EditorBrowsableState.Never)]
\r
109 public override string ToolTip
\r
111 get { return String.Empty; }
\r
112 set { /* Don't do anything. */ }
\r
115 protected override void AddAttributesToRender (HtmlTextWriter writer)
\r
118 Page.VerifyRenderingInServerForm (this);
\r
120 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
\r
121 base.AddAttributesToRender (writer);
\r
122 writer.AddAttribute (HtmlTextWriterAttribute.Size,
\r
123 Rows.ToString (NumberFormatInfo.InvariantInfo));
\r
125 if (SelectionMode == ListSelectionMode.Multiple)
\r
126 writer.AddAttribute (HtmlTextWriterAttribute.Multiple, "multiple");
\r
128 if (AutoPostBack && Page != null){
\r
129 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
\r
130 Page.GetPostBackClientEvent (this, ""));
\r
131 writer.AddAttribute ("language", "javascript");
\r
135 protected override void OnPreRender (EventArgs e)
\r
137 base.OnPreRender (e);
\r
138 if (Page != null && SelectionMode == ListSelectionMode.Multiple && Enabled)
\r
139 Page.RegisterRequiresPostBack(this);
\r
142 protected override void RenderContents (HtmlTextWriter writer)
\r
144 bool isMultAllowed = (SelectionMode == ListSelectionMode.Multiple);
\r
145 bool selMade = false;
\r
146 foreach (ListItem current in Items){
\r
147 writer.WriteBeginTag ("option");
\r
148 if (current.Selected){
\r
149 if (!isMultAllowed && selMade)
\r
150 throw new HttpException ("Cannot_MultiSelect_In_Single_Mode");
\r
152 writer.WriteAttribute ("selected", "selected");
\r
154 writer.WriteAttribute ("value", current.Value, true);
\r
155 writer.Write ('>');
\r
156 writer.Write (HttpUtility.HtmlEncode (current.Text));
\r
157 writer.WriteEndTag ("option");
\r
158 writer.WriteLine ();
\r
162 bool IPostBackDataHandler.LoadPostData (string postDataKey,
\r
163 NameValueCollection postCollection)
\r
165 string[] vals = postCollection.GetValues (postDataKey);
\r
166 bool updated = false;
\r
168 if (SelectionMode == ListSelectionMode.Single){
\r
169 int index = Items.FindByValueInternal (vals [0]);
\r
170 if (SelectedIndex != index){
\r
171 SelectedIndex = index;
\r
175 ArrayList indices = SelectedIndices;
176 int length = vals.Length;
177 ArrayList final = new ArrayList (length);
178 foreach (string current in vals)
\r
179 final.Add (Items.FindByValueInternal (current));
\r
181 if (indices.Count == length) {
182 for (int ctr = 0; ctr < length; ctr++){
183 if (((int) final [ctr]) != ((int) indices [ctr])){
195 if (SelectedIndex != -1)
\r
196 SelectedIndex = -1;
\r
202 void IPostBackDataHandler.RaisePostDataChangedEvent ()
\r
204 OnSelectedIndexChanged (EventArgs.Empty);
\r