ListItem.cs 4.3 KB

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