DataList.cs 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. if (current.HasControls ()) {
  781. int len = current.Controls.Count;
  782. for (int i = 0 ; i < len ; i++) {
  783. Control currentCtrl = current.Controls [i];
  784. if (!(currentCtrl is Table))
  785. continue;
  786. foreach (TableRow cRow in ((Table) currentCtrl).Rows)
  787. cRow.MergeStyle (rowStyle);
  788. }
  789. }
  790. }
  791. }
  792. /// <summary>
  793. /// Undocumented
  794. /// </summary>
  795. protected virtual void InitializeItem (DataListItem item)
  796. {
  797. ListItemType type = item.ItemType;
  798. ITemplate template = itemTemplate;
  799. switch (type) {
  800. case ListItemType.Header:
  801. template = headerTemplate;
  802. break;
  803. case ListItemType.Footer:
  804. template = footerTemplate;
  805. break;
  806. case ListItemType.AlternatingItem:
  807. if (alternatingItemTemplate != null)
  808. template = alternatingItemTemplate;
  809. break;
  810. case ListItemType.SelectedItem:
  811. if (selectedItemTemplate != null) {
  812. template = selectedItemTemplate;
  813. break;
  814. }
  815. if ((item.ItemIndex % 2) != 0)
  816. goto case ListItemType.AlternatingItem;
  817. break;
  818. case ListItemType.EditItem:
  819. if (editItemTemplate != null) {
  820. template = editItemTemplate;
  821. break;
  822. }
  823. if (item.ItemIndex == SelectedIndex)
  824. goto case ListItemType.SelectedItem;
  825. if ((item.ItemIndex % 2) != 0)
  826. goto case ListItemType.AlternatingItem;
  827. break;
  828. case ListItemType.Separator:
  829. template = separatorTemplate;
  830. break;
  831. }
  832. if (template != null)
  833. template.InstantiateIn (item);
  834. }
  835. bool IRepeatInfoUser.HasFooter {
  836. get { return (ShowFooter && footerTemplate != null); }
  837. }
  838. bool IRepeatInfoUser.HasHeader {
  839. get { return (ShowHeader && headerTemplate != null); }
  840. }
  841. bool IRepeatInfoUser.HasSeparators {
  842. get { return (separatorTemplate != null); }
  843. }
  844. int IRepeatInfoUser.RepeatedItemCount {
  845. get {
  846. if (itemsArray != null)
  847. return itemsArray.Count;
  848. return 0;
  849. }
  850. }
  851. void IRepeatInfoUser.RenderItem (ListItemType itemType,
  852. int repeatIndex,
  853. RepeatInfo repeatInfo,
  854. HtmlTextWriter writer)
  855. {
  856. DataListItem item = GetItem (itemType, repeatIndex);
  857. if (item != null)
  858. item.RenderItem (writer,
  859. extractTemplateRows,
  860. (repeatInfo.RepeatLayout == RepeatLayout.Table));
  861. }
  862. Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
  863. {
  864. DataListItem item = GetItem (itemType, repeatIndex);
  865. if (item == null || !item.ControlStyleCreated)
  866. return null;
  867. return item.ControlStyle;
  868. }
  869. }
  870. }