2 // System.Web.UI.WebControls.ListControl.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.
31 using System.Web.Util;
32 using System.Collections;
33 using System.Globalization;
34 using System.ComponentModel;
35 using System.Collections.Specialized;
37 namespace System.Web.UI.WebControls {
39 [DataBindingHandler("System.Web.UI.Design.WebControls.ListControlDataBindingHandler, " + Consts.AssemblySystem_Design)]
40 [DefaultEventAttribute ("SelectedIndexChanged")]
42 [DefaultPropertyAttribute ("DataSource")]
44 [Designer("System.Web.UI.Design.WebControls.ListControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
46 [ControlValueProperty ("SelectedValue", null)]
47 [ParseChildrenAttribute (true, "Items", ChildControlType = typeof (Control))]
49 [ParseChildrenAttribute (true, "Items")]
51 public abstract class ListControl :
53 DataBoundControl, IEditableTextControl, ITextControl
59 private static readonly object SelectedIndexChangedEvent = new object ();
61 private static readonly object TextChangedEvent = new object ();
64 private object data_source;
65 private ListItemCollection items;
67 public ListControl () : base (HtmlTextWriterTag.Select)
72 [DefaultValue (false)]
75 [WebSysDescription ("")]
76 [WebCategory ("Behavior")]
77 public virtual bool AppendDataBoundItems
80 return ViewState.GetBool ("AppendDataBoundItems", false);
83 ViewState ["AppendDataBoundItems"] = value;
92 [WebSysDescription ("")]
93 [WebCategory ("Behavior")]
94 public virtual bool AutoPostBack {
95 get { return ViewState.GetBool ("AutoPostBack", false); }
96 set { ViewState ["AutoPostBack"] = value; }
101 [WebSysDescription ("")]
102 [WebCategory ("Data")]
103 public virtual string DataMember {
104 get { return ViewState.GetString ("DataMember", String.Empty); }
105 set { ViewState ["DataMember"] = value; }
110 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
111 [WebSysDescription ("")]
112 [WebCategory ("Data")]
113 public virtual object DataSource {
114 get { return data_source; }
115 set { data_source = value; }
123 [WebSysDescription ("")]
124 [WebCategory ("Data")]
125 public virtual string DataTextField {
126 get { return ViewState.GetString ("DataTextField", String.Empty); }
127 set { ViewState ["DataTextField"] = value; }
134 [WebSysDescription ("")]
135 [WebCategory ("Data")]
136 public virtual string DataTextFormatString {
137 get { return ViewState.GetString ("DataTextFormatString", String.Empty); }
138 set { ViewState ["DataTextFormatString"] = value; }
145 [WebSysDescription ("")]
146 [WebCategory ("Data")]
147 public virtual string DataValueField {
148 get { return ViewState.GetString ("DataValueField", String.Empty); }
149 set { ViewState ["DataValueField"] = value; }
153 [Editor ("System.Web.UI.Design.WebControls.ListItemsCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
156 [MergableProperty(false)]
157 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
158 [WebSysDescription ("")]
159 [WebCategory ("Misc")]
160 public virtual ListItemCollection Items {
163 items = new ListItemCollection ();
168 // I can't find this info stored in the viewstate anywhere
169 // so it must be calculated on the fly.
173 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
177 [WebSysDescription ("")]
178 [WebCategory ("Misc")]
179 public virtual int SelectedIndex {
183 for (int i = 0; i < items.Count; i++) {
184 if (items [i].Selected)
191 if (value == -1 || items == null)
193 if (value < 0 || value >= Items.Count)
194 throw new ArgumentOutOfRangeException ("value");
195 items [value].Selected = true;
201 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
202 [WebSysDescription ("")]
203 [WebCategory ("Misc")]
204 public virtual ListItem SelectedItem {
206 int si = SelectedIndex;
214 [Bindable(true, BindingDirection.TwoWay)]
221 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
222 [WebSysDescription ("")]
223 [WebCategory ("Misc")]
224 public virtual string SelectedValue {
226 int si = SelectedIndex;
229 return Items [si].Value;
233 for (int i = 0; i < Items.Count; i++) {
234 if (Items [i].Value == value)
235 Items [i].Selected = true;
243 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
246 [WebSysDescription ("")]
247 [WebCategoryAttribute ("Behavior")]
248 public virtual string Text
251 throw new NotImplementedException ();
254 throw new NotImplementedException ();
258 protected virtual new HtmlTextWriterTag TagKey
261 return HtmlTextWriterTag.Select;
265 protected override void AddAttributesToRender (HtmlTextWriter w)
267 base.AddAttributesToRender (w);
272 public virtual void ClearSelection ()
277 int count = Items.Count;
278 for (int i = 0; i<count; i++)
279 items [i].Selected = false;
284 protected internal override void LoadControlState (object savedState)
286 throw new NotImplementedException ();
290 protected override void OnDataBinding (EventArgs e)
292 base.OnDataBinding (e);
294 IEnumerable list = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
300 foreach (object container in list) {
304 if (DataTextField != String.Empty) {
305 text = DataBinder.Eval (container,
306 DataTextField).ToString ();
311 if (DataValueField != String.Empty) {
312 value = DataBinder.Eval (container,
313 DataValueField).ToString ();
318 if (text == String.Empty) {
319 if (value != String.Empty)
321 } else if (DataTextFormatString != String.Empty) {
322 // Dont apply the format string if we don't actually
324 text = String.Format (DataTextFormatString, text);
327 ListItem item = new ListItem (text, value);
334 protected internal override void OnInit (EventArgs e)
336 throw new NotImplementedException ();
345 override void OnPreRender (EventArgs e)
347 base.OnPreRender (e);
352 protected virtual void OnTextChanged (EventArgs e)
354 throw new NotImplementedException ();
358 protected internal override void PerformDataBinding (IEnumerable dataSource)
360 throw new NotImplementedException ();
364 protected override void PerformSelect ()
366 throw new NotImplementedException ();
370 protected internal override void RenderContents (HtmlTextWriter w)
372 throw new NotImplementedException ();
376 protected internal override object SaveControlState ()
378 throw new NotImplementedException ();
382 protected override object SaveViewState ()
385 object second = null;
386 ArrayList selected = null;
388 first = base.SaveViewState ();
390 IStateManager manager = items as IStateManager;
392 second = manager.SaveViewState ();
395 selected = new ArrayList ();
396 int count = Items.Count;
397 for (int i = 0; i < count; i++) {
398 if (items [i].Selected)
403 if (first == null && second == null && selected == null)
406 return new Triplet (first, second, selected);
409 protected override void LoadViewState (object savedState)
412 object second = null;
413 ArrayList indices = null;
415 Triplet triplet = savedState as Triplet;
416 if (triplet != null) {
417 first = triplet.First;
418 second = triplet.Second;
419 indices = triplet.Third as ArrayList;
422 base.LoadViewState (first);
424 if (second != null) {
425 IStateManager manager = Items as IStateManager;
426 manager.LoadViewState (second);
429 if (indices != null) {
430 foreach (int index in indices)
431 Items [index].Selected = true;
437 protected void SetPostDataSelection (int selectedIndex)
439 throw new NotImplementedException ();
443 protected override void TrackViewState ()
445 base.TrackViewState ();
446 IStateManager manager = items as IStateManager;
448 manager.TrackViewState ();
451 protected virtual void OnSelectedIndexChanged (EventArgs e)
453 EventHandler handler = (EventHandler) Events [SelectedIndexChangedEvent];
459 protected internal virtual void VerifyMultiSelect ()
464 [WebSysDescription ("")]
465 [WebCategory ("Action")]
466 public event EventHandler SelectedIndexChanged {
467 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
468 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
472 /* sealed in the docs */
473 public event EventHandler TextChanged {
475 Events.AddHandler (TextChangedEvent, value);
478 Events.RemoveHandler (TextChangedEvent, value);
485 [DefaultValue (false)]
486 [WebSysDescription ("")]
487 [WebCategory ("Behavior")]
488 public virtual bool CausesValidation {
490 throw new NotImplementedException ();
493 throw new NotImplementedException ();
500 [WebSysDescription ("")]
501 [WebCategoryAttribute ("Behavior")]
502 public virtual string ValidationGroup {
504 throw new NotImplementedException ();
507 throw new NotImplementedException ();