BaseDataList.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //
  2. // System.Web.UI.WebControls.BaseDataList.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2001)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. using System;
  12. using System.ComponentModel;
  13. using System.ComponentModel.Design;
  14. using System.Collections;
  15. using System.Web;
  16. using System.Web.UI;
  17. using System.Web.Util;
  18. namespace System.Web.UI.WebControls
  19. {
  20. [DefaultEvent("SelectedIndexChanged")]
  21. [DefaultProperty("DataSource")]
  22. [Designer("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  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 base.Controls;
  44. }
  45. }
  46. public override void DataBind()
  47. {
  48. #if NET_1_2
  49. RequiresDataBinding = false;
  50. #endif
  51. OnDataBinding(EventArgs.Empty);
  52. }
  53. [WebCategory("Action")]
  54. [WebSysDescription("BaseDataList_OnSelectedIndexChanged")]
  55. public event EventHandler SelectedIndexChanged
  56. {
  57. add
  58. {
  59. Events.AddHandler(SelectedIndexChangedEvent, value);
  60. }
  61. remove
  62. {
  63. Events.RemoveHandler(SelectedIndexChangedEvent, value);
  64. }
  65. }
  66. [Bindable(true)]
  67. [DefaultValue(-1)]
  68. [WebCategory("Layout")]
  69. [WebSysDescription("BaseDataList_CellPadding")]
  70. public virtual int CellPadding
  71. {
  72. get
  73. {
  74. if(!ControlStyleCreated)
  75. return -1;
  76. return ((TableStyle)ControlStyle).CellPadding;
  77. }
  78. set
  79. {
  80. ((TableStyle)ControlStyle).CellPadding = value;
  81. }
  82. }
  83. [Bindable(true)]
  84. [DefaultValue(-1)]
  85. [WebCategory("Layout")]
  86. [WebSysDescription("BaseDataList_CellSpacing")]
  87. public virtual int CellSpacing
  88. {
  89. get
  90. {
  91. if(!ControlStyleCreated)
  92. return -1;
  93. return ((TableStyle)ControlStyle).CellSpacing;
  94. }
  95. set
  96. {
  97. ((TableStyle)ControlStyle).CellSpacing = value;
  98. }
  99. }
  100. [DefaultValue("")]
  101. [WebCategory("Data")]
  102. [WebSysDescription("BaseDataList_DataKeyField")]
  103. public virtual string DataKeyField
  104. {
  105. get
  106. {
  107. object o = ViewState["DataKeyField"];
  108. if(o!=null)
  109. return (string)o;
  110. return String.Empty;
  111. }
  112. set
  113. {
  114. ViewState["DataKeyField"] = value;
  115. }
  116. }
  117. [Browsable(true)]
  118. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  119. [WebSysDescription("BaseDataList_DataKeys")]
  120. public DataKeyCollection DataKeys
  121. {
  122. get
  123. {
  124. if( dataKeys==null )
  125. dataKeys = new DataKeyCollection(DataKeysArray);
  126. return dataKeys;
  127. }
  128. }
  129. [DefaultValue("")]
  130. [WebCategory("Data")]
  131. [WebSysDescription("BaseDataList_DataMember")]
  132. public string DataMember
  133. {
  134. get
  135. {
  136. object o = ViewState["DataMember"];
  137. if(o!=null)
  138. return (string)o;
  139. return String.Empty;
  140. }
  141. set
  142. {
  143. ViewState["DataMember"] = value;
  144. }
  145. }
  146. [Bindable(true)]
  147. [DefaultValue(null)]
  148. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  149. [WebCategory("Data")]
  150. [WebSysDescription("BaseDataList_DataSource")]
  151. public virtual object DataSource
  152. {
  153. get
  154. {
  155. return dataSource;
  156. }
  157. set
  158. {
  159. if( (value!=null) && (value is IListSource || value is IEnumerable) )
  160. {
  161. dataSource = value;
  162. } else
  163. {
  164. throw new ArgumentException(HttpRuntime.FormatResourceString("Invalid_DataSource_Type", ID));
  165. }
  166. }
  167. }
  168. [Bindable(true)]
  169. [DefaultValue(GridLines.Both)]
  170. [WebCategory("Appearance")]
  171. [WebSysDescription("BaseDataList_GridLines")]
  172. public virtual GridLines GridLines
  173. {
  174. get
  175. {
  176. if(ControlStyleCreated)
  177. return ((TableStyle)ControlStyle).GridLines;
  178. return GridLines.Both;
  179. }
  180. set
  181. {
  182. ((TableStyle)ControlStyle).GridLines = value;
  183. }
  184. }
  185. // LAMESPEC HorizontalAlign has a Category attribute, this should obviously be a WebCategory attribute
  186. // but is defined incorrectly in the MS framework
  187. [Bindable(true)]
  188. [DefaultValue(HorizontalAlign.NotSet)]
  189. [Category("Layout")]
  190. [WebSysDescription("BaseDataList_HorizontalAlign")]
  191. public virtual HorizontalAlign HorizontalAlign
  192. {
  193. get
  194. {
  195. if(ControlStyleCreated)
  196. return ((TableStyle)ControlStyle).HorizontalAlign;
  197. return HorizontalAlign.NotSet;
  198. }
  199. set
  200. {
  201. ((TableStyle)ControlStyle).HorizontalAlign = value;
  202. }
  203. }
  204. protected ArrayList DataKeysArray
  205. {
  206. get
  207. {
  208. object o = ViewState["DataKeys"];
  209. if(o == null)
  210. {
  211. o = new ArrayList();
  212. ViewState["DataKeys"] = o;
  213. }
  214. return (ArrayList)o;
  215. }
  216. }
  217. protected override void AddParsedSubObject(object o)
  218. {
  219. // Preventing literal controls from being added as children.
  220. }
  221. protected override void CreateChildControls()
  222. {
  223. Controls.Clear();
  224. if(ViewState[ItemCountViewStateKey]!=null)
  225. {
  226. CreateControlHierarchy(false);
  227. ClearChildViewState();
  228. }
  229. }
  230. protected override void OnDataBinding(EventArgs e)
  231. {
  232. base.OnDataBinding(e);
  233. Controls.Clear();
  234. ClearChildViewState();
  235. CreateControlHierarchy(true);
  236. ChildControlsCreated = true;
  237. TrackViewState();
  238. }
  239. protected virtual void OnSelectedIndexChanged(EventArgs e)
  240. {
  241. if(Events != null)
  242. {
  243. EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
  244. if(eh!=null)
  245. eh(this, e);
  246. }
  247. }
  248. protected override void Render(HtmlTextWriter writer)
  249. {
  250. PrepareControlHierarchy();
  251. RenderContents(writer);
  252. }
  253. protected abstract void PrepareControlHierarchy();
  254. protected abstract void CreateControlHierarchy(bool useDataSource);
  255. #if NET_1_2
  256. // should be `internal protected' (why, oh WHY did they do that !?!)
  257. protected override void OnInit (EventArgs e)
  258. {
  259. base.OnInit(e);
  260. inited = true;
  261. if (!Page.IsPostBack)
  262. RequiresDataBinding = true;
  263. }
  264. // should be `internal protected' (why, oh WHY did they do that !?!)
  265. protected override void OnLoad (EventArgs e)
  266. {
  267. IDataSource ds = GetDataSourceObject () as IDataSource;
  268. if (ds != null && DataSourceID != "")
  269. ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
  270. base.OnLoad(e);
  271. }
  272. // should be `internal protected' (why, oh WHY did they do that !?!)
  273. protected override void OnPreRender (EventArgs e)
  274. {
  275. EnsureDataBound ();
  276. base.OnPreRender (e);
  277. }
  278. protected void EnsureDataBound ()
  279. {
  280. if (RequiresDataBinding && DataSourceID != "")
  281. DataBind ();
  282. }
  283. protected virtual object GetDataSourceObject ()
  284. {
  285. if (DataSourceID != "")
  286. return (IDataSource) NamingContainer.FindControl (DataSourceID);
  287. return DataSource;
  288. }
  289. protected virtual IEnumerable GetResolvedDataSource ()
  290. {
  291. if (DataSource != null && DataSourceID != "")
  292. throw new HttpException ();
  293. IDataSource ds = this.GetDataSourceObject () as IDataSource;
  294. if (ds != null && DataSourceID != "")
  295. return ds.GetView (DataMember).Select ();
  296. else if (DataSource != null)
  297. return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  298. else
  299. return null;
  300. }
  301. protected void OnDataSourceChanged (object sender, EventArgs e)
  302. {
  303. RequiresDataBinding = true;
  304. }
  305. public virtual string DataSourceID {
  306. get {
  307. object o = ViewState ["DataSourceID"];
  308. if (o != null)
  309. return (string)o;
  310. return String.Empty;
  311. }
  312. set {
  313. if (inited)
  314. RequiresDataBinding = true;
  315. ViewState ["DataSourceID"] = value;
  316. }
  317. }
  318. bool requiresDataBinding;
  319. protected bool RequiresDataBinding {
  320. get { return requiresDataBinding; }
  321. set { requiresDataBinding = value; }
  322. }
  323. protected bool inited;
  324. #else
  325. internal IEnumerable GetResolvedDataSource ()
  326. {
  327. if (DataSource != null)
  328. return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  329. else
  330. return null;
  331. }
  332. #endif
  333. }
  334. }