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 // 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.Collections;
\r
36 using System.Web.UI;
\r
37 using System.ComponentModel;
\r
39 namespace System.Web.UI.WebControls
\r
41 [TypeConverter(typeof(ExpandableObjectConverter))]
\r
42 [ControlBuilder(typeof(ListItemControlBuilder))]
\r
43 public sealed class ListItem : IStateManager, IParserAccessor, IAttributeAccessor
\r
45 private AttributeCollection attributes;
\r
46 private string text;
\r
48 private bool marked;
\r
49 private bool selected;
\r
50 private bool dirty_t;
\r
51 private bool dirty_v;
\r
53 public ListItem(string text, string value)
\r
60 public ListItem(string text): this(text, null)
\r
64 public ListItem(): this(null, null)
\r
68 public static ListItem FromString(string text)
\r
70 return new ListItem(text);
\r
73 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
\r
74 public AttributeCollection Attributes
\r
78 if(attributes == null)
\r
79 attributes = new AttributeCollection(new StateBag(true));
\r
84 [DefaultValue (false)]
\r
85 public bool Selected
\r
99 return (dirty_t && dirty_v);
\r
109 [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
\r
122 return String.Empty;
\r
127 if (IsTrackingViewState)
\r
132 [DefaultValue ("")]
\r
133 public string Value
\r
142 return String.Empty;
\r
147 if(IsTrackingViewState)
\r
152 string IAttributeAccessor.GetAttribute (string key)
154 if (attributes == null)
157 return attributes [key];
160 void IAttributeAccessor.SetAttribute (string key, string value)
162 Attributes [key] = value;
166 /// The data is parsed - object must be of type LiteralControl or DataBoundLiteralControl.
\r
167 /// In latter case, throw an exception telling that the data cannot be bind-ed.
\r
169 void IParserAccessor.AddParsedSubObject(object obj)
\r
171 if(obj is LiteralControl)
\r
173 Text = ((LiteralControl)obj).Text;
\r
176 if(obj is DataBoundLiteralControl)
\r
178 throw new HttpException(HttpRuntime.FormatResourceString("Control_Cannot_DataBind","ListItem"));
\r
180 throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Have_Children_Of_Type", "ListItem", obj.GetType().ToString()));
\r
183 bool IsTrackingViewState
\r
191 internal void TrackViewState()
\r
196 internal void LoadViewState(object state)
\r
200 Pair tv = (Pair)state;
\r
203 Text = (string)tv.First;
\r
205 if(tv.Second!=null)
\r
207 Value = (string)tv.Second;
\r
212 internal object SaveViewState()
\r
214 if (dirty_t && dirty_v)
\r
215 return new Pair(Text, Value);
\r
218 return new Pair (Text, null);
\r
221 return new Pair(null, Value);
\r
226 public override bool Equals (object o)
\r
228 ListItem li = o as ListItem;
\r
232 return (Text == li.Text && Value == li.Value);
\r
235 public override int GetHashCode ()
\r
237 return base.GetHashCode ();
\r
240 public override string ToString ()
\r
245 bool IStateManager.IsTrackingViewState
\r
249 return IsTrackingViewState;
\r
253 void IStateManager.TrackViewState()
\r
258 object IStateManager.SaveViewState()
\r
260 return SaveViewState();
\r
263 void IStateManager.LoadViewState(object state)
\r
265 LoadViewState(state);
\r