2 // System.Web.UI.WebControls.ListItem.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 using System.Collections;
\r
15 using System.Web.UI;
\r
16 using System.ComponentModel;
\r
18 namespace System.Web.UI.WebControls
\r
20 [TypeConverter(typeof(ExpandableObjectConverter))]
\r
21 [ControlBuilder(typeof(ListItemControlBuilder))]
\r
22 public sealed class ListItem : IStateManager, IParserAccessor, IAttributeAccessor
\r
24 private AttributeCollection attributes;
\r
25 private string text;
\r
27 private bool marked;
\r
28 private bool selected;
\r
29 private bool dirty_t;
\r
30 private bool dirty_v;
\r
32 public ListItem(string text, string value)
\r
39 public ListItem(string text): this(text, null)
\r
43 public ListItem(): this(null, null)
\r
47 public static ListItem FromString(string text)
\r
49 return new ListItem(text);
\r
52 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
\r
53 public AttributeCollection Attributes
\r
57 if(attributes == null)
\r
58 attributes = new AttributeCollection(new StateBag(true));
\r
63 [DefaultValue (false)]
\r
64 public bool Selected
\r
78 return (dirty_t && dirty_v);
\r
88 [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
\r
101 return String.Empty;
\r
106 if (IsTrackingViewState)
\r
111 [DefaultValue ("")]
\r
112 public string Value
\r
121 return String.Empty;
\r
126 if(IsTrackingViewState)
\r
131 string IAttributeAccessor.GetAttribute (string key)
133 if (attributes == null)
136 return attributes [key];
139 void IAttributeAccessor.SetAttribute (string key, string value)
141 Attributes [key] = value;
145 /// The data is parsed - object must be of type LiteralControl or DataBoundLiteralControl.
\r
146 /// In latter case, throw an exception telling that the data cannot be bind-ed.
\r
148 void IParserAccessor.AddParsedSubObject(object obj)
\r
150 if(obj is LiteralControl)
\r
152 Text = ((LiteralControl)obj).Text;
\r
155 if(obj is DataBoundLiteralControl)
\r
157 throw new HttpException(HttpRuntime.FormatResourceString("Control_Cannot_DataBind","ListItem"));
\r
159 throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Have_Children_Of_Type", "ListItem", obj.GetType().ToString()));
\r
162 bool IsTrackingViewState
\r
170 internal void TrackViewState()
\r
175 internal void LoadViewState(object state)
\r
179 Pair tv = (Pair)state;
\r
182 Text = (string)tv.First;
\r
184 if(tv.Second!=null)
\r
186 Value = (string)tv.Second;
\r
191 internal object SaveViewState()
\r
193 if (dirty_t && dirty_v)
\r
194 return new Pair(Text, Value);
\r
197 return new Pair (Text, null);
\r
200 return new Pair(null, Value);
\r
205 public override bool Equals (object o)
\r
207 ListItem li = o as ListItem;
\r
211 return (Text == li.Text && Value == li.Value);
\r
214 public override int GetHashCode ()
\r
216 return base.GetHashCode ();
\r
219 public override string ToString ()
\r
224 bool IStateManager.IsTrackingViewState
\r
228 return IsTrackingViewState;
\r
232 void IStateManager.TrackViewState()
\r
237 object IStateManager.SaveViewState()
\r
239 return SaveViewState();
\r
242 void IStateManager.LoadViewState(object state)
\r
244 LoadViewState(state);
\r