ListItem.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // System.Web.UI.WebControls.ListItem.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2005-2009 Novell, Inc (http://www.novell.com)
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. using System.Collections.Specialized;
  29. using System.ComponentModel;
  30. using System.Security.Permissions;
  31. namespace System.Web.UI.WebControls {
  32. // CAS - no inheritance demand required because the class is sealed
  33. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. // attributes
  35. [ControlBuilder(typeof(ListItemControlBuilder))]
  36. [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
  37. public sealed class ListItem : IAttributeAccessor, IParserAccessor, IStateManager
  38. {
  39. #if NET_2_0
  40. public ListItem (string text, string value, bool enabled) : this (text, value)
  41. {
  42. this.enabled = enabled;
  43. }
  44. #endif
  45. public ListItem (string text, string value)
  46. {
  47. this.text = text;
  48. this.value = value;
  49. }
  50. public ListItem (string text) : this (text, null)
  51. {
  52. }
  53. public ListItem () : this (null, null)
  54. {
  55. }
  56. public static ListItem FromString (string text)
  57. {
  58. return new ListItem (text);
  59. }
  60. public override bool Equals (object o)
  61. {
  62. ListItem li = o as ListItem;
  63. if (li == null)
  64. return false;
  65. return li.Text == Text && li.Value == Value;
  66. }
  67. public override int GetHashCode ()
  68. {
  69. return Text.GetHashCode () ^ Value.GetHashCode ();
  70. }
  71. string IAttributeAccessor.GetAttribute (string key)
  72. {
  73. if (attrs == null)
  74. return null;
  75. return (string) Attributes [key];
  76. }
  77. void IAttributeAccessor.SetAttribute (string key, string value)
  78. {
  79. Attributes [key] = value;
  80. }
  81. void IParserAccessor.AddParsedSubObject (object obj)
  82. {
  83. LiteralControl lc = obj as LiteralControl;
  84. if (lc == null) {
  85. // obj.GetType() will throw a NullRef if obj is null. That's fine according to the test.
  86. throw new HttpException ("'ListItem' cannot have children of type " + obj.GetType ());
  87. }
  88. Text = lc.Text;
  89. }
  90. void IStateManager.LoadViewState (object state)
  91. {
  92. LoadViewState (state);
  93. }
  94. internal void LoadViewState (object state)
  95. {
  96. if (state == null)
  97. return;
  98. object [] states = (object []) state;
  99. if (states [0] != null) {
  100. sb = new StateBag (true);
  101. sb.LoadViewState (states[0]);
  102. sb.SetDirty (true);
  103. }
  104. if (states [1] != null)
  105. text = (string) states [1];
  106. if (states [2] != null)
  107. value = (string) states [2];
  108. if (states [3] != null)
  109. selected = (bool) states [3];
  110. #if NET_2_0
  111. if (states [4] != null)
  112. enabled = (bool) states [4];
  113. #endif
  114. }
  115. object IStateManager.SaveViewState ()
  116. {
  117. return SaveViewState ();
  118. }
  119. internal object SaveViewState ()
  120. {
  121. if (!dirty)
  122. return null;
  123. #if NET_2_0
  124. object [] state = new object [5];
  125. #else
  126. object [] state = new object [4];
  127. #endif
  128. state [0] = sb != null ? sb.SaveViewState () : null;
  129. state [1] = (object) text;
  130. state [2] = (object) value;
  131. state [3] = (object) selected;
  132. #if NET_2_0
  133. state [4] = (object) enabled;
  134. #endif
  135. return state;
  136. }
  137. void IStateManager.TrackViewState ()
  138. {
  139. TrackViewState ();
  140. }
  141. internal void TrackViewState ()
  142. {
  143. tracking = true;
  144. if (sb != null) {
  145. sb.TrackViewState ();
  146. sb.SetDirty (true);
  147. }
  148. }
  149. public override string ToString ()
  150. {
  151. return Text;
  152. }
  153. [Browsable(false)]
  154. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  155. public AttributeCollection Attributes {
  156. get {
  157. if (attrs != null)
  158. return attrs;
  159. if (sb == null) {
  160. sb = new StateBag (true);
  161. if (tracking)
  162. sb.TrackViewState ();
  163. }
  164. return attrs = new AttributeCollection (sb);
  165. }
  166. }
  167. bool IStateManager.IsTrackingViewState {
  168. get { return tracking; }
  169. }
  170. [DefaultValue(false)]
  171. public bool Selected {
  172. get { return selected; }
  173. set {
  174. selected = value;
  175. if (tracking)
  176. SetDirty ();
  177. }
  178. }
  179. [DefaultValue("")]
  180. [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
  181. public string Text {
  182. get {
  183. string r = text;
  184. if (r == null)
  185. r = value;
  186. if (r == null)
  187. r = String.Empty;
  188. return r;
  189. }
  190. set {
  191. text = value;
  192. if (tracking)
  193. SetDirty ();
  194. }
  195. }
  196. [DefaultValue("")]
  197. public string Value {
  198. get {
  199. string r = value;
  200. if (r == null)
  201. r = text;
  202. if (r == null)
  203. r = String.Empty;
  204. return r;
  205. }
  206. set {
  207. this.value = value;
  208. if (tracking)
  209. SetDirty ();
  210. }
  211. }
  212. internal void SetDirty ()
  213. {
  214. dirty = true;
  215. }
  216. #if NET_2_0
  217. public bool Enabled
  218. {
  219. get { return enabled; }
  220. set {
  221. enabled = value;
  222. if (tracking)
  223. SetDirty ();
  224. }
  225. }
  226. #endif
  227. internal bool HasAttributes {
  228. get { return attrs != null && attrs.Count > 0; }
  229. }
  230. string text;
  231. string value;
  232. bool selected;
  233. bool dirty;
  234. #if NET_2_0
  235. bool enabled = true;
  236. #endif
  237. bool tracking;
  238. StateBag sb;
  239. AttributeCollection attrs;
  240. }
  241. }