2 // System.Web.UI.WebControls.ListItem.cs
5 // Ben Maurer (bmaurer@novell.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.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 public sealed class ListItem : IAttributeAccessor, IParserAccessor, IStateManager {
43 public ListItem (string text, string value, bool enabled) : this (text, value)
45 this.enabled = enabled;
48 public ListItem (string text, string value)
54 public ListItem (string text) : this (text, null)
58 public ListItem () : this (null, null)
62 public static ListItem FromString (string text)
64 return new ListItem (text);
67 public override bool Equals (object o)
69 ListItem li = o as ListItem;
72 return li.Text == Text && li.Value == Value;
75 public override int GetHashCode ()
77 return Text.GetHashCode () ^ Value.GetHashCode ();
81 string IAttributeAccessor.GetAttribute (string key)
86 return (string) Attributes [key];
89 void IAttributeAccessor.SetAttribute (string key, string value)
91 Attributes [key] = value;
94 void IParserAccessor.AddParsedSubObject (object obj)
96 LiteralControl lc = obj as LiteralControl;
98 // obj.GetType() will throw a NullRef if obj is null. That's fine according to the test.
99 throw new HttpException ("'ListItem' cannot have children of type " + obj.GetType ());
104 void IStateManager.LoadViewState (object state)
106 LoadViewState (state);
109 internal void LoadViewState (object state)
111 object [] states = (object []) state;
113 if (states [0] != null) sb = (StateBag) states[0];
114 if (states [1] != null) text = (string) states [1];
115 if (states [2] != null) value = (string) states [2];
117 if (states [3] != null) enabled = (bool) states [3];
121 object IStateManager.SaveViewState ()
123 return SaveViewState ();
126 internal object SaveViewState ()
129 object [] state = new object [4];
131 object [] state = new object [3];
134 state [1] = (object) text;
135 state [2] = (object) value;
137 state [3] = (object) enabled;
142 void IStateManager.TrackViewState ()
147 internal void TrackViewState ()
153 public override string ToString ()
160 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
161 public AttributeCollection Attributes {
167 sb = new StateBag (true);
169 sb.TrackViewState ();
172 return attrs = new AttributeCollection (sb);
176 bool IStateManager.IsTrackingViewState {
183 [DefaultValue(false)]
184 public bool Selected {
195 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
212 public string Value {
230 get { return enabled; }
231 set { enabled = value; }
234 internal bool HasAttributes {
235 get { return attrs != null && attrs.Count > 0; }
247 AttributeCollection attrs;