ListItem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 static int MARKED = (0x01 << 0);
  25. private static int SELECTED = (0x01 << 1);
  26. private static int DIRTY_T = (0x01 << 2);
  27. private static int DIRTY_V = (0x01 << 3);
  28. private static int selBits;
  29. private AttributeCollection attributes;
  30. private string text;
  31. private string val;
  32. public ListItem(string text, string value)
  33. {
  34. this.text = text;
  35. this.val = value;
  36. selBits = 0x00;
  37. attributes = null;
  38. }
  39. public ListItem(string text): this(text, null)
  40. {
  41. }
  42. public ListItem(): this(null, null)
  43. {
  44. }
  45. public static ListItem FromString(string text)
  46. {
  47. return new ListItem(text);
  48. }
  49. public AttributeCollection Attributes
  50. {
  51. get
  52. {
  53. if(attributes == null)
  54. attributes = new AttributeCollection(new StateBag(true));
  55. return attributes;
  56. }
  57. }
  58. public bool Selected
  59. {
  60. get
  61. {
  62. return IsSet(SELECTED);
  63. }
  64. set
  65. {
  66. Set(SELECTED);
  67. }
  68. }
  69. internal bool Dirty
  70. {
  71. get
  72. {
  73. return (IsSet(DIRTY_T) && IsSet(DIRTY_V));
  74. }
  75. set
  76. {
  77. Set(DIRTY_T);
  78. Set(DIRTY_V);
  79. }
  80. }
  81. private bool IsSet(int bit)
  82. {
  83. return ( (selBits & bit) != 0x00 );
  84. }
  85. private void Set(int bit)
  86. {
  87. selBits |= bit;
  88. }
  89. public string Text
  90. {
  91. get
  92. {
  93. if(text!=null)
  94. {
  95. return text;
  96. }
  97. if(val!=null)
  98. {
  99. return val;
  100. }
  101. return String.Empty;
  102. }
  103. set
  104. {
  105. text = value;
  106. if(IsTrackingViewState)
  107. {
  108. Set(DIRTY_T);
  109. }
  110. }
  111. }
  112. public string Value
  113. {
  114. get
  115. {
  116. if(val!=null)
  117. {
  118. return val;
  119. }
  120. if(text!=null)
  121. {
  122. return text;
  123. }
  124. return String.Empty;
  125. }
  126. set
  127. {
  128. val = value;
  129. if(IsTrackingViewState)
  130. {
  131. Set(DIRTY_V);
  132. }
  133. }
  134. }
  135. string IAttributeAccessor.GetAttribute(string key)
  136. {
  137. return attributes[key];
  138. }
  139. void IAttributeAccessor.SetAttribute(string key, string value)
  140. {
  141. attributes[key] = value;
  142. }
  143. /// <remarks>
  144. /// The data is parsed - object must be of type LiteralControl or DataBoundLiteralControl.
  145. /// In latter case, throw an exception telling that the data cannot be bind-ed.
  146. /// </remarks>
  147. void IParserAccessor.AddParsedSubObject(object obj)
  148. {
  149. if(obj is LiteralControl)
  150. {
  151. Text = ((LiteralControl)obj).Text;
  152. return;
  153. }
  154. if(obj is DataBoundLiteralControl)
  155. {
  156. throw new HttpException(HttpRuntime.FormatResourceString("Control_Cannot_DataBind","ListItem"));
  157. }
  158. throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Have_Children_Of_Type", "ListItem", obj.GetType().ToString()));
  159. }
  160. bool IsTrackingViewState
  161. {
  162. get
  163. {
  164. return IsSet(MARKED);
  165. }
  166. }
  167. internal void TrackViewState()
  168. {
  169. Set(MARKED);
  170. }
  171. internal void LoadViewState(object state)
  172. {
  173. if(state is Pair)
  174. {
  175. Pair tv = (Pair)state;
  176. if(tv.First!=null)
  177. {
  178. Text = (string)tv.First;
  179. }
  180. if(tv.Second!=null)
  181. {
  182. Value = (string)tv.Second;
  183. }
  184. }
  185. if(state is string)
  186. {
  187. Text = (string)state;
  188. }
  189. }
  190. internal object SaveViewState()
  191. {
  192. if(IsSet(DIRTY_T) && IsSet(DIRTY_V))
  193. {
  194. return new Pair(Text, Value);
  195. }
  196. if(IsSet(DIRTY_T))
  197. {
  198. return Text;
  199. }
  200. if(IsSet(DIRTY_V))
  201. {
  202. return new Pair(null, Value);
  203. }
  204. return null;
  205. }
  206. bool IStateManager.IsTrackingViewState
  207. {
  208. get
  209. {
  210. return IsTrackingViewState;
  211. }
  212. }
  213. void IStateManager.TrackViewState()
  214. {
  215. TrackViewState();
  216. }
  217. object IStateManager.SaveViewState()
  218. {
  219. return SaveViewState();
  220. }
  221. void IStateManager.LoadViewState(object state)
  222. {
  223. LoadViewState(state);
  224. }
  225. }
  226. }