BaseDataList.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // System.Web.UI.WebControls.BaseDataList.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. // Sanjay Gupta ([email protected])
  8. //
  9. // (C) Gaurav Vaish (2001)
  10. // (C) 2003 Andreas Nahr
  11. // (C) 2004 Novell, Inc. (http://www.novell.com)
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.ComponentModel;
  35. using System.ComponentModel.Design;
  36. using System.Collections;
  37. using System.Web;
  38. using System.Web.UI;
  39. using System.Web.Util;
  40. namespace System.Web.UI.WebControls
  41. {
  42. [DefaultEvent("SelectedIndexChanged")]
  43. [DefaultProperty("DataSource")]
  44. [Designer("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  45. public abstract class BaseDataList: WebControl
  46. {
  47. private static readonly object SelectedIndexChangedEvent = new object();
  48. internal static string ItemCountViewStateKey = "_!ItemCount";
  49. private DataKeyCollection dataKeys;
  50. private object dataSource;
  51. public BaseDataList() : base()
  52. {
  53. }
  54. public static bool IsBindableType(Type type)
  55. {
  56. if(type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal))
  57. return true;
  58. return false;
  59. }
  60. public override ControlCollection Controls
  61. {
  62. get
  63. {
  64. EnsureChildControls();
  65. return base.Controls;
  66. }
  67. }
  68. public override void DataBind()
  69. {
  70. #if NET_2_0
  71. RequiresDataBinding = false;
  72. #endif
  73. OnDataBinding(EventArgs.Empty);
  74. }
  75. [WebCategory("Action")]
  76. [WebSysDescription("BaseDataList_OnSelectedIndexChanged")]
  77. public event EventHandler SelectedIndexChanged
  78. {
  79. add
  80. {
  81. Events.AddHandler(SelectedIndexChangedEvent, value);
  82. }
  83. remove
  84. {
  85. Events.RemoveHandler(SelectedIndexChangedEvent, value);
  86. }
  87. }
  88. [Bindable(true)]
  89. [DefaultValue(-1)]
  90. [WebCategory("Layout")]
  91. [WebSysDescription("BaseDataList_CellPadding")]
  92. public virtual int CellPadding
  93. {
  94. get
  95. {
  96. if(!ControlStyleCreated)
  97. return -1;
  98. return ((TableStyle)ControlStyle).CellPadding;
  99. }
  100. set
  101. {
  102. ((TableStyle)ControlStyle).CellPadding = value;
  103. }
  104. }
  105. [Bindable(true)]
  106. [DefaultValue(-1)]
  107. [WebCategory("Layout")]
  108. [WebSysDescription("BaseDataList_CellSpacing")]
  109. public virtual int CellSpacing
  110. {
  111. get
  112. {
  113. if(!ControlStyleCreated)
  114. return -1;
  115. return ((TableStyle)ControlStyle).CellSpacing;
  116. }
  117. set
  118. {
  119. ((TableStyle)ControlStyle).CellSpacing = value;
  120. }
  121. }
  122. [DefaultValue("")]
  123. [WebCategory("Data")]
  124. [WebSysDescription("BaseDataList_DataKeyField")]
  125. public virtual string DataKeyField
  126. {
  127. get
  128. {
  129. object o = ViewState["DataKeyField"];
  130. if(o!=null)
  131. return (string)o;
  132. return String.Empty;
  133. }
  134. set
  135. {
  136. ViewState["DataKeyField"] = value;
  137. }
  138. }
  139. [Browsable(true)]
  140. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  141. [WebSysDescription("BaseDataList_DataKeys")]
  142. public DataKeyCollection DataKeys
  143. {
  144. get
  145. {
  146. if( dataKeys==null )
  147. dataKeys = new DataKeyCollection(DataKeysArray);
  148. return dataKeys;
  149. }
  150. }
  151. [DefaultValue("")]
  152. [WebCategory("Data")]
  153. [WebSysDescription("BaseDataList_DataMember")]
  154. public string DataMember
  155. {
  156. get
  157. {
  158. object o = ViewState["DataMember"];
  159. if(o!=null)
  160. return (string)o;
  161. return String.Empty;
  162. }
  163. set
  164. {
  165. ViewState["DataMember"] = value;
  166. }
  167. }
  168. [Bindable(true)]
  169. [DefaultValue(null)]
  170. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  171. [WebCategory("Data")]
  172. [WebSysDescription("BaseDataList_DataSource")]
  173. public virtual object DataSource {
  174. get {
  175. return dataSource;
  176. }
  177. set {
  178. if (value == null || value is IListSource || value is IEnumerable) {
  179. dataSource = value;
  180. } else {
  181. throw new ArgumentException (HttpRuntime.FormatResourceString (
  182. "Invalid_DataSource_Type", ID));
  183. }
  184. }
  185. }
  186. [Bindable(true)]
  187. [DefaultValue(GridLines.Both)]
  188. [WebCategory("Appearance")]
  189. [WebSysDescription("BaseDataList_GridLines")]
  190. public virtual GridLines GridLines
  191. {
  192. get
  193. {
  194. if(ControlStyleCreated)
  195. return ((TableStyle)ControlStyle).GridLines;
  196. return GridLines.Both;
  197. }
  198. set
  199. {
  200. ((TableStyle)ControlStyle).GridLines = value;
  201. }
  202. }
  203. // LAMESPEC HorizontalAlign has a Category attribute, this should obviously be a WebCategory attribute
  204. // but is defined incorrectly in the MS framework
  205. [Bindable(true)]
  206. [DefaultValue(HorizontalAlign.NotSet)]
  207. [Category("Layout")]
  208. [WebSysDescription("BaseDataList_HorizontalAlign")]
  209. public virtual HorizontalAlign HorizontalAlign
  210. {
  211. get
  212. {
  213. if(ControlStyleCreated)
  214. return ((TableStyle)ControlStyle).HorizontalAlign;
  215. return HorizontalAlign.NotSet;
  216. }
  217. set
  218. {
  219. ((TableStyle)ControlStyle).HorizontalAlign = value;
  220. }
  221. }
  222. protected ArrayList DataKeysArray
  223. {
  224. get
  225. {
  226. object o = ViewState["DataKeys"];
  227. if(o == null)
  228. {
  229. o = new ArrayList();
  230. ViewState["DataKeys"] = o;
  231. }
  232. return (ArrayList)o;
  233. }
  234. }
  235. protected override void AddParsedSubObject(object o)
  236. {
  237. // Preventing literal controls from being added as children.
  238. }
  239. protected override void CreateChildControls()
  240. {
  241. Controls.Clear();
  242. if(ViewState[ItemCountViewStateKey]!=null)
  243. {
  244. CreateControlHierarchy(false);
  245. ClearChildViewState();
  246. }
  247. }
  248. protected override void OnDataBinding(EventArgs e)
  249. {
  250. base.OnDataBinding(e);
  251. Controls.Clear();
  252. ClearChildViewState();
  253. CreateControlHierarchy(true);
  254. ChildControlsCreated = true;
  255. TrackViewState();
  256. }
  257. protected virtual void OnSelectedIndexChanged(EventArgs e)
  258. {
  259. if(Events != null)
  260. {
  261. EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
  262. if(eh!=null)
  263. eh(this, e);
  264. }
  265. }
  266. protected override void Render(HtmlTextWriter writer)
  267. {
  268. PrepareControlHierarchy();
  269. RenderContents(writer);
  270. }
  271. protected abstract void PrepareControlHierarchy();
  272. protected abstract void CreateControlHierarchy(bool useDataSource);
  273. #if NET_2_0
  274. // should be `internal protected' (why, oh WHY did they do that !?!)
  275. protected override void OnInit (EventArgs e)
  276. {
  277. base.OnInit(e);
  278. inited = true;
  279. if (!Page.IsPostBack)
  280. RequiresDataBinding = true;
  281. }
  282. // should be `internal protected' (why, oh WHY did they do that !?!)
  283. protected override void OnLoad (EventArgs e)
  284. {
  285. IDataSource ds = GetDataSourceObject () as IDataSource;
  286. if (ds != null && DataSourceID != "")
  287. ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
  288. base.OnLoad(e);
  289. }
  290. // should be `internal protected' (why, oh WHY did they do that !?!)
  291. protected override void OnPreRender (EventArgs e)
  292. {
  293. EnsureDataBound ();
  294. base.OnPreRender (e);
  295. }
  296. protected void EnsureDataBound ()
  297. {
  298. if (RequiresDataBinding && DataSourceID != "")
  299. DataBind ();
  300. }
  301. protected virtual object GetDataSourceObject ()
  302. {
  303. if (DataSourceID != "")
  304. return (IDataSource) NamingContainer.FindControl (DataSourceID);
  305. return DataSource;
  306. }
  307. protected virtual IEnumerable GetResolvedDataSource ()
  308. {
  309. if (DataSource != null && DataSourceID != "")
  310. throw new HttpException ();
  311. IDataSource ds = this.GetDataSourceObject () as IDataSource;
  312. if (ds != null && DataSourceID != "") {
  313. return ds.GetView (DataMember).ExecuteSelect (selectArguments);
  314. }
  315. else if (DataSource != null)
  316. return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  317. else
  318. return null;
  319. }
  320. protected void OnDataSourceChanged (object sender, EventArgs e)
  321. {
  322. RequiresDataBinding = true;
  323. }
  324. public virtual string DataSourceID {
  325. get {
  326. object o = ViewState ["DataSourceID"];
  327. if (o != null)
  328. return (string)o;
  329. return String.Empty;
  330. }
  331. set {
  332. if (inited)
  333. RequiresDataBinding = true;
  334. ViewState ["DataSourceID"] = value;
  335. }
  336. }
  337. bool requiresDataBinding;
  338. protected bool RequiresDataBinding {
  339. get { return requiresDataBinding; }
  340. set { requiresDataBinding = value; }
  341. }
  342. protected bool inited;
  343. DataSourceSelectArguments selectArguments = null;
  344. protected DataSourceSelectArguments SelectArguments {
  345. get { return selectArguments; }
  346. }
  347. #else
  348. internal IEnumerable GetResolvedDataSource ()
  349. {
  350. if (DataSource != null)
  351. return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  352. else
  353. return null;
  354. }
  355. #endif
  356. }
  357. }