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)
114 object [] states = (object []) state;
116 if (states [0] != null) {
117 sb = new StateBag (true);
118 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];
128 if (states [4] != null)
129 enabled = (bool) states [4];
133 object IStateManager.SaveViewState ()
135 return SaveViewState ();
138 internal object SaveViewState ()
144 object [] state = new object [5];
146 object [] state = new object [4];
148 state [0] = sb != null ? sb.SaveViewState () : null;
149 state [1] = (object) text;
150 state [2] = (object) value;
151 state [3] = (object) selected;
153 state [4] = (object) enabled;
158 void IStateManager.TrackViewState ()
163 internal void TrackViewState ()
168 public override string ToString ()
174 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
175 public AttributeCollection Attributes {
181 sb = new StateBag (true);
183 sb.TrackViewState ();
186 return attrs = new AttributeCollection (sb);
190 bool IStateManager.IsTrackingViewState {
191 get { return tracking; }
194 [DefaultValue(false)]
195 public bool Selected {
196 get { return selected; }
205 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
224 public string Value {
241 internal void SetDirty ()
249 get { return enabled; }
258 internal bool HasAttributes {
259 get { return attrs != null && attrs.Count > 0; }
271 AttributeCollection attrs;