BaseDataList.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: BaseDataList
  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.ComponentModel;
  15. using System.Collections;
  16. using System.Web;
  17. using System.Web.UI;
  18. namespace System.Web.UI.WebControls
  19. {
  20. [DefaultEvent("SelectedIndexChanged")]
  21. [DefaultProperty("DataSource")]
  22. [ParseChildren(true)]
  23. [PersistChildren(false)]
  24. //TODO: [Designer("??")]
  25. public abstract class BaseDataList: WebControl
  26. {
  27. private static readonly object SelectedIndexChangedEvent = new object();
  28. internal static string ItemCountViewStateKey = "_!ItemCount";
  29. private DataKeyCollection dataKeys;
  30. private object dataSource;
  31. public BaseDataList() : base()
  32. {
  33. }
  34. public static bool IsBindableType(Type type)
  35. {
  36. if(type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal))
  37. return true;
  38. return false;
  39. }
  40. public override ControlCollection Controls
  41. {
  42. get
  43. {
  44. EnsureChildControls();
  45. return base.Controls;
  46. }
  47. }
  48. public override void DataBind()
  49. {
  50. OnDataBinding(EventArgs.Empty);
  51. }
  52. [WebCategory("Action")]
  53. [WebSysDescription("BaseDataList_OnSelectedIndexChanged")]
  54. public event EventHandler SelectedIndexChanged
  55. {
  56. add
  57. {
  58. Events.AddHandler(SelectedIndexChangedEvent, value);
  59. }
  60. remove
  61. {
  62. Events.RemoveHandler(SelectedIndexChangedEvent, value);
  63. }
  64. }
  65. [Bindable(true)]
  66. [DefaultValue(-1)]
  67. [WebCategory("Layout")]
  68. [WebSysDescription("BaseDataList_CellPadding")]
  69. public virtual int CellPadding
  70. {
  71. get
  72. {
  73. if(!ControlStyleCreated)
  74. return -1;
  75. return ((TableStyle)ControlStyle).CellPadding;
  76. }
  77. set
  78. {
  79. ((TableStyle)ControlStyle).CellPadding = value;
  80. }
  81. }
  82. [Bindable(true)]
  83. [DefaultValue(-1)]
  84. [WebCategory("Layout")]
  85. [WebSysDescription("BaseDataList_CellSpacing")]
  86. public virtual int CellSpacing
  87. {
  88. get
  89. {
  90. if(!ControlStyleCreated)
  91. return -1;
  92. return ((TableStyle)ControlStyle).CellSpacing;
  93. }
  94. set
  95. {
  96. ((TableStyle)ControlStyle).CellSpacing = value;
  97. }
  98. }
  99. [DefaultValue("")]
  100. [WebCategory("Data")]
  101. [WebSysDescription("BaseDataList_DataKeyField")]
  102. public virtual string DataKeyField
  103. {
  104. get
  105. {
  106. object o = ViewState["DataKeyField"];
  107. if(o!=null)
  108. return (string)o;
  109. return String.Empty;
  110. }
  111. set
  112. {
  113. ViewState["DataKeyField"] = value;
  114. }
  115. }
  116. [Browsable(true)]
  117. //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  118. [WebSysDescription("BaseDataList_DataKeys")]
  119. public DataKeyCollection DataKeys
  120. {
  121. get
  122. {
  123. if( dataKeys==null )
  124. dataKeys = new DataKeyCollection(DataKeysArray);
  125. return dataKeys;
  126. }
  127. }
  128. [DefaultValue("")]
  129. [WebCategory("Data")]
  130. [WebSysDescription("BaseDataList_DataMember")]
  131. public string DataMember
  132. {
  133. get
  134. {
  135. object o = ViewState["DataMember"];
  136. if(o!=null)
  137. return (string)o;
  138. return String.Empty;
  139. }
  140. set
  141. {
  142. ViewState["DataMember"] = value;
  143. }
  144. }
  145. [Bindable(true)]
  146. //[DefaultValue(null)]
  147. //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  148. [WebCategory("Data")]
  149. [WebSysDescription("BaseDataList_DataSource")]
  150. public virtual object DataSource
  151. {
  152. get
  153. {
  154. return dataSource;
  155. }
  156. set
  157. {
  158. if( (value!=null) && (value is IListSource || value is IEnumerable) )
  159. {
  160. dataSource = value;
  161. } else
  162. {
  163. throw new ArgumentException(HttpRuntime.FormatResourceString("Invalid_DataSource_Type", ID));
  164. }
  165. }
  166. }
  167. [Bindable(true)]
  168. [DefaultValue(GridLines.Both)]
  169. [WebCategory("Appearance")]
  170. [WebSysDescription("BaseDataList_GridLines")]
  171. public virtual GridLines GridLines
  172. {
  173. get
  174. {
  175. if(ControlStyleCreated)
  176. return ((TableStyle)ControlStyle).GridLines;
  177. return GridLines.Both;
  178. }
  179. set
  180. {
  181. ((TableStyle)ControlStyle).GridLines = value;
  182. }
  183. }
  184. [Bindable(true)]
  185. [DefaultValue(HorizontalAlign.NotSet)]
  186. [WebCategory("Layout")]
  187. [WebSysDescription("BaseDataList_HorizontalAlign")]
  188. public virtual HorizontalAlign HorizontalAlign
  189. {
  190. get
  191. {
  192. if(ControlStyleCreated)
  193. return ((TableStyle)ControlStyle).HorizontalAlign;
  194. return HorizontalAlign.NotSet;
  195. }
  196. set
  197. {
  198. ((TableStyle)ControlStyle).HorizontalAlign = value;
  199. }
  200. }
  201. protected ArrayList DataKeysArray
  202. {
  203. get
  204. {
  205. object o = ViewState["DataKeys"];
  206. if(o == null)
  207. {
  208. o = new ArrayList();
  209. ViewState["DataKeys"] = o;
  210. }
  211. return (ArrayList)o;
  212. }
  213. }
  214. protected override void AddParsedSubObject(object o)
  215. {
  216. // Preventing literal controls from being added as children.
  217. }
  218. protected override void CreateChildControls()
  219. {
  220. Controls.Clear();
  221. if(ViewState[ItemCountViewStateKey]!=null)
  222. {
  223. CreateControlHierarchy(true);
  224. ClearChildViewState();
  225. }
  226. }
  227. protected override void OnDataBinding(EventArgs e)
  228. {
  229. base.OnDataBinding(e);
  230. Controls.Clear();
  231. ClearChildViewState();
  232. CreateControlHierarchy(true);
  233. ChildControlsCreated = true;
  234. TrackViewState();
  235. }
  236. protected virtual void OnSelectedIndexChanged(EventArgs e)
  237. {
  238. if(Events != null)
  239. {
  240. EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
  241. if(eh!=null)
  242. eh(this, e);
  243. }
  244. }
  245. protected override void Render(HtmlTextWriter writer)
  246. {
  247. PrepareControlHierarchy();
  248. base.RenderContents(writer);
  249. }
  250. protected abstract void PrepareControlHierarchy();
  251. protected abstract void CreateControlHierarchy(bool useDataSource);
  252. }
  253. }