2
0

DataList.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. //
  2. // System.Web.UI.WebControls.DataList.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. using System.ComponentModel.Design;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.Web.Util;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [Designer("System.Web.UI.Design.WebControls.DataListDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  41. [Editor ("System.Web.UI.Design.WebControls.DataListComponentEditor, " + Consts.AssemblySystem_Design, typeof (ComponentEditor))]
  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. ArrayList itemsArray;
  71. DataListItemCollection items;
  72. bool extractTemplateRows;
  73. public DataList ()
  74. {
  75. }
  76. [DefaultValue (null)]
  77. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  78. [NotifyParentProperty (true)]
  79. [WebCategory ("Style")]
  80. [PersistenceMode (PersistenceMode.InnerProperty)]
  81. [WebSysDescription ("The style applied to alternating items.")]
  82. public virtual TableItemStyle AlternatingItemStyle {
  83. get {
  84. if (alternatingItemStyle == null) {
  85. alternatingItemStyle = new TableItemStyle ();
  86. if (IsTrackingViewState)
  87. alternatingItemStyle.TrackViewState ();
  88. }
  89. return alternatingItemStyle;
  90. }
  91. }
  92. [Browsable (false)]
  93. [DefaultValue (null)]
  94. [TemplateContainer (typeof (DataListItem))]
  95. [PersistenceMode (PersistenceMode.InnerProperty)]
  96. [WebSysDescription ("The template used for alternating items.")]
  97. public virtual ITemplate AlternatingItemTemplate {
  98. get { return alternatingItemTemplate; }
  99. set { alternatingItemTemplate = value; }
  100. }
  101. [DefaultValue (-1)]
  102. [WebCategory ("Misc")]
  103. [WebSysDescription ("The index of the item shown in edit mode.")]
  104. public virtual int EditItemIndex {
  105. get {
  106. object o = ViewState ["EditItemIndex"];
  107. if (o != null)
  108. return (int) o;
  109. return -1;
  110. }
  111. set {
  112. if (value < -1)
  113. throw new ArgumentOutOfRangeException("value");
  114. ViewState ["EditItemIndex"] = value;
  115. }
  116. }
  117. [DefaultValue (null)]
  118. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  119. [NotifyParentProperty (true)]
  120. [WebCategory ("Style")]
  121. [PersistenceMode (PersistenceMode.InnerProperty)]
  122. [WebSysDescription ("The style applied to items in edit mode.")]
  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 ("The template used for items in edit mode.")]
  138. public virtual ITemplate EditItemTemplate {
  139. get { return editItemTemplate; }
  140. set { editItemTemplate = value; }
  141. }
  142. [DefaultValue (false), WebCategory ("Misc")]
  143. [WebSysDescription ("Extract rows in the template.")]
  144. public virtual bool ExtractTemplateRows {
  145. get {
  146. object o = ViewState ["ExtractTemplateRows"];
  147. if (o != null)
  148. return (bool) o;
  149. return false;
  150. }
  151. set { ViewState ["ExtractTemplateRows"] = value; }
  152. }
  153. [DefaultValue (null)]
  154. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  155. [NotifyParentProperty (true)]
  156. [WebCategory ("Style")]
  157. [PersistenceMode (PersistenceMode.InnerProperty)]
  158. [WebSysDescription ("The style applied to the footer.")]
  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 ("The template used for the footer.")]
  174. public virtual ITemplate FooterTemplate {
  175. get { return footerTemplate; }
  176. set { footerTemplate = value; }
  177. }
  178. [DefaultValue (typeof (GridLines), "None")]
  179. public override GridLines GridLines {
  180. get { return base.GridLines; }
  181. set { base.GridLines = value; }
  182. }
  183. [DefaultValue (null)]
  184. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  185. [NotifyParentProperty (true)]
  186. [WebCategory ("Style")]
  187. [PersistenceMode (PersistenceMode.InnerProperty)]
  188. [WebSysDescription ("The style applied to the header.")]
  189. public virtual TableItemStyle HeaderStyle {
  190. get {
  191. if (headerStyle == null) {
  192. headerStyle = new TableItemStyle ();
  193. if (IsTrackingViewState)
  194. headerStyle.TrackViewState ();
  195. }
  196. return headerStyle;
  197. }
  198. }
  199. [Browsable (false)]
  200. [DefaultValue (null)]
  201. [TemplateContainer (typeof (DataListItem))]
  202. [PersistenceMode (PersistenceMode.InnerProperty)]
  203. [WebSysDescription ("The template used for the header.")]
  204. public virtual ITemplate HeaderTemplate {
  205. get { return headerTemplate; }
  206. set { headerTemplate = value; }
  207. }
  208. [Browsable (false)]
  209. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  210. [WebSysDescription ("A colletion containing all items.")]
  211. public virtual DataListItemCollection Items {
  212. get {
  213. if (items == null) {
  214. if (itemsArray == null) {
  215. EnsureChildControls ();
  216. itemsArray = new ArrayList ();
  217. }
  218. items = new DataListItemCollection (itemsArray);
  219. }
  220. return items;
  221. }
  222. }
  223. [DefaultValue (null)]
  224. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  225. [NotifyParentProperty (true)]
  226. [WebCategory ("Style")]
  227. [PersistenceMode (PersistenceMode.InnerProperty)]
  228. [WebSysDescription ("The style applied to items.")]
  229. public virtual TableItemStyle ItemStyle {
  230. get {
  231. if (itemStyle == null) {
  232. itemStyle = new TableItemStyle ();
  233. if (IsTrackingViewState)
  234. itemStyle.TrackViewState ();
  235. }
  236. return itemStyle;
  237. }
  238. }
  239. [Browsable (false)]
  240. [DefaultValue (null)]
  241. [TemplateContainer (typeof (DataListItem))]
  242. [PersistenceMode (PersistenceMode.InnerProperty)]
  243. [WebSysDescription ("The template used for items.")]
  244. public virtual ITemplate ItemTemplate {
  245. get { return itemTemplate; }
  246. set { itemTemplate = value; }
  247. }
  248. [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
  249. [WebSysDescription ("The number of columns that should be used.")]
  250. public virtual int RepeatColumns {
  251. get {
  252. object o = ViewState ["RepeatColumns"];
  253. if (o != null)
  254. return (int) o;
  255. return 0;
  256. }
  257. set {
  258. if (value < 0)
  259. throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");
  260. ViewState ["RepeatColumns"] = value;
  261. }
  262. }
  263. [DefaultValue (typeof (RepeatDirection), "Vertical"), Bindable (true), WebCategory ("Layout")]
  264. [WebSysDescription ("Which direction should be used when filling the columns.")]
  265. public virtual RepeatDirection RepeatDirection {
  266. get {
  267. object o = ViewState ["RepeatDirection"];
  268. if (o != null)
  269. return (RepeatDirection) o;
  270. return RepeatDirection.Vertical;
  271. }
  272. set {
  273. if (!Enum.IsDefined (typeof (RepeatDirection), value))
  274. throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
  275. ViewState ["RepeatDirection"] = value;
  276. }
  277. }
  278. [DefaultValue (typeof (RepeatLayout), "Table"), Bindable (true), WebCategory ("Layout")]
  279. [WebSysDescription ("The type of layout - mechanism that is used.")]
  280. public virtual RepeatLayout RepeatLayout {
  281. get {
  282. object o = ViewState ["RepeatLayout"];
  283. if (o != null)
  284. return (RepeatLayout) o;
  285. return RepeatLayout.Table;
  286. }
  287. set {
  288. if (!Enum.IsDefined (typeof (RepeatLayout), value))
  289. throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
  290. ViewState ["RepeatLayout"] = value;
  291. }
  292. }
  293. [DefaultValue (-1), Bindable (true)]
  294. [WebSysDescription ("The currently selected item index number.")]
  295. public virtual int SelectedIndex {
  296. get {
  297. object o = ViewState ["SelectedIndex"];
  298. if (o != null)
  299. return (int) o;
  300. return -1;
  301. }
  302. set {
  303. //FIXME: Looks like a bug in Microsoft's specs.
  304. // Exception is missing in document. I haven't tested the case
  305. // But I think exception should follow
  306. if (value < -1)
  307. throw new ArgumentOutOfRangeException("value");
  308. int prevSel = SelectedIndex;
  309. ViewState ["SelectedIndex"] = value;
  310. DataListItem prevSelItem;
  311. ListItemType liType;
  312. if (itemsArray != null) {
  313. if (prevSel >= 0 && prevSel < itemsArray.Count) {
  314. prevSelItem = (DataListItem) itemsArray [prevSel];
  315. if (prevSelItem.ItemType != ListItemType.EditItem) {
  316. liType = ((prevSel % 2) == 0 ? ListItemType.AlternatingItem :
  317. ListItemType.Item);
  318. prevSelItem.SetItemType (liType);
  319. }
  320. }
  321. if (value >= 0 && value < itemsArray.Count) {
  322. prevSelItem = (DataListItem) itemsArray [value];
  323. if (prevSelItem.ItemType != ListItemType.EditItem) {
  324. prevSelItem.SetItemType (ListItemType.SelectedItem);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. [Browsable (false)]
  331. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  332. [WebSysDescription ("The currently selected item.")]
  333. public virtual DataListItem SelectedItem {
  334. get {
  335. if (SelectedIndex == -1)
  336. return null;
  337. return Items [SelectedIndex];
  338. }
  339. }
  340. [DefaultValue (null)]
  341. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  342. [NotifyParentProperty (true)]
  343. [WebCategory ("Style")]
  344. [PersistenceMode (PersistenceMode.InnerProperty)]
  345. [WebSysDescription ("The style used for the currently selected item.")]
  346. public virtual TableItemStyle SelectedItemStyle {
  347. get {
  348. if (selectedItemStyle == null) {
  349. selectedItemStyle = new TableItemStyle ();
  350. if (IsTrackingViewState)
  351. selectedItemStyle.TrackViewState ();
  352. }
  353. return selectedItemStyle;
  354. }
  355. }
  356. [Browsable (false)]
  357. [DefaultValue (null)]
  358. [TemplateContainer (typeof (DataListItem))]
  359. [PersistenceMode (PersistenceMode.InnerProperty)]
  360. [WebSysDescription ("The template used for currently selected items.")]
  361. public virtual ITemplate SelectedItemTemplate {
  362. get { return selectedItemTemplate; }
  363. set { selectedItemTemplate = value; }
  364. }
  365. [DefaultValue (null)]
  366. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  367. [NotifyParentProperty (true)]
  368. [WebCategory ("Style")]
  369. [PersistenceMode (PersistenceMode.InnerProperty)]
  370. [WebSysDescription ("The style applied to separators.")]
  371. public virtual TableItemStyle SeparatorStyle {
  372. get {
  373. if (separatorStyle == null) {
  374. separatorStyle = new TableItemStyle ();
  375. if (IsTrackingViewState)
  376. separatorStyle.TrackViewState ();
  377. }
  378. return separatorStyle;
  379. }
  380. }
  381. [Browsable (false)]
  382. [DefaultValue (null)]
  383. [TemplateContainer (typeof (DataListItem))]
  384. [PersistenceMode (PersistenceMode.InnerProperty)]
  385. [WebSysDescription ("The template used for separators.")]
  386. public virtual ITemplate SeparatorTemplate {
  387. get { return separatorTemplate; }
  388. set { separatorTemplate = value; }
  389. }
  390. [DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
  391. [WebSysDescription ("Display the header for the DataList.")]
  392. public virtual bool ShowHeader {
  393. get {
  394. object o = ViewState ["ShowHeader"];
  395. if (o != null)
  396. return (bool) o;
  397. return true;
  398. }
  399. set { ViewState ["ShowHeader"] = value; }
  400. }
  401. [DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
  402. [WebSysDescription ("Display the footer for the DataList.")]
  403. public virtual bool ShowFooter {
  404. get {
  405. object o = ViewState ["ShowFooter"];
  406. if (o != null)
  407. return (bool) o;
  408. return true;
  409. }
  410. set
  411. {
  412. ViewState["ShowFooter"] = value;
  413. }
  414. }
  415. [WebCategory ("Action")]
  416. [WebSysDescription ("Raised when a cancel command is generated.")]
  417. public event DataListCommandEventHandler CancelCommand {
  418. add { Events.AddHandler (CancelCommandEvent, value); }
  419. remove { Events.RemoveHandler (CancelCommandEvent, value); }
  420. }
  421. [WebCategory ("Action")]
  422. [WebSysDescription ("Raised when a delete command is generated.")]
  423. public event DataListCommandEventHandler DeleteCommand {
  424. add { Events.AddHandler (DeleteCommandEvent, value); }
  425. remove { Events.RemoveHandler (DeleteCommandEvent, value); }
  426. }
  427. [WebCategory ("Action")]
  428. [WebSysDescription ("Raised when an edit command is generated.")]
  429. public event DataListCommandEventHandler EditCommand {
  430. add { Events.AddHandler (EditCommandEvent, value); }
  431. remove { Events.RemoveHandler (EditCommandEvent, value); }
  432. }
  433. [WebCategory ("Action")]
  434. [WebSysDescription ("Raised when an item command is generated.")]
  435. public event DataListCommandEventHandler ItemCommand {
  436. add { Events.AddHandler (ItemCommandEvent, value); }
  437. remove { Events.RemoveHandler (ItemCommandEvent, value); }
  438. }
  439. [WebCategory ("Behavior")]
  440. [WebSysDescription ("Raised when a new item is created.")]
  441. public event DataListItemEventHandler ItemCreated {
  442. add { Events.AddHandler (ItemCreatedEvent, value); }
  443. remove { Events.RemoveHandler (ItemCreatedEvent, value); }
  444. }
  445. [WebCategory ("Behavior")]
  446. [WebSysDescription ("Raised when a item gets data-bound.")]
  447. public event DataListItemEventHandler ItemDataBound {
  448. add { Events.AddHandler (ItemDataBoundEvent, value); }
  449. remove { Events.RemoveHandler (ItemDataBoundEvent, value); }
  450. }
  451. [WebCategory ("Action")]
  452. [WebSysDescription ("Raised when an update command is generated.")]
  453. public event DataListCommandEventHandler UpdateCommand {
  454. add { Events.AddHandler (UpdateCommandEvent, value); }
  455. remove { Events.RemoveHandler (UpdateCommandEvent, value); }
  456. }
  457. protected override Style CreateControlStyle ()
  458. {
  459. TableStyle retVal = new TableStyle (ViewState);
  460. retVal.CellSpacing = 0;
  461. return retVal;
  462. }
  463. protected override void LoadViewState (object savedState)
  464. {
  465. if (savedState == null)
  466. return;
  467. object [] states = (object []) savedState;
  468. if (states [0] != null)
  469. base.LoadViewState (states [0]);
  470. if (states [1] != null)
  471. alternatingItemStyle.LoadViewState (states [1]);
  472. if (states [2] != null)
  473. editItemStyle.LoadViewState (states [2]);
  474. if (states [3] != null)
  475. footerStyle.LoadViewState (states [3]);
  476. if (states [4] != null)
  477. headerStyle.LoadViewState (states [4]);
  478. if (states [5] != null)
  479. itemStyle.LoadViewState (states [5]);
  480. if (states [6] != null)
  481. selectedItemStyle.LoadViewState (states [6]);
  482. if (states [7] != null)
  483. separatorStyle.LoadViewState (states [7]);
  484. }
  485. protected override object SaveViewState()
  486. {
  487. object [] states = new object [8];
  488. states [0] = base.SaveViewState ();
  489. states [1] = (alternatingItemStyle == null ? null : alternatingItemStyle.SaveViewState ());
  490. states [2] = (editItemStyle == null ? null : editItemStyle.SaveViewState ());
  491. states [3] = (footerStyle == null ? null : footerStyle.SaveViewState ());
  492. states [4] = (headerStyle == null ? null : headerStyle.SaveViewState ());
  493. states [5] = (itemStyle == null ? null : itemStyle.SaveViewState ());
  494. states [6] = (selectedItemStyle == null ? null : selectedItemStyle.SaveViewState ());
  495. states [7] = (separatorStyle == null ? null : separatorStyle.SaveViewState ());
  496. return states;
  497. }
  498. protected override void TrackViewState ()
  499. {
  500. base.TrackViewState ();
  501. if (alternatingItemStyle != null)
  502. alternatingItemStyle.TrackViewState ();
  503. if (editItemStyle != null)
  504. editItemStyle.TrackViewState ();
  505. if (footerStyle != null)
  506. footerStyle.TrackViewState ();
  507. if (headerStyle != null)
  508. headerStyle.TrackViewState ();
  509. if (itemStyle != null)
  510. itemStyle.TrackViewState ();
  511. if (selectedItemStyle != null)
  512. selectedItemStyle.TrackViewState ();
  513. if (separatorStyle != null)
  514. separatorStyle.TrackViewState ();
  515. }
  516. protected override bool OnBubbleEvent (object source, EventArgs e)
  517. {
  518. if (!(e is DataListCommandEventArgs))
  519. return false;
  520. DataListCommandEventArgs args = (DataListCommandEventArgs) e;
  521. OnItemCommand (args);
  522. string cmdName = args.CommandName.ToLower ();
  523. if (cmdName == "cancel") {
  524. OnCancelCommand (args);
  525. } else if (cmdName == "delete") {
  526. OnDeleteCommand (args);
  527. } else if (cmdName == "edit") {
  528. OnEditCommand (args);
  529. } else if (cmdName == "select") {
  530. SelectedIndex = args.Item.ItemIndex;
  531. OnSelectedIndexChanged (EventArgs.Empty);
  532. } else if (cmdName == "update") {
  533. OnUpdateCommand (args);
  534. }
  535. return true;
  536. }
  537. void InvokeCommandEvent (DataListCommandEventArgs args, object key)
  538. {
  539. DataListCommandEventHandler dlceh = (DataListCommandEventHandler) Events [key];
  540. if (dlceh != null)
  541. dlceh (this, args);
  542. }
  543. void InvokeItemEvent (DataListItemEventArgs args, object key)
  544. {
  545. DataListItemEventHandler dlieh = (DataListItemEventHandler) Events [key];
  546. if (dlieh != null)
  547. dlieh (this, args);
  548. }
  549. protected virtual void OnCancelCommand (DataListCommandEventArgs e)
  550. {
  551. InvokeCommandEvent (e, CancelCommandEvent);
  552. }
  553. protected virtual void OnDeleteCommand (DataListCommandEventArgs e)
  554. {
  555. InvokeCommandEvent (e, DeleteCommandEvent);
  556. }
  557. protected virtual void OnEditCommand (DataListCommandEventArgs e)
  558. {
  559. InvokeCommandEvent (e, EditCommandEvent);
  560. }
  561. protected virtual void OnItemCommand (DataListCommandEventArgs e)
  562. {
  563. InvokeCommandEvent (e, ItemCommandEvent);
  564. }
  565. protected virtual void OnItemCreated (DataListItemEventArgs e)
  566. {
  567. InvokeItemEvent (e, ItemCreatedEvent);
  568. }
  569. protected virtual void OnItemDataBound (DataListItemEventArgs e)
  570. {
  571. InvokeItemEvent (e, ItemDataBoundEvent);
  572. }
  573. protected virtual void OnUpdateCommand (DataListCommandEventArgs e)
  574. {
  575. InvokeCommandEvent (e, UpdateCommandEvent);
  576. }
  577. protected override void RenderContents (HtmlTextWriter writer)
  578. {
  579. if (Controls.Count == 0)
  580. return;
  581. RepeatInfo repeater = new RepeatInfo ();
  582. Table templateTable = null;
  583. if (extractTemplateRows) {
  584. repeater.RepeatDirection = RepeatDirection.Vertical;
  585. repeater.RepeatLayout = RepeatLayout.Flow;
  586. repeater.RepeatColumns = 1;
  587. repeater.OuterTableImplied = true;
  588. templateTable = new Table ();
  589. templateTable.ID = ClientID;
  590. templateTable.CopyBaseAttributes (this);
  591. templateTable.ApplyStyle (ControlStyle);
  592. templateTable.RenderBeginTag (writer);
  593. } else {
  594. repeater.RepeatDirection = RepeatDirection;
  595. repeater.RepeatLayout = RepeatLayout;
  596. repeater.RepeatColumns = RepeatColumns;
  597. }
  598. repeater.RenderRepeater (writer, this, ControlStyle, this);
  599. if (templateTable != null) {
  600. templateTable.RenderEndTag (writer);
  601. }
  602. }
  603. private DataListItem GetItem (ListItemType itemType, int repeatIndex)
  604. {
  605. DataListItem retVal = null;
  606. switch (itemType) {
  607. case ListItemType.Header:
  608. retVal = (DataListItem) Controls [0];
  609. break;
  610. case ListItemType.Footer:
  611. retVal = (DataListItem) Controls [Controls.Count - 1];
  612. break;
  613. case ListItemType.Item:
  614. goto case ListItemType.EditItem;
  615. case ListItemType.AlternatingItem:
  616. goto case ListItemType.EditItem;
  617. case ListItemType.SelectedItem:
  618. goto case ListItemType.EditItem;
  619. case ListItemType.EditItem:
  620. retVal = (DataListItem) itemsArray [repeatIndex];
  621. break;
  622. case ListItemType.Separator:
  623. int index = 2 * repeatIndex + 1;
  624. if (headerTemplate != null)
  625. index ++;
  626. retVal = (DataListItem) Controls [index];
  627. break;
  628. }
  629. return retVal;
  630. }
  631. /// <summary>
  632. /// Undocumented
  633. /// </summary>
  634. protected override void CreateControlHierarchy (bool useDataSource)
  635. {
  636. IEnumerable source = null;
  637. ArrayList dkeys = DataKeysArray;
  638. if (itemsArray != null) {
  639. itemsArray.Clear ();
  640. } else {
  641. itemsArray = new ArrayList ();
  642. }
  643. extractTemplateRows = ExtractTemplateRows;
  644. if (!useDataSource) {
  645. int count = (int) ViewState ["_!ItemCount"];
  646. if (count != -1) {
  647. source = new DataSourceInternal (count);
  648. itemsArray.Capacity = count;
  649. }
  650. } else {
  651. dkeys.Clear ();
  652. source = GetResolvedDataSource ();
  653. if (source is ICollection) {
  654. dkeys.Capacity = ((ICollection) source).Count;
  655. itemsArray.Capacity = ((ICollection) source).Count;
  656. }
  657. }
  658. int itemCount = 0;
  659. if (source != null) {
  660. int index = 0;
  661. int editIndex = EditItemIndex;
  662. int selIndex = SelectedIndex;
  663. string dataKey = DataKeyField;
  664. if (headerTemplate != null)
  665. CreateItem (-1, ListItemType.Header, useDataSource, null);
  666. foreach (object current in source) {
  667. if (useDataSource) {
  668. try {
  669. dkeys.Add (DataBinder.GetPropertyValue (current, dataKey));
  670. } catch {
  671. dkeys.Add (dkeys.Count);
  672. }
  673. }
  674. ListItemType type = ListItemType.Item;
  675. if (index == editIndex) {
  676. type = ListItemType.EditItem;
  677. } else if (index == selIndex) {
  678. type = ListItemType.SelectedItem;
  679. } else if ((index % 2) != 0) {
  680. type = ListItemType.AlternatingItem;
  681. }
  682. itemsArray.Add (CreateItem (index, type, useDataSource, current));
  683. if (separatorTemplate != null)
  684. CreateItem (index, ListItemType.Separator, useDataSource, null);
  685. itemCount++;
  686. index++;
  687. }
  688. if (footerTemplate != null)
  689. CreateItem (-1, ListItemType.Footer, useDataSource, null);
  690. }
  691. if (useDataSource)
  692. ViewState ["_!ItemCount"] = (source != null ? itemCount : -1);
  693. }
  694. /// <summary>
  695. /// Undocumented
  696. /// </summary>
  697. protected virtual DataListItem CreateItem (int itemIndex, ListItemType itemType)
  698. {
  699. return new DataListItem (itemIndex, itemType);
  700. }
  701. private DataListItem CreateItem (int itemIndex, ListItemType itemType, bool dataBind, object dataItem)
  702. {
  703. DataListItem retVal = CreateItem (itemIndex, itemType);
  704. DataListItemEventArgs e = new DataListItemEventArgs (retVal);
  705. InitializeItem (retVal);
  706. if (dataBind)
  707. retVal.DataItem = dataItem;
  708. OnItemCreated (e);
  709. Controls.Add (retVal);
  710. if (dataBind) {
  711. retVal.DataBind ();
  712. OnItemDataBound (e);
  713. retVal.DataItem = null;
  714. }
  715. return retVal;
  716. }
  717. /// <summary>
  718. /// Undocumented
  719. /// </summary>
  720. protected override void PrepareControlHierarchy ()
  721. {
  722. if (Controls.Count == 0)
  723. return;
  724. Style defaultStyle = null;
  725. Style rowStyle = null;
  726. if (alternatingItemStyle != null) {
  727. defaultStyle = new TableItemStyle ();
  728. defaultStyle.CopyFrom (itemStyle);
  729. defaultStyle.CopyFrom (alternatingItemStyle);
  730. } else {
  731. defaultStyle = itemStyle;
  732. }
  733. foreach (DataListItem current in Controls) {
  734. rowStyle = null;
  735. switch (current.ItemType) {
  736. case ListItemType.Header:
  737. if (headerStyle != null)
  738. rowStyle = headerStyle;
  739. break;
  740. case ListItemType.Footer:
  741. if (footerStyle != null)
  742. rowStyle = footerStyle;
  743. break;
  744. case ListItemType.Separator:
  745. rowStyle = separatorStyle;
  746. break;
  747. case ListItemType.Item:
  748. rowStyle = itemStyle;
  749. break;
  750. case ListItemType.AlternatingItem:
  751. rowStyle = defaultStyle;
  752. break;
  753. case ListItemType.SelectedItem:
  754. rowStyle = new TableItemStyle ();
  755. if ((current.ItemIndex % 2) == 0) {
  756. rowStyle.CopyFrom (itemStyle);
  757. } else {
  758. rowStyle.CopyFrom (defaultStyle);
  759. }
  760. rowStyle.CopyFrom (selectedItemStyle);
  761. break;
  762. case ListItemType.EditItem:
  763. rowStyle = new TableItemStyle ();
  764. if ((current.ItemIndex % 2) == 0) {
  765. rowStyle.CopyFrom (itemStyle);
  766. } else {
  767. rowStyle.CopyFrom (defaultStyle);
  768. }
  769. if (current.ItemIndex == SelectedIndex)
  770. rowStyle.CopyFrom (selectedItemStyle);
  771. rowStyle.CopyFrom (editItemStyle);
  772. break;
  773. }
  774. if (rowStyle == null)
  775. continue;
  776. if (!extractTemplateRows) {
  777. current.MergeStyle (rowStyle);
  778. continue;
  779. }
  780. foreach (Control currentCtrl in current.Controls) {
  781. if (!(currentCtrl is Table))
  782. continue;
  783. foreach (TableRow cRow in ((Table) currentCtrl).Rows)
  784. cRow.MergeStyle (rowStyle);
  785. }
  786. }
  787. }
  788. /// <summary>
  789. /// Undocumented
  790. /// </summary>
  791. protected virtual void InitializeItem (DataListItem item)
  792. {
  793. ListItemType type = item.ItemType;
  794. ITemplate template = itemTemplate;
  795. switch (type) {
  796. case ListItemType.Header:
  797. template = headerTemplate;
  798. break;
  799. case ListItemType.Footer:
  800. template = footerTemplate;
  801. break;
  802. case ListItemType.AlternatingItem:
  803. if (alternatingItemTemplate != null)
  804. template = alternatingItemTemplate;
  805. break;
  806. case ListItemType.SelectedItem:
  807. if (selectedItemTemplate != null) {
  808. template = selectedItemTemplate;
  809. break;
  810. }
  811. if ((item.ItemIndex % 2) != 0)
  812. goto case ListItemType.AlternatingItem;
  813. break;
  814. case ListItemType.EditItem:
  815. if (editItemTemplate != null) {
  816. template = editItemTemplate;
  817. break;
  818. }
  819. if (item.ItemIndex == SelectedIndex)
  820. goto case ListItemType.SelectedItem;
  821. if ((item.ItemIndex % 2) != 0)
  822. goto case ListItemType.AlternatingItem;
  823. break;
  824. case ListItemType.Separator:
  825. template = separatorTemplate;
  826. break;
  827. }
  828. if (template != null)
  829. template.InstantiateIn (item);
  830. }
  831. bool IRepeatInfoUser.HasFooter {
  832. get { return (ShowFooter && footerTemplate != null); }
  833. }
  834. bool IRepeatInfoUser.HasHeader {
  835. get { return (ShowHeader && headerTemplate != null); }
  836. }
  837. bool IRepeatInfoUser.HasSeparators {
  838. get { return (separatorTemplate != null); }
  839. }
  840. int IRepeatInfoUser.RepeatedItemCount {
  841. get {
  842. if (itemsArray != null)
  843. return itemsArray.Count;
  844. return 0;
  845. }
  846. }
  847. void IRepeatInfoUser.RenderItem (ListItemType itemType,
  848. int repeatIndex,
  849. RepeatInfo repeatInfo,
  850. HtmlTextWriter writer)
  851. {
  852. DataListItem item = GetItem (itemType, repeatIndex);
  853. if (item != null)
  854. item.RenderItem (writer,
  855. extractTemplateRows,
  856. (repeatInfo.RepeatLayout == RepeatLayout.Table));
  857. }
  858. Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
  859. {
  860. DataListItem item = GetItem (itemType, repeatIndex);
  861. if (item == null || !item.ControlStyleCreated)
  862. return null;
  863. return item.ControlStyle;
  864. }
  865. }
  866. }