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")]
44 [SupportsEventValidation
]
45 public class ListBox
: ListControl
, IPostBackDataHandler
{
52 #if HAVE_CONTROL_ADAPTERS
58 get { return base.BorderColor; }
59 set { base.BorderColor = value; }
63 #if HAVE_CONTROL_ADAPTERS
68 BorderStyle BorderStyle
{
69 get { return base.BorderStyle; }
70 set { base.BorderStyle = value; }
74 #if HAVE_CONTROL_ADAPTERS
80 get { return base.BorderWidth; }
81 set { base.BorderWidth = value; }
85 [WebSysDescription ("")]
86 [WebCategory ("Appearance")]
87 public virtual int Rows
{
89 return ViewState
.GetInt ("Rows", 4);
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
{
103 return (ListSelectionMode
) ViewState
.GetInt ("SelectionMode",
104 (int) ListSelectionMode
.Single
);
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
)
122 Page
.VerifyRenderingInServerForm (this);
125 writer
.AddAttribute (HtmlTextWriterAttribute
.Name
, UniqueID
);
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
,
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
;
158 override void OnPreRender (EventArgs e
)
160 base.OnPreRender (e
);
162 if (page
!= null && IsEnabled
)
163 page
.RegisterRequiresPostBack (this);
167 bool LoadPostData (string postDataKey
, NameValueCollection postCollection
)
170 string [] values
= postCollection
.GetValues (postDataKey
);
171 if (values
== null || values
.Length
== 0) {
172 int prev_index
= SelectedIndex
;
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
196 bool SelectMultiple (string [] values
)
198 ArrayList prev_selected
= GetSelectedIndicesInternal ();
200 foreach (string val
in values
) {
201 ListItem item
= Items
.FindByValue (val
);
203 item
.Selected
= true;
206 ArrayList new_selection
= GetSelectedIndicesInternal ();
207 int i
= prev_selected
.Count
;
208 if (new_selection
.Count
!= i
)
212 if ((int) prev_selected
[i
] != (int) new_selection
[i
])
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
;