PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Web/System.Web.UI.WebControls/DataList.cs

https://bitbucket.org/danipen/mono
C# | 1047 lines | 878 code | 106 blank | 63 comment | 254 complexity | 89d0ef7d7d1a192a9b86380685978e0f MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Web.UI.WebControls.DataList.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. //
  7. // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections;
  29. using System.ComponentModel;
  30. using System.Globalization;
  31. using System.Security.Permissions;
  32. using System.Web.Util;
  33. namespace System.Web.UI.WebControls
  34. {
  35. // CAS
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. // attributes
  39. [Designer ("System.Web.UI.Design.WebControls.DataListDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. [ControlValueProperty ("SelectedValue")]
  41. [Editor ("System.Web.UI.Design.WebControls.DataListComponentEditor, " + Consts.AssemblySystem_Design, "System.ComponentModel.ComponentEditor, " + Consts.AssemblySystem)]
  42. public class DataList : BaseDataList, INamingContainer, IRepeatInfoUser
  43. {
  44. public const string CancelCommandName = "Cancel";
  45. public const string DeleteCommandName = "Delete";
  46. public const string EditCommandName = "Edit";
  47. public const string SelectCommandName = "Select";
  48. public const string UpdateCommandName = "Update";
  49. static readonly object cancelCommandEvent = new object ();
  50. static readonly object deleteCommandEvent = new object ();
  51. static readonly object editCommandEvent = new object ();
  52. static readonly object itemCommandEvent = new object ();
  53. static readonly object itemCreatedEvent = new object ();
  54. static readonly object itemDataBoundEvent = new object ();
  55. static readonly object updateCommandEvent = new object ();
  56. TableItemStyle alternatingItemStyle;
  57. TableItemStyle editItemStyle;
  58. TableItemStyle footerStyle;
  59. TableItemStyle headerStyle;
  60. TableItemStyle itemStyle;
  61. TableItemStyle selectedItemStyle;
  62. TableItemStyle separatorStyle;
  63. ITemplate alternatingItemTemplate;
  64. ITemplate editItemTemplate;
  65. ITemplate footerTemplate;
  66. ITemplate headerTemplate;
  67. ITemplate itemTemplate;
  68. ITemplate selectedItemTemplate;
  69. ITemplate separatorTemplate;
  70. DataListItemCollection items;
  71. ArrayList list;
  72. int idx;
  73. public DataList ()
  74. {
  75. idx = -1;
  76. }
  77. [DefaultValue (null)]
  78. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  79. [NotifyParentProperty (true)]
  80. [PersistenceMode (PersistenceMode.InnerProperty)]
  81. [WebSysDescription ("")]
  82. [WebCategory ("Style")]
  83. public virtual TableItemStyle AlternatingItemStyle {
  84. get {
  85. if (alternatingItemStyle == null) {
  86. alternatingItemStyle = new TableItemStyle ();
  87. if (IsTrackingViewState)
  88. alternatingItemStyle.TrackViewState ();
  89. }
  90. return alternatingItemStyle;
  91. }
  92. }
  93. [Browsable (false)]
  94. [DefaultValue (null)]
  95. [TemplateContainer (typeof (DataListItem))]
  96. [PersistenceMode (PersistenceMode.InnerProperty)]
  97. [WebSysDescription ("")]
  98. [WebCategory ("Style")]
  99. public virtual ITemplate AlternatingItemTemplate {
  100. get { return alternatingItemTemplate; }
  101. set { alternatingItemTemplate = value; }
  102. }
  103. [DefaultValue (-1)]
  104. [WebSysDescription ("")]
  105. [WebCategory ("Misc")]
  106. public virtual int EditItemIndex {
  107. get {
  108. object o = ViewState ["EditItemIndex"];
  109. return (o == null) ? -1 : (int) o;
  110. }
  111. set {
  112. if (value < -1)
  113. throw new ArgumentOutOfRangeException ("EditItemIndex", "< -1");
  114. ViewState ["EditItemIndex"] = value;
  115. }
  116. }
  117. [DefaultValue (null)]
  118. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  119. [NotifyParentProperty (true)]
  120. [PersistenceMode (PersistenceMode.InnerProperty)]
  121. [WebSysDescription ("")]
  122. [WebCategory ("Style")]
  123. public virtual TableItemStyle EditItemStyle {
  124. get {
  125. if (editItemStyle == null) {
  126. editItemStyle = new TableItemStyle ();
  127. if (IsTrackingViewState)
  128. editItemStyle.TrackViewState ();
  129. }
  130. return editItemStyle;
  131. }
  132. }
  133. [Browsable (false)]
  134. [DefaultValue (null)]
  135. [TemplateContainer (typeof (DataListItem))]
  136. [PersistenceMode (PersistenceMode.InnerProperty)]
  137. [WebSysDescription ("")]
  138. [WebCategory ("Style")]
  139. public virtual ITemplate EditItemTemplate {
  140. get { return editItemTemplate; }
  141. set { editItemTemplate = value; }
  142. }
  143. [DefaultValue (false)]
  144. [WebSysDescription ("")]
  145. [WebCategory ("Misc")]
  146. public virtual bool ExtractTemplateRows {
  147. get {
  148. object o = ViewState ["ExtractTemplateRows"];
  149. return (o == null) ? false : (bool) o;
  150. }
  151. set { ViewState ["ExtractTemplateRows"] = value; }
  152. }
  153. [DefaultValue (null)]
  154. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  155. [NotifyParentProperty (true)]
  156. [PersistenceMode (PersistenceMode.InnerProperty)]
  157. [WebSysDescription ("")]
  158. [WebCategory ("Style")]
  159. public virtual TableItemStyle FooterStyle {
  160. get {
  161. if (footerStyle == null) {
  162. footerStyle = new TableItemStyle ();
  163. if (IsTrackingViewState)
  164. footerStyle.TrackViewState ();
  165. }
  166. return footerStyle;
  167. }
  168. }
  169. [Browsable (false)]
  170. [DefaultValue (null)]
  171. [TemplateContainer (typeof (DataListItem))]
  172. [PersistenceMode (PersistenceMode.InnerProperty)]
  173. [WebSysDescription ("")]
  174. [WebCategory ("Style")]
  175. public virtual ITemplate FooterTemplate {
  176. get { return footerTemplate; }
  177. set { footerTemplate = value; }
  178. }
  179. // yes! they do NOT match in fx1.1
  180. [DefaultValue (GridLines.None)]
  181. public override GridLines GridLines {
  182. get {
  183. if (!ControlStyleCreated)
  184. return GridLines.None;
  185. return TableStyle.GridLines;
  186. }
  187. set { TableStyle.GridLines = value; }
  188. }
  189. [DefaultValue (null)]
  190. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  191. [NotifyParentProperty (true)]
  192. [PersistenceMode (PersistenceMode.InnerProperty)]
  193. [WebSysDescription ("")]
  194. [WebCategory ("Style")]
  195. public virtual TableItemStyle HeaderStyle {
  196. get {
  197. if (headerStyle == null) {
  198. headerStyle = new TableItemStyle ();
  199. if (IsTrackingViewState)
  200. headerStyle.TrackViewState ();
  201. }
  202. return headerStyle;
  203. }
  204. }
  205. [Browsable (false)]
  206. [DefaultValue (null)]
  207. [TemplateContainer (typeof (DataListItem))]
  208. [PersistenceMode (PersistenceMode.InnerProperty)]
  209. [WebSysDescription ("")]
  210. [WebCategory ("Style")]
  211. public virtual ITemplate HeaderTemplate {
  212. get { return headerTemplate; }
  213. set { headerTemplate = value; }
  214. }
  215. [Browsable (false)]
  216. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  217. [WebSysDescription ("")]
  218. [WebCategory ("Style")]
  219. public virtual DataListItemCollection Items {
  220. get {
  221. if (items == null)
  222. items = new DataListItemCollection (ItemList);
  223. return items;
  224. }
  225. }
  226. [DefaultValue (null)]
  227. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  228. [NotifyParentProperty (true)]
  229. [PersistenceMode (PersistenceMode.InnerProperty)]
  230. [WebSysDescription ("")]
  231. [WebCategory ("Style")]
  232. public virtual TableItemStyle ItemStyle {
  233. get {
  234. if (itemStyle == null) {
  235. itemStyle = new TableItemStyle ();
  236. if (IsTrackingViewState)
  237. itemStyle.TrackViewState ();
  238. }
  239. return itemStyle;
  240. }
  241. }
  242. [Browsable (false)]
  243. [DefaultValue (null)]
  244. [TemplateContainer (typeof (DataListItem))]
  245. [PersistenceMode (PersistenceMode.InnerProperty)]
  246. [WebSysDescription ("")]
  247. [WebCategory ("Style")]
  248. public virtual ITemplate ItemTemplate {
  249. get { return itemTemplate; }
  250. set { itemTemplate = value; }
  251. }
  252. [DefaultValue (0)]
  253. [WebSysDescription ("")]
  254. [WebCategory ("Layout")]
  255. public virtual int RepeatColumns {
  256. get {
  257. object o = ViewState ["RepeatColumns"];
  258. return (o == null) ? 0 : (int) o;
  259. }
  260. set {
  261. if (value < 0)
  262. throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");
  263. ViewState ["RepeatColumns"] = value;
  264. }
  265. }
  266. [DefaultValue (RepeatDirection.Vertical)]
  267. [WebSysDescription ("")]
  268. [WebCategory ("Layout")]
  269. public virtual RepeatDirection RepeatDirection {
  270. get {
  271. object o = ViewState ["RepeatDirection"];
  272. return (o == null) ? RepeatDirection.Vertical : (RepeatDirection) o;
  273. }
  274. set { ViewState ["RepeatDirection"] = value; }
  275. }
  276. [DefaultValue (RepeatLayout.Table)]
  277. [WebSysDescription ("")]
  278. [WebCategory ("Layout")]
  279. public virtual RepeatLayout RepeatLayout {
  280. get {
  281. object o = ViewState ["RepeatLayout"];
  282. return (o == null) ? RepeatLayout.Table : (RepeatLayout) o;
  283. }
  284. set {
  285. #if NET_4_0
  286. if (value == RepeatLayout.OrderedList || value == RepeatLayout.UnorderedList)
  287. throw new ArgumentOutOfRangeException (String.Format ("DataList does not support the '{0}' layout.", value));
  288. #endif
  289. ViewState ["RepeatLayout"] = value;
  290. }
  291. }
  292. [Bindable (true)]
  293. [DefaultValue (-1)]
  294. [WebSysDescription ("")]
  295. [WebCategory ("Layout")]
  296. public virtual int SelectedIndex {
  297. get {
  298. object o = ViewState ["SelectedIndex"];
  299. return (o == null) ? -1 : (int) o;
  300. }
  301. set {
  302. if (value < -1)
  303. throw new ArgumentOutOfRangeException ("SelectedIndex", "< -1");
  304. ViewState ["SelectedIndex"] = value;
  305. }
  306. }
  307. [Browsable (false)]
  308. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  309. [WebSysDescription ("")]
  310. [WebCategory ("Layout")]
  311. public virtual DataListItem SelectedItem {
  312. get {
  313. if (SelectedIndex < 0)
  314. return null;
  315. if (SelectedIndex >= Items.Count)
  316. throw new ArgumentOutOfRangeException ("SelectedItem", ">= Items.Count");
  317. return items [SelectedIndex];
  318. }
  319. }
  320. [DefaultValue (null)]
  321. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  322. [NotifyParentProperty (true)]
  323. [PersistenceMode (PersistenceMode.InnerProperty)]
  324. [WebSysDescription ("")]
  325. [WebCategory ("Style")]
  326. public virtual TableItemStyle SelectedItemStyle {
  327. get {
  328. if (selectedItemStyle == null) {
  329. selectedItemStyle = new TableItemStyle ();
  330. if (IsTrackingViewState)
  331. selectedItemStyle.TrackViewState ();
  332. }
  333. return selectedItemStyle;
  334. }
  335. }
  336. [Browsable (false)]
  337. [DefaultValue (null)]
  338. [TemplateContainer (typeof (DataListItem))]
  339. [PersistenceMode (PersistenceMode.InnerProperty)]
  340. [WebSysDescription ("")]
  341. [WebCategory ("Style")]
  342. public virtual ITemplate SelectedItemTemplate {
  343. get { return selectedItemTemplate; }
  344. set { selectedItemTemplate = value; }
  345. }
  346. [DefaultValue (null)]
  347. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  348. [NotifyParentProperty (true)]
  349. [PersistenceMode (PersistenceMode.InnerProperty)]
  350. [WebSysDescription ("")]
  351. [WebCategory ("Style")]
  352. public virtual TableItemStyle SeparatorStyle {
  353. get {
  354. if (separatorStyle == null) {
  355. separatorStyle = new TableItemStyle ();
  356. if (IsTrackingViewState)
  357. separatorStyle.TrackViewState ();
  358. }
  359. return separatorStyle;
  360. }
  361. }
  362. [Browsable (false)]
  363. [DefaultValue (null)]
  364. [TemplateContainer (typeof (DataListItem))]
  365. [PersistenceMode (PersistenceMode.InnerProperty)]
  366. [WebSysDescription ("")]
  367. [WebCategory ("Style")]
  368. public virtual ITemplate SeparatorTemplate {
  369. get { return separatorTemplate; }
  370. set { separatorTemplate = value; }
  371. }
  372. [DefaultValue (true)]
  373. [WebSysDescription ("")]
  374. [WebCategory ("Appearance")]
  375. public virtual bool ShowFooter {
  376. get {
  377. object o = ViewState ["ShowFooter"];
  378. return (o == null) ? true : (bool) o;
  379. }
  380. set { ViewState ["ShowFooter"] = value; }
  381. }
  382. [DefaultValue (true)]
  383. [WebSysDescription ("")]
  384. [WebCategory ("Appearance")]
  385. public virtual bool ShowHeader {
  386. get {
  387. object o = ViewState ["ShowHeader"];
  388. return (o == null) ? true : (bool) o;
  389. }
  390. set { ViewState ["ShowHeader"] = value; }
  391. }
  392. [MonoTODO ("incomplete")]
  393. [Browsable (false)]
  394. public object SelectedValue {
  395. get {
  396. if (DataKeyField.Length == 0)
  397. throw new InvalidOperationException (Locale.GetText ("No DataKeyField present."));
  398. int idx = SelectedIndex;
  399. if ((idx >= 0) && (idx < DataKeys.Count))
  400. return DataKeys [idx];
  401. return null;
  402. }
  403. }
  404. protected override HtmlTextWriterTag TagKey {
  405. get { return HtmlTextWriterTag.Table; }
  406. }
  407. TableStyle TableStyle {
  408. // this will throw an InvalidCasException just like we need
  409. get { return (TableStyle) ControlStyle; }
  410. }
  411. ArrayList ItemList {
  412. get {
  413. if (list == null)
  414. list = new ArrayList ();
  415. return list;
  416. }
  417. }
  418. void DoItem (int i, ListItemType t, object d, bool databind)
  419. {
  420. DataListItem itm = CreateItem (i, t);
  421. if (databind)
  422. itm.DataItem = d;
  423. DataListItemEventArgs e = new DataListItemEventArgs (itm);
  424. InitializeItem (itm);
  425. //
  426. // It is very important that this be called *before* data
  427. // binding. Otherwise, we won't save our state in the viewstate.
  428. //
  429. Controls.Add (itm);
  430. if (i != -1)
  431. ItemList.Add (itm);
  432. OnItemCreated (e);
  433. if (databind) {
  434. itm.DataBind ();
  435. OnItemDataBound (e);
  436. itm.DataItem = null;
  437. }
  438. }
  439. void DoItemInLoop (int i, object d, bool databind, ListItemType type)
  440. {
  441. DoItem (i, type, d, databind);
  442. if (SeparatorTemplate != null)
  443. DoItem (i, ListItemType.Separator, null, databind);
  444. }
  445. protected override void CreateControlHierarchy (bool useDataSource)
  446. {
  447. Controls.Clear();
  448. ItemList.Clear ();
  449. IEnumerable ds = null;
  450. ArrayList keys = null;
  451. if (useDataSource) {
  452. idx = 0;
  453. if (IsBoundUsingDataSourceID)
  454. ds = GetData();
  455. else
  456. ds = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  457. keys = DataKeysArray;
  458. keys.Clear ();
  459. } else
  460. idx = (int) ViewState ["Items"];
  461. if ((ds == null) && (idx == 0))
  462. return;
  463. if (headerTemplate != null)
  464. DoItem (-1, ListItemType.Header, null, useDataSource);
  465. // items
  466. int selected_index = SelectedIndex;
  467. int edit_item_index = EditItemIndex;
  468. ListItemType type;
  469. if (ds != null) {
  470. string key = DataKeyField;
  471. foreach (object o in ds) {
  472. if (useDataSource && !String.IsNullOrEmpty (key))
  473. keys.Add (DataBinder.GetPropertyValue (o, key));
  474. type = ListItemType.Item;
  475. if (idx == edit_item_index)
  476. type = ListItemType.EditItem;
  477. else if (idx == selected_index)
  478. type = ListItemType.SelectedItem;
  479. else if ((idx & 1) != 0)
  480. type = ListItemType.AlternatingItem;
  481. DoItemInLoop (idx, o, useDataSource, type);
  482. idx++;
  483. }
  484. } else {
  485. for (int i = 0; i < idx; i++) {
  486. type = ListItemType.Item;
  487. if (i == edit_item_index)
  488. type = ListItemType.EditItem;
  489. else if (i == selected_index)
  490. type = ListItemType.SelectedItem;
  491. else if ((i & 1) != 0)
  492. type = ListItemType.AlternatingItem;
  493. DoItemInLoop (i, null, useDataSource, type);
  494. }
  495. }
  496. if (footerTemplate != null)
  497. DoItem (-1, ListItemType.Footer, null, useDataSource);
  498. ViewState ["Items"] = idx;
  499. }
  500. protected override Style CreateControlStyle ()
  501. {
  502. // not kept (directly) in the DataList ViewState
  503. TableStyle tableStyle = new TableStyle ();
  504. tableStyle.CellSpacing = 0;
  505. return tableStyle;
  506. }
  507. protected virtual DataListItem CreateItem (int itemIndex, ListItemType itemType)
  508. {
  509. return new DataListItem (itemIndex, itemType);
  510. }
  511. protected virtual void InitializeItem (DataListItem item)
  512. {
  513. ITemplate t = null;
  514. switch (item.ItemType) {
  515. case ListItemType.Header:
  516. t = HeaderTemplate;
  517. break;
  518. case ListItemType.Footer:
  519. t = FooterTemplate;
  520. break;
  521. case ListItemType.Separator:
  522. t = SeparatorTemplate;
  523. break;
  524. case ListItemType.Item:
  525. case ListItemType.AlternatingItem:
  526. case ListItemType.SelectedItem:
  527. case ListItemType.EditItem:
  528. if ((item.ItemType == ListItemType.EditItem) && (EditItemTemplate != null))
  529. t = EditItemTemplate;
  530. else if ((item.ItemType == ListItemType.SelectedItem) && (SelectedItemTemplate != null))
  531. t = SelectedItemTemplate;
  532. else if ((item.ItemType == ListItemType.AlternatingItem) && (AlternatingItemTemplate != null))
  533. t = AlternatingItemTemplate;
  534. else
  535. t = ItemTemplate;
  536. break;
  537. }
  538. if (t != null)
  539. t.InstantiateIn (item);
  540. }
  541. protected override void LoadViewState (object savedState)
  542. {
  543. object[] state = (object[]) savedState;
  544. base.LoadViewState (state [0]);
  545. if (state [1] != null)
  546. ItemStyle.LoadViewState (state [1]);
  547. if (state [2] != null)
  548. SelectedItemStyle.LoadViewState (state [2]);
  549. if (state [3] != null)
  550. AlternatingItemStyle.LoadViewState (state [3]);
  551. if (state [4] != null)
  552. EditItemStyle.LoadViewState (state [4]);
  553. if (state [5] != null)
  554. SeparatorStyle.LoadViewState (state [5]);
  555. if (state [6] != null)
  556. HeaderStyle.LoadViewState (state [6]);
  557. if (state [7] != null)
  558. FooterStyle.LoadViewState (state [7]);
  559. if (state [8] != null)
  560. ControlStyle.LoadViewState (state [8]);
  561. }
  562. protected override bool OnBubbleEvent (object source, EventArgs e)
  563. {
  564. DataListCommandEventArgs dlca = (e as DataListCommandEventArgs);
  565. if (dlca == null)
  566. return false;
  567. string cn = dlca.CommandName;
  568. CultureInfo inv = Helpers.InvariantCulture;
  569. OnItemCommand (dlca);
  570. if (String.Compare (cn, CancelCommandName, true, inv) == 0)
  571. OnCancelCommand (dlca);
  572. else if (String.Compare (cn, DeleteCommandName, true, inv) == 0)
  573. OnDeleteCommand (dlca);
  574. else if (String.Compare (cn, EditCommandName, true, inv) == 0)
  575. OnEditCommand (dlca);
  576. else if (String.Compare (cn, SelectCommandName, true, inv) == 0) {
  577. SelectedIndex = dlca.Item.ItemIndex;
  578. OnSelectedIndexChanged (dlca);
  579. } else if (String.Compare (cn, UpdateCommandName, true, inv) == 0)
  580. OnUpdateCommand (dlca);
  581. return true;
  582. }
  583. protected virtual void OnCancelCommand (DataListCommandEventArgs e)
  584. {
  585. DataListCommandEventHandler cancelCommand = (DataListCommandEventHandler) Events [cancelCommandEvent];
  586. if (cancelCommand != null)
  587. cancelCommand (this, e);
  588. }
  589. protected virtual void OnDeleteCommand (DataListCommandEventArgs e)
  590. {
  591. DataListCommandEventHandler deleteCommand = (DataListCommandEventHandler) Events [deleteCommandEvent];
  592. if (deleteCommand != null)
  593. deleteCommand (this, e);
  594. }
  595. protected virtual void OnEditCommand (DataListCommandEventArgs e)
  596. {
  597. DataListCommandEventHandler editCommand = (DataListCommandEventHandler) Events [editCommandEvent];
  598. if (editCommand != null)
  599. editCommand (this, e);
  600. }
  601. protected internal override void OnInit (EventArgs e)
  602. {
  603. // EditItemIndex and SelectedIndex now use the Control State (i.e not the
  604. // View State)
  605. Page page = Page;
  606. if (page != null)
  607. page.RegisterRequiresControlState (this);
  608. base.OnInit (e);
  609. }
  610. protected virtual void OnItemCommand (DataListCommandEventArgs e)
  611. {
  612. DataListCommandEventHandler itemCommand = (DataListCommandEventHandler) Events [itemCommandEvent];
  613. if (itemCommand != null)
  614. itemCommand (this, e);
  615. }
  616. protected virtual void OnItemCreated (DataListItemEventArgs e)
  617. {
  618. DataListItemEventHandler itemCreated = (DataListItemEventHandler) Events [itemCreatedEvent];
  619. if (itemCreated != null)
  620. itemCreated (this, e);
  621. }
  622. protected virtual void OnItemDataBound (DataListItemEventArgs e)
  623. {
  624. DataListItemEventHandler itemDataBound = (DataListItemEventHandler) Events [itemDataBoundEvent];
  625. if (itemDataBound != null)
  626. itemDataBound (this, e);
  627. }
  628. protected virtual void OnUpdateCommand (DataListCommandEventArgs e)
  629. {
  630. DataListCommandEventHandler updateCommand = (DataListCommandEventHandler) Events [updateCommandEvent];
  631. if (updateCommand != null)
  632. updateCommand (this, e);
  633. }
  634. protected override void PrepareControlHierarchy ()
  635. {
  636. if (!HasControls () || Controls.Count == 0)
  637. return; // No one called CreateControlHierarchy() with DataSource != null
  638. Style alt = null;
  639. foreach (DataListItem item in Controls) {
  640. switch (item.ItemType) {
  641. case ListItemType.Item:
  642. item.MergeStyle (itemStyle);
  643. break;
  644. case ListItemType.AlternatingItem:
  645. if (alt == null) {
  646. if (alternatingItemStyle != null) {
  647. alt = new TableItemStyle ();
  648. alt.CopyFrom (itemStyle);
  649. alt.CopyFrom (alternatingItemStyle);
  650. } else
  651. alt = itemStyle;
  652. }
  653. item.MergeStyle (alt);
  654. break;
  655. case ListItemType.EditItem:
  656. if (editItemStyle != null)
  657. item.MergeStyle (editItemStyle);
  658. else
  659. item.MergeStyle (itemStyle);
  660. break;
  661. case ListItemType.Footer:
  662. if (!ShowFooter) {
  663. item.Visible = false;
  664. break;
  665. }
  666. if (footerStyle != null)
  667. item.MergeStyle (footerStyle);
  668. break;
  669. case ListItemType.Header:
  670. if (!ShowHeader) {
  671. item.Visible = false;
  672. break;
  673. }
  674. if (headerStyle != null)
  675. item.MergeStyle (headerStyle);
  676. break;
  677. case ListItemType.SelectedItem:
  678. if (selectedItemStyle != null)
  679. item.MergeStyle (selectedItemStyle);
  680. else
  681. item.MergeStyle (itemStyle);
  682. break;
  683. case ListItemType.Separator:
  684. if (separatorStyle != null)
  685. item.MergeStyle(separatorStyle);
  686. else
  687. item.MergeStyle (itemStyle);
  688. break;
  689. }
  690. }
  691. }
  692. protected internal override void RenderContents (HtmlTextWriter writer)
  693. {
  694. if (Items.Count == 0)
  695. return;
  696. RepeatInfo ri = new RepeatInfo ();
  697. ri.RepeatColumns = RepeatColumns;
  698. ri.RepeatDirection = RepeatDirection;
  699. ri.RepeatLayout = RepeatLayout;
  700. ri.CaptionAlign = CaptionAlign;
  701. ri.Caption = Caption;
  702. ri.UseAccessibleHeader = UseAccessibleHeader;
  703. /*
  704. // debugging stuff that I prefer to keep for a while
  705. Console.WriteLine ("RepeatColumns {0}", ri.RepeatColumns);
  706. Console.WriteLine ("RepeatDirection {0}", ri.RepeatDirection);
  707. Console.WriteLine ("RepeatLayout {0}", ri.RepeatLayout);
  708. Console.WriteLine ("OuterTableImplied {0}", ExtractTemplateRows);
  709. Console.WriteLine ("IRepeatInfoUser.HasFooter {0}", (ShowFooter && (footerTemplate != null)));
  710. Console.WriteLine ("IRepeatInfoUser.HasHeader {0}", (ShowHeader && (headerTemplate != null)));
  711. Console.WriteLine ("IRepeatInfoUser.HasSeparators {0}", (separatorTemplate != null));
  712. Console.WriteLine ("IRepeatInfoUser.RepeatedItemCount {0}", Items.Count);
  713. for (int i=0; i < Items.Count; i++) {
  714. DataListItem dli = Items [i];
  715. Console.WriteLine ("{0}: Index {1}, Type {2}", i, dli.ItemIndex, dli.ItemType);
  716. }
  717. */
  718. bool extract = ExtractTemplateRows;
  719. if (extract) {
  720. ri.OuterTableImplied = true;
  721. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
  722. if (ControlStyleCreated)
  723. ControlStyle.AddAttributesToRender (writer);
  724. writer.RenderBeginTag (HtmlTextWriterTag.Table);
  725. ri.RenderRepeater (writer, this, ControlStyle, this);
  726. writer.RenderEndTag ();
  727. } else
  728. ri.RenderRepeater (writer, this, ControlStyle, this);
  729. }
  730. protected override object SaveViewState ()
  731. {
  732. object[] state = new object [9];
  733. state[0] = base.SaveViewState ();
  734. if (itemStyle != null)
  735. state [1] = itemStyle.SaveViewState ();
  736. if (selectedItemStyle != null)
  737. state [2] = selectedItemStyle.SaveViewState ();
  738. if (alternatingItemStyle != null)
  739. state [3] = alternatingItemStyle.SaveViewState ();
  740. if (editItemStyle != null)
  741. state [4] = editItemStyle.SaveViewState ();
  742. if (separatorStyle != null)
  743. state [5] = separatorStyle.SaveViewState ();
  744. if (headerStyle != null)
  745. state [6] = headerStyle.SaveViewState ();
  746. if (footerStyle != null)
  747. state [7] = footerStyle.SaveViewState ();
  748. if (ControlStyleCreated)
  749. state [8] = ControlStyle.SaveViewState ();
  750. return state;
  751. }
  752. protected override void TrackViewState ()
  753. {
  754. base.TrackViewState ();
  755. if (alternatingItemStyle != null)
  756. alternatingItemStyle.TrackViewState ();
  757. if (editItemStyle != null)
  758. editItemStyle.TrackViewState ();
  759. if (footerStyle != null)
  760. footerStyle.TrackViewState ();
  761. if (headerStyle != null)
  762. headerStyle.TrackViewState ();
  763. if (itemStyle != null)
  764. itemStyle.TrackViewState ();
  765. if (selectedItemStyle != null)
  766. selectedItemStyle.TrackViewState ();
  767. if (separatorStyle != null)
  768. separatorStyle.TrackViewState ();
  769. if (ControlStyleCreated)
  770. ControlStyle.TrackViewState ();
  771. }
  772. [WebSysDescription ("")]
  773. [WebCategory ("Action")]
  774. public event DataListCommandEventHandler CancelCommand {
  775. add { Events.AddHandler (cancelCommandEvent, value); }
  776. remove { Events.RemoveHandler (cancelCommandEvent, value); }
  777. }
  778. [WebSysDescription ("")]
  779. [WebCategory ("Action")]
  780. public event DataListCommandEventHandler DeleteCommand {
  781. add { Events.AddHandler (deleteCommandEvent, value); }
  782. remove { Events.RemoveHandler (deleteCommandEvent, value); }
  783. }
  784. [WebSysDescription ("")]
  785. [WebCategory ("Action")]
  786. public event DataListCommandEventHandler EditCommand {
  787. add { Events.AddHandler (editCommandEvent, value); }
  788. remove { Events.RemoveHandler (editCommandEvent, value); }
  789. }
  790. [WebSysDescription ("")]
  791. [WebCategory ("Action")]
  792. public event DataListCommandEventHandler ItemCommand {
  793. add { Events.AddHandler (itemCommandEvent, value); }
  794. remove { Events.RemoveHandler (itemCommandEvent, value); }
  795. }
  796. [WebSysDescription ("")]
  797. [WebCategory ("Action")]
  798. public event DataListItemEventHandler ItemCreated {
  799. add { Events.AddHandler (itemCreatedEvent, value); }
  800. remove { Events.RemoveHandler (itemCreatedEvent, value); }
  801. }
  802. [WebSysDescription ("")]
  803. [WebCategory ("Action")]
  804. public event DataListItemEventHandler ItemDataBound {
  805. add { Events.AddHandler (itemDataBoundEvent, value); }
  806. remove { Events.RemoveHandler (itemDataBoundEvent, value); }
  807. }
  808. [WebSysDescription ("")]
  809. [WebCategory ("Action")]
  810. public event DataListCommandEventHandler UpdateCommand {
  811. add { Events.AddHandler (updateCommandEvent, value); }
  812. remove { Events.RemoveHandler (updateCommandEvent, value); }
  813. }
  814. // IRepeatInfoUser support
  815. bool IRepeatInfoUser.HasFooter {
  816. get { return (ShowFooter && (footerTemplate != null)); }
  817. }
  818. bool IRepeatInfoUser.HasHeader {
  819. get { return (ShowHeader && (headerTemplate != null)); }
  820. }
  821. bool IRepeatInfoUser.HasSeparators {
  822. get { return (separatorTemplate != null); }
  823. }
  824. // don't include header, footer and separators in the count
  825. int IRepeatInfoUser.RepeatedItemCount {
  826. get {
  827. if (idx == -1) {
  828. object o = ViewState ["Items"];
  829. idx = (o == null) ? 0 : (int) o;
  830. }
  831. return idx;
  832. }
  833. }
  834. Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
  835. {
  836. DataListItem item = null;
  837. switch (itemType) {
  838. case ListItemType.Header:
  839. case ListItemType.Footer:
  840. if (repeatIndex >= 0 && (!HasControls () || repeatIndex >= Controls.Count))
  841. throw new ArgumentOutOfRangeException ();
  842. item = FindFirstItem (itemType);
  843. break;
  844. case ListItemType.Item:
  845. case ListItemType.AlternatingItem:
  846. case ListItemType.SelectedItem:
  847. case ListItemType.EditItem:
  848. if (repeatIndex >= 0 && (!HasControls () || repeatIndex >= Controls.Count))
  849. throw new ArgumentOutOfRangeException ();
  850. item = FindBestItem (repeatIndex);
  851. break;
  852. case ListItemType.Separator:
  853. if (repeatIndex >= 0 && (!HasControls () || repeatIndex >= Controls.Count))
  854. throw new ArgumentOutOfRangeException ();
  855. item = FindSpecificItem (itemType, repeatIndex);
  856. break;
  857. default:
  858. item = null;
  859. break;
  860. }
  861. if (item == null || item.ControlStyleCreated == false)
  862. return null;
  863. return item.ControlStyle;
  864. }
  865. // Header and Footer don't have a "real" index (-1)
  866. DataListItem FindFirstItem (ListItemType itemType)
  867. {
  868. for (int i = 0; i < Controls.Count; i++) {
  869. DataListItem item = (Controls [i] as DataListItem);
  870. if ((item != null) && (item.ItemType == itemType))
  871. return item;
  872. }
  873. return null;
  874. }
  875. // Both Type and Index must match (e.g. Separator)
  876. DataListItem FindSpecificItem (ListItemType itemType, int repeatIndex)
  877. {
  878. for (int i = 0; i < Controls.Count; i++) {
  879. DataListItem item = (Controls [i] as DataListItem);
  880. if ((item != null) && (item.ItemType == itemType) && (item.ItemIndex == repeatIndex))
  881. return item;
  882. }
  883. return null;
  884. }
  885. // we get call for Item even for AlternatingItem :(
  886. DataListItem FindBestItem (int repeatIndex)
  887. {
  888. for (int i = 0; i < Controls.Count; i++) {
  889. DataListItem item = (Controls [i] as DataListItem);
  890. if ((item != null) && (item.ItemIndex == repeatIndex)) {
  891. switch (item.ItemType) {
  892. case ListItemType.Item:
  893. case ListItemType.AlternatingItem:
  894. case ListItemType.SelectedItem:
  895. case ListItemType.EditItem:
  896. return item;
  897. default:
  898. return null;
  899. }
  900. }
  901. }
  902. return null;
  903. }
  904. void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
  905. {
  906. // if possible take the easy way out...
  907. if (!HasControls ())
  908. return;
  909. DataListItem item = null;
  910. switch (itemType) {
  911. case ListItemType.Header:
  912. case ListItemType.Footer:
  913. item = FindFirstItem (itemType);
  914. break;
  915. case ListItemType.Item:
  916. case ListItemType.AlternatingItem:
  917. case ListItemType.SelectedItem:
  918. case ListItemType.EditItem:
  919. item = FindBestItem (repeatIndex);
  920. break;
  921. case ListItemType.Separator:
  922. item = FindSpecificItem (itemType, repeatIndex);
  923. break;
  924. }
  925. if (item != null) {
  926. bool extract = ExtractTemplateRows;
  927. bool table = (RepeatLayout == RepeatLayout.Table);
  928. if (!table || extract) {
  929. // sadly RepeatInfo doesn't support Style for RepeatLayout.Flow
  930. Style s = (this as IRepeatInfoUser).GetItemStyle (itemType, repeatIndex);
  931. if (s != null)
  932. item.ControlStyle.CopyFrom (s);
  933. }
  934. //Console.WriteLine ("RenderItem #{0} type {1}", repeatIndex, itemType);
  935. item.RenderItem (writer, extract, table);
  936. } else {
  937. //Console.WriteLine ("Couldn't find #{0} type {1} out of {2} items / {3} controls", repeatIndex, itemType, Items.Count, Controls.Count);
  938. }
  939. }
  940. }
  941. }