ListItem.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: ListItem
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.Web;
  16. using System.Web.UI;
  17. using System.ComponentModel;
  18. namespace System.Web.UI.WebControls
  19. {
  20. [TypeConverter(typeof(ExpandableObjectConverter))]
  21. [ControlBuilder(typeof(ListItemControlBuilder))]
  22. public sealed class ListItem : IStateManager, IParserAccessor, IAttributeAccessor
  23. {
  24. private AttributeCollection attributes;
  25. private string text;
  26. private string val;
  27. private bool marked;
  28. private bool selected;
  29. private bool dirty_t;
  30. private bool dirty_v;
  31. public ListItem(string text, string value)
  32. {
  33. this.text = text;
  34. this.val = value;
  35. attributes = null;
  36. }
  37. public ListItem(string text): this(text, null)
  38. {
  39. }
  40. public ListItem(): this(null, null)
  41. {
  42. }
  43. public static ListItem FromString(string text)
  44. {
  45. return new ListItem(text);
  46. }
  47. public AttributeCollection Attributes
  48. {
  49. get
  50. {
  51. if(attributes == null)
  52. attributes = new AttributeCollection(new StateBag(true));
  53. return attributes;
  54. }
  55. }
  56. public bool Selected
  57. {
  58. get {
  59. return selected;
  60. }
  61. set {
  62. selected = value;
  63. }
  64. }
  65. internal bool Dirty
  66. {
  67. get {
  68. return (dirty_t && dirty_v);
  69. }
  70. set {
  71. dirty_t = value;
  72. dirty_v = value;
  73. }
  74. }
  75. public string Text
  76. {
  77. get
  78. {
  79. if(text!=null)
  80. {
  81. return text;
  82. }
  83. if(val!=null)
  84. {
  85. return val;
  86. }
  87. return String.Empty;
  88. }
  89. set
  90. {
  91. text = value;
  92. if (IsTrackingViewState)
  93. dirty_t = true;
  94. }
  95. }
  96. public string Value
  97. {
  98. get {
  99. if (val != null)
  100. return val;
  101. if (text != null)
  102. return text;
  103. return String.Empty;
  104. }
  105. set
  106. {
  107. val = value;
  108. if(IsTrackingViewState)
  109. dirty_v = true;
  110. }
  111. }
  112. string IAttributeAccessor.GetAttribute(string key)
  113. {
  114. return attributes[key];
  115. }
  116. void IAttributeAccessor.SetAttribute(string key, string value)
  117. {
  118. attributes[key] = value;
  119. }
  120. /// <remarks>
  121. /// The data is parsed - object must be of type LiteralControl or DataBoundLiteralControl.
  122. /// In latter case, throw an exception telling that the data cannot be bind-ed.
  123. /// </remarks>
  124. void IParserAccessor.AddParsedSubObject(object obj)
  125. {
  126. if(obj is LiteralControl)
  127. {
  128. Text = ((LiteralControl)obj).Text;
  129. return;
  130. }
  131. if(obj is DataBoundLiteralControl)
  132. {
  133. throw new HttpException(HttpRuntime.FormatResourceString("Control_Cannot_DataBind","ListItem"));
  134. }
  135. throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Have_Children_Of_Type", "ListItem", obj.GetType().ToString()));
  136. }
  137. bool IsTrackingViewState
  138. {
  139. get
  140. {
  141. return marked;
  142. }
  143. }
  144. internal void TrackViewState()
  145. {
  146. marked = true;
  147. }
  148. internal void LoadViewState(object state)
  149. {
  150. if(state is Pair)
  151. {
  152. Pair tv = (Pair)state;
  153. if(tv.First!=null)
  154. {
  155. Text = (string)tv.First;
  156. }
  157. if(tv.Second!=null)
  158. {
  159. Value = (string)tv.Second;
  160. }
  161. }
  162. }
  163. internal object SaveViewState()
  164. {
  165. if (dirty_t && dirty_v)
  166. return new Pair(Text, Value);
  167. if (dirty_t)
  168. return new Pair (Text, null);
  169. if (dirty_v)
  170. return new Pair(null, Value);
  171. return null;
  172. }
  173. public override bool Equals (object o)
  174. {
  175. ListItem li = o as ListItem;
  176. if (li == null)
  177. return false;
  178. return (Text == li.Text && Value == li.Value);
  179. }
  180. public override int GetHashCode ()
  181. {
  182. return base.GetHashCode ();
  183. }
  184. public override string ToString ()
  185. {
  186. return Text;
  187. }
  188. bool IStateManager.IsTrackingViewState
  189. {
  190. get
  191. {
  192. return IsTrackingViewState;
  193. }
  194. }
  195. void IStateManager.TrackViewState()
  196. {
  197. TrackViewState();
  198. }
  199. object IStateManager.SaveViewState()
  200. {
  201. return SaveViewState();
  202. }
  203. void IStateManager.LoadViewState(object state)
  204. {
  205. LoadViewState(state);
  206. }
  207. }
  208. }