2 // System.Web.UI.WebControls.ListItem.cs
5 // Ben Maurer (bmaurer@novell.com)
7 // (C) 2005-2009 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.Specialized;
30 using System.ComponentModel;
31 using System.Security.Permissions;
33 namespace System.Web.UI.WebControls {
35 // CAS - no inheritance demand required because the class is sealed
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [ControlBuilder(typeof(ListItemControlBuilder))]
39 [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
40 [ParseChildren (true, "Text")]
41 public sealed class ListItem : IAttributeAccessor, IParserAccessor, IStateManager
43 public ListItem (string text, string value, bool enabled) : this (text, value)
45 this.enabled = enabled;
47 public ListItem (string text, string value)
53 public ListItem (string text) : this (text, null)
57 public ListItem () : this (null, null)
61 public static ListItem FromString (string text)
63 return new ListItem (text);
66 public override bool Equals (object o)
68 ListItem li = o as ListItem;
71 return li.Text == Text && li.Value == Value;
74 public override int GetHashCode ()
76 return Text.GetHashCode () ^ Value.GetHashCode ();
80 string IAttributeAccessor.GetAttribute (string key)
85 return (string) Attributes [key];
88 void IAttributeAccessor.SetAttribute (string key, string value)
90 Attributes [key] = value;
93 void IParserAccessor.AddParsedSubObject (object obj)
95 LiteralControl lc = obj as LiteralControl;
97 // obj.GetType() will throw a NullRef if obj is null. That's fine according to the test.
98 throw new HttpException ("'ListItem' cannot have children of type " + obj.GetType ());
103 void IStateManager.LoadViewState (object state)
105 LoadViewState (state);
108 internal void LoadViewState (object state)
113 object [] states = (object []) state;
115 if (states [0] != null) {
116 sb = new StateBag (true);
117 sb.LoadViewState (states[0]);
121 if (states [1] != null)
122 text = (string) states [1];
123 if (states [2] != null)
124 value = (string) states [2];
125 if (states [3] != null)
126 selected = (bool) states [3];
127 if (states [4] != null)
128 enabled = (bool) states [4];
131 object IStateManager.SaveViewState ()
133 return SaveViewState ();
136 internal object SaveViewState ()
141 object [] state = new object [5];
142 state [0] = sb != null ? sb.SaveViewState () : null;
143 state [1] = (object) text;
144 state [2] = (object) value;
145 state [3] = (object) selected;
146 state [4] = (object) enabled;
150 void IStateManager.TrackViewState ()
155 internal void TrackViewState ()
159 sb.TrackViewState ();
164 public override string ToString ()
170 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
171 public AttributeCollection Attributes {
177 sb = new StateBag (true);
179 sb.TrackViewState ();
182 return attrs = new AttributeCollection (sb);
186 bool IStateManager.IsTrackingViewState {
187 get { return tracking; }
190 [TypeConverter ("System.Web.UI.MinimizableAttributeTypeConverter")]
191 [DefaultValue(false)]
192 public bool Selected {
193 get { return selected; }
203 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
223 public string Value {
240 internal void SetDirty ()
245 [DefaultValue (true)]
248 get { return enabled; }
256 internal bool HasAttributes {
257 get { return attrs != null && attrs.Count > 0; }
267 AttributeCollection attrs;