ListItem.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // System.Web.UI.WebControls.ListItem.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.ComponentModel;
  36. namespace System.Web.UI.WebControls
  37. {
  38. [TypeConverter(typeof(ExpandableObjectConverter))]
  39. [ControlBuilder(typeof(ListItemControlBuilder))]
  40. public sealed class ListItem : IStateManager, IParserAccessor, IAttributeAccessor
  41. {
  42. private AttributeCollection attributes;
  43. private string text;
  44. private string val;
  45. private bool marked;
  46. private bool selected;
  47. private bool dirty_t;
  48. private bool dirty_v;
  49. public ListItem(string text, string value)
  50. {
  51. this.text = text;
  52. this.val = value;
  53. attributes = null;
  54. }
  55. public ListItem(string text): this(text, null)
  56. {
  57. }
  58. public ListItem(): this(null, null)
  59. {
  60. }
  61. public static ListItem FromString(string text)
  62. {
  63. return new ListItem(text);
  64. }
  65. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  66. public AttributeCollection Attributes
  67. {
  68. get
  69. {
  70. if(attributes == null)
  71. attributes = new AttributeCollection(new StateBag(true));
  72. return attributes;
  73. }
  74. }
  75. [DefaultValue (false)]
  76. public bool Selected
  77. {
  78. get {
  79. return selected;
  80. }
  81. set {
  82. selected = value;
  83. }
  84. }
  85. internal bool Dirty
  86. {
  87. get {
  88. return (dirty_t && dirty_v);
  89. }
  90. set {
  91. dirty_t = value;
  92. dirty_v = value;
  93. }
  94. }
  95. [DefaultValue ("")]
  96. [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
  97. public string Text
  98. {
  99. get
  100. {
  101. if(text!=null)
  102. {
  103. return text;
  104. }
  105. if(val!=null)
  106. {
  107. return val;
  108. }
  109. return String.Empty;
  110. }
  111. set
  112. {
  113. text = value;
  114. if (IsTrackingViewState)
  115. dirty_t = true;
  116. }
  117. }
  118. [DefaultValue ("")]
  119. public string Value
  120. {
  121. get {
  122. if (val != null)
  123. return val;
  124. if (text != null)
  125. return text;
  126. return String.Empty;
  127. }
  128. set
  129. {
  130. val = value;
  131. if(IsTrackingViewState)
  132. dirty_v = true;
  133. }
  134. }
  135. string IAttributeAccessor.GetAttribute (string key)
  136. {
  137. if (attributes == null)
  138. return null;
  139. return attributes [key];
  140. }
  141. void IAttributeAccessor.SetAttribute (string key, string value)
  142. {
  143. Attributes [key] = value;
  144. }
  145. /// <remarks>
  146. /// The data is parsed - object must be of type LiteralControl or DataBoundLiteralControl.
  147. /// In latter case, throw an exception telling that the data cannot be bind-ed.
  148. /// </remarks>
  149. void IParserAccessor.AddParsedSubObject(object obj)
  150. {
  151. if(obj is LiteralControl)
  152. {
  153. Text = ((LiteralControl)obj).Text;
  154. return;
  155. }
  156. if(obj is DataBoundLiteralControl)
  157. {
  158. throw new HttpException(HttpRuntime.FormatResourceString("Control_Cannot_DataBind","ListItem"));
  159. }
  160. throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Have_Children_Of_Type", "ListItem", obj.GetType().ToString()));
  161. }
  162. bool IsTrackingViewState
  163. {
  164. get
  165. {
  166. return marked;
  167. }
  168. }
  169. internal void TrackViewState()
  170. {
  171. marked = true;
  172. }
  173. internal void LoadViewState(object state)
  174. {
  175. if(state is Pair)
  176. {
  177. Pair tv = (Pair)state;
  178. if(tv.First!=null)
  179. {
  180. Text = (string)tv.First;
  181. }
  182. if(tv.Second!=null)
  183. {
  184. Value = (string)tv.Second;
  185. }
  186. }
  187. }
  188. internal object SaveViewState()
  189. {
  190. if (dirty_t && dirty_v)
  191. return new Pair(Text, Value);
  192. if (dirty_t)
  193. return new Pair (Text, null);
  194. if (dirty_v)
  195. return new Pair(null, Value);
  196. return null;
  197. }
  198. public override bool Equals (object o)
  199. {
  200. ListItem li = o as ListItem;
  201. if (li == null)
  202. return false;
  203. return (Text == li.Text && Value == li.Value);
  204. }
  205. public override int GetHashCode ()
  206. {
  207. return base.GetHashCode ();
  208. }
  209. public override string ToString ()
  210. {
  211. return Text;
  212. }
  213. bool IStateManager.IsTrackingViewState
  214. {
  215. get
  216. {
  217. return IsTrackingViewState;
  218. }
  219. }
  220. void IStateManager.TrackViewState()
  221. {
  222. TrackViewState();
  223. }
  224. object IStateManager.SaveViewState()
  225. {
  226. return SaveViewState();
  227. }
  228. void IStateManager.LoadViewState(object state)
  229. {
  230. LoadViewState(state);
  231. }
  232. }
  233. }