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.
35 using System
.Collections
;
36 using System
.Collections
.Specialized
;
37 using System
.ComponentModel
;
38 using System
.Globalization
;
42 namespace System
.Web
.UI
.WebControls
44 [ValidationProperty("SelectedItem")]
45 public class ListBox
: ListControl
, IPostBackDataHandler
47 public ListBox () : base ()
52 public override Color BorderColor
54 get { return base.BorderColor; }
55 set { base.BorderColor = value; }
59 public override BorderStyle BorderStyle
61 get { return base.BorderStyle; }
62 set { base.BorderStyle = value; }
66 public override Unit BorderWidth
68 get { return base.BorderWidth; }
69 set { base.BorderWidth = value; }
73 [DefaultValue (4), Bindable (true), WebCategory ("Appearance")]
74 [WebSysDescription ("The number of rows displayed by the control.")]
75 public virtual int Rows
78 object o
= ViewState
["Rows"];
79 return (o
== null) ? 4 : (int) o
;
83 if (value < 1 || value > 2000)
84 throw new ArgumentOutOfRangeException ("value", "Rows value has to be >= 0 and <= 2000.");
86 ViewState
["Rows"] = value;
90 [DefaultValue (typeof (ListSelectionMode
), "Single"), WebCategory ("Behavior")]
91 [WebSysDescription ("The mode describing how the entries can be selected.")]
92 public virtual ListSelectionMode SelectionMode
96 object o
= ViewState
["SelectionMode"];
97 return (o
== null) ? ListSelectionMode
.Single
: (ListSelectionMode
) o
;
101 if (!Enum
.IsDefined (typeof (ListSelectionMode
), value))
102 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
103 ViewState
["SelectionMode"] = value;
107 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
108 [Bindable (false), EditorBrowsable (EditorBrowsableState
.Never
)]
109 public override string ToolTip
111 get { return String.Empty; }
112 set { /* Don't do anything. */ }
115 protected override void AddAttributesToRender (HtmlTextWriter writer
)
118 Page
.VerifyRenderingInServerForm (this);
120 writer
.AddAttribute (HtmlTextWriterAttribute
.Name
, UniqueID
);
121 base.AddAttributesToRender (writer
);
122 writer
.AddAttribute (HtmlTextWriterAttribute
.Size
,
123 Rows
.ToString (NumberFormatInfo
.InvariantInfo
));
125 if (SelectionMode
== ListSelectionMode
.Multiple
)
126 writer
.AddAttribute (HtmlTextWriterAttribute
.Multiple
, "multiple");
128 if (AutoPostBack
&& Page
!= null){
129 writer
.AddAttribute (HtmlTextWriterAttribute
.Onchange
,
130 Page
.GetPostBackClientEvent (this, ""));
131 writer
.AddAttribute ("language", "javascript");
135 protected override void OnPreRender (EventArgs e
)
137 base.OnPreRender (e
);
138 if (Page
!= null && SelectionMode
== ListSelectionMode
.Multiple
&& Enabled
)
139 Page
.RegisterRequiresPostBack(this);
142 protected override void RenderContents (HtmlTextWriter writer
)
144 bool isMultAllowed
= (SelectionMode
== ListSelectionMode
.Multiple
);
145 bool selMade
= false;
146 foreach (ListItem current
in Items
){
147 writer
.WriteBeginTag ("option");
148 if (current
.Selected
){
149 if (!isMultAllowed
&& selMade
)
150 throw new HttpException ("Cannot_MultiSelect_In_Single_Mode");
152 writer
.WriteAttribute ("selected", "selected");
154 writer
.WriteAttribute ("value", current
.Value
, true);
156 writer
.Write (HttpUtility
.HtmlEncode (current
.Text
));
157 writer
.WriteEndTag ("option");
162 bool IPostBackDataHandler
.LoadPostData (string postDataKey
,
163 NameValueCollection postCollection
)
165 string[] vals
= postCollection
.GetValues (postDataKey
);
166 bool updated
= false;
168 if (SelectionMode
== ListSelectionMode
.Single
){
169 int index
= Items
.FindByValueInternal (vals
[0]);
170 if (SelectedIndex
!= index
){
171 SelectedIndex
= index
;
175 ArrayList indices
= SelectedIndices
;
176 int length
= vals
.Length
;
177 ArrayList final
= new ArrayList (length
);
178 foreach (string current
in vals
)
179 final
.Add (Items
.FindByValueInternal (current
));
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)
202 void IPostBackDataHandler
.RaisePostDataChangedEvent ()
204 OnSelectedIndexChanged (EventArgs
.Empty
);