2
0

BaseDataList.cs 5.8 KB

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