BaseDataList.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. #if NET_2_0
  52. bool inited;
  53. IDataSource currentSource;
  54. DataSourceSelectArguments selectArguments = null;
  55. bool requiresDataBinding;
  56. #endif
  57. public BaseDataList() : base()
  58. {
  59. }
  60. public static bool IsBindableType(Type type)
  61. {
  62. if(type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal))
  63. return true;
  64. return false;
  65. }
  66. public override ControlCollection Controls
  67. {
  68. get
  69. {
  70. EnsureChildControls();
  71. return base.Controls;
  72. }
  73. }
  74. public override void DataBind()
  75. {
  76. #if NET_2_0
  77. RequiresDataBinding = false;
  78. #endif
  79. OnDataBinding(EventArgs.Empty);
  80. }
  81. [WebCategory("Action")]
  82. [WebSysDescription("BaseDataList_OnSelectedIndexChanged")]
  83. public event EventHandler SelectedIndexChanged
  84. {
  85. add
  86. {
  87. Events.AddHandler(SelectedIndexChangedEvent, value);
  88. }
  89. remove
  90. {
  91. Events.RemoveHandler(SelectedIndexChangedEvent, value);
  92. }
  93. }
  94. #if !NET_2_0
  95. [Bindable(true)]
  96. #endif
  97. [DefaultValue(-1)]
  98. [WebCategory("Layout")]
  99. [WebSysDescription("BaseDataList_CellPadding")]
  100. public virtual int CellPadding
  101. {
  102. get
  103. {
  104. if(!ControlStyleCreated)
  105. return -1;
  106. return ((TableStyle)ControlStyle).CellPadding;
  107. }
  108. set
  109. {
  110. ((TableStyle)ControlStyle).CellPadding = value;
  111. }
  112. }
  113. #if !NET_2_0
  114. [Bindable(true)]
  115. #endif
  116. [DefaultValue(-1)]
  117. [WebCategory("Layout")]
  118. [WebSysDescription("BaseDataList_CellSpacing")]
  119. public virtual int CellSpacing
  120. {
  121. get
  122. {
  123. if(!ControlStyleCreated)
  124. return -1;
  125. return ((TableStyle)ControlStyle).CellSpacing;
  126. }
  127. set
  128. {
  129. ((TableStyle)ControlStyle).CellSpacing = value;
  130. }
  131. }
  132. #if NET_2_0
  133. [DefaultValue ("")]
  134. [WebCategory ("Accessibility")]
  135. [WebSysDescription ("Caption")]
  136. public virtual string Caption {
  137. get {
  138. object o = ViewState ["Caption"];
  139. return (o != null) ? (string) o : "";
  140. }
  141. set { ViewState ["Caption"] = value; }
  142. }
  143. #endif
  144. #if NET_2_0
  145. [ThemeableAttribute (false)]
  146. #endif
  147. [DefaultValue("")]
  148. [WebCategory("Data")]
  149. [WebSysDescription("BaseDataList_DataKeyField")]
  150. public virtual string DataKeyField
  151. {
  152. get
  153. {
  154. object o = ViewState["DataKeyField"];
  155. if(o!=null)
  156. return (string)o;
  157. return String.Empty;
  158. }
  159. set
  160. {
  161. ViewState["DataKeyField"] = value;
  162. }
  163. }
  164. [Browsable(true)]
  165. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  166. [WebSysDescription("BaseDataList_DataKeys")]
  167. public DataKeyCollection DataKeys
  168. {
  169. get
  170. {
  171. if( dataKeys==null )
  172. dataKeys = new DataKeyCollection(DataKeysArray);
  173. return dataKeys;
  174. }
  175. }
  176. #if NET_2_0
  177. [ThemeableAttribute (false)]
  178. #endif
  179. [DefaultValue("")]
  180. [WebCategory("Data")]
  181. [WebSysDescription("BaseDataList_DataMember")]
  182. public string DataMember
  183. {
  184. get
  185. {
  186. object o = ViewState["DataMember"];
  187. if(o!=null)
  188. return (string)o;
  189. return String.Empty;
  190. }
  191. set
  192. {
  193. ViewState["DataMember"] = value;
  194. }
  195. }
  196. #if NET_2_0
  197. [ThemeableAttribute (false)]
  198. #endif
  199. [Bindable(true)]
  200. [DefaultValue(null)]
  201. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  202. [WebCategory("Data")]
  203. [WebSysDescription("BaseDataList_DataSource")]
  204. public virtual object DataSource {
  205. get {
  206. return dataSource;
  207. }
  208. set {
  209. if (value == null || value is IListSource || value is IEnumerable) {
  210. dataSource = value;
  211. #if NET_2_0
  212. if (inited) OnDataPropertyChanged ();
  213. #endif
  214. } else {
  215. throw new ArgumentException (HttpRuntime.FormatResourceString (
  216. "Invalid_DataSource_Type", ID));
  217. }
  218. }
  219. }
  220. #if !NET_2_0
  221. [Bindable(true)]
  222. #endif
  223. [DefaultValue(GridLines.Both)]
  224. [WebCategory("Appearance")]
  225. [WebSysDescription("BaseDataList_GridLines")]
  226. public virtual GridLines GridLines
  227. {
  228. get
  229. {
  230. if(ControlStyleCreated)
  231. return ((TableStyle)ControlStyle).GridLines;
  232. return GridLines.Both;
  233. }
  234. set
  235. {
  236. ((TableStyle)ControlStyle).GridLines = value;
  237. }
  238. }
  239. // LAMESPEC HorizontalAlign has a Category attribute, this should obviously be a WebCategory attribute
  240. // but is defined incorrectly in the MS framework
  241. #if !NET_2_0
  242. [Bindable(true)]
  243. #endif
  244. [DefaultValue(HorizontalAlign.NotSet)]
  245. [Category("Layout")]
  246. [WebSysDescription("BaseDataList_HorizontalAlign")]
  247. public virtual HorizontalAlign HorizontalAlign
  248. {
  249. get
  250. {
  251. if(ControlStyleCreated)
  252. return ((TableStyle)ControlStyle).HorizontalAlign;
  253. return HorizontalAlign.NotSet;
  254. }
  255. set
  256. {
  257. ((TableStyle)ControlStyle).HorizontalAlign = value;
  258. }
  259. }
  260. protected ArrayList DataKeysArray
  261. {
  262. get
  263. {
  264. object o = ViewState["DataKeys"];
  265. if(o == null)
  266. {
  267. o = new ArrayList();
  268. ViewState["DataKeys"] = o;
  269. }
  270. return (ArrayList)o;
  271. }
  272. }
  273. protected override void AddParsedSubObject(object o)
  274. {
  275. // Preventing literal controls from being added as children.
  276. }
  277. protected override void CreateChildControls()
  278. {
  279. Controls.Clear();
  280. if(ViewState[ItemCountViewStateKey]!=null)
  281. {
  282. CreateControlHierarchy(false);
  283. ClearChildViewState();
  284. }
  285. }
  286. protected override void OnDataBinding(EventArgs e)
  287. {
  288. base.OnDataBinding(e);
  289. Controls.Clear();
  290. ClearChildViewState();
  291. CreateControlHierarchy(true);
  292. ChildControlsCreated = true;
  293. TrackViewState();
  294. }
  295. protected virtual void OnSelectedIndexChanged(EventArgs e)
  296. {
  297. if(Events != null)
  298. {
  299. EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
  300. if(eh!=null)
  301. eh(this, e);
  302. }
  303. }
  304. protected override void Render(HtmlTextWriter writer)
  305. {
  306. PrepareControlHierarchy();
  307. RenderContents(writer);
  308. }
  309. protected abstract void PrepareControlHierarchy();
  310. protected abstract void CreateControlHierarchy(bool useDataSource);
  311. #if NET_2_0
  312. protected override void OnInit (EventArgs e)
  313. {
  314. base.OnInit(e);
  315. Page.PreLoad += new EventHandler (OnPagePreLoad);
  316. }
  317. void OnPagePreLoad (object sender, EventArgs e)
  318. {
  319. SubscribeSourceChangeEvent ();
  320. inited = true;
  321. }
  322. void SubscribeSourceChangeEvent ()
  323. {
  324. IDataSource ds = GetDataSource ();
  325. if (currentSource != ds && currentSource != null) {
  326. currentSource.DataSourceChanged -= new EventHandler (OnDataSourceViewChanged);
  327. currentSource = ds;
  328. }
  329. if (ds != null)
  330. ds.DataSourceChanged += new EventHandler (OnDataSourceViewChanged);
  331. }
  332. protected override void OnLoad (EventArgs e)
  333. {
  334. if (IsBoundUsingDataSourceID && (!Page.IsPostBack || !EnableViewState))
  335. RequiresDataBinding = true;
  336. base.OnLoad(e);
  337. }
  338. protected override void OnPreRender (EventArgs e)
  339. {
  340. EnsureDataBound ();
  341. base.OnPreRender (e);
  342. }
  343. protected bool IsBoundUsingDataSourceID {
  344. get { return DataSourceID.Length > 0; }
  345. }
  346. protected void EnsureDataBound ()
  347. {
  348. if (RequiresDataBinding && IsBoundUsingDataSourceID)
  349. DataBind ();
  350. }
  351. IDataSource GetDataSource ()
  352. {
  353. if (IsBoundUsingDataSourceID) {
  354. Control ctrl = NamingContainer.FindControl (DataSourceID);
  355. if (ctrl == null)
  356. throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
  357. if (!(ctrl is IDataSource))
  358. throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
  359. return (IDataSource) ctrl;
  360. }
  361. return DataSource as IDataSource;
  362. }
  363. protected IEnumerable GetData ()
  364. {
  365. if (DataSource != null && IsBoundUsingDataSourceID)
  366. throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
  367. IDataSource ds = GetDataSource ();
  368. if (ds != null)
  369. return ds.GetView (DataMember).ExecuteSelect (SelectArguments);
  370. IEnumerable ie = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  371. if (ie != null) return ie;
  372. throw new HttpException (string.Format ("Unexpected data source type: {0}", DataSource.GetType()));
  373. }
  374. protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
  375. {
  376. RequiresDataBinding = true;
  377. }
  378. protected virtual void OnDataPropertyChanged ()
  379. {
  380. RequiresDataBinding = true;
  381. SubscribeSourceChangeEvent ();
  382. }
  383. [DefaultValueAttribute ("")]
  384. [IDReferencePropertyAttribute (typeof(System.Web.UI.DataSourceControl))]
  385. [ThemeableAttribute (false)]
  386. public virtual string DataSourceID {
  387. get {
  388. object o = ViewState ["DataSourceID"];
  389. if (o != null)
  390. return (string)o;
  391. return String.Empty;
  392. }
  393. set {
  394. ViewState ["DataSourceID"] = value;
  395. if (inited) OnDataPropertyChanged ();
  396. }
  397. }
  398. protected bool Initialized {
  399. get { return inited; }
  400. }
  401. protected bool RequiresDataBinding {
  402. get { return requiresDataBinding; }
  403. set { requiresDataBinding = value; }
  404. }
  405. protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
  406. {
  407. return DataSourceSelectArguments.Empty;
  408. }
  409. protected DataSourceSelectArguments SelectArguments {
  410. get {
  411. if (selectArguments == null)
  412. selectArguments = CreateDataSourceSelectArguments ();
  413. return selectArguments;
  414. }
  415. }
  416. internal IEnumerable GetResolvedDataSource ()
  417. {
  418. return GetData ();
  419. }
  420. #else
  421. internal IEnumerable GetResolvedDataSource ()
  422. {
  423. if (DataSource != null)
  424. return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  425. else
  426. return null;
  427. }
  428. #endif
  429. }
  430. }