ListView.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. //
  2. // System.Web.UI.WebControls.ListView
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007-2008 Novell, Inc
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_3_5
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Collections.Specialized;
  34. using System.ComponentModel;
  35. using System.Drawing;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.HtmlControls;
  39. namespace System.Web.UI.WebControls
  40. {
  41. [DefaultEventAttribute ("SelectedIndexChanged")]
  42. [ControlValuePropertyAttribute ("SelectedValue")]
  43. [DesignerAttribute ("System.Web.UI.Design.WebControls.ListViewDesigner, System.Web.Extensions.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  44. [SupportsEventValidationAttribute ()]
  45. [ToolboxBitmapAttribute (typeof (ListView), "ListView.ico")]
  46. [DefaultPropertyAttribute ("SelectedValue")]
  47. public class ListView : DataBoundControl, INamingContainer, IPageableItemContainer
  48. {
  49. const int CSTATE_BASE_STATE = 0;
  50. const int CSTATE_DATAKEYNAMES = 1;
  51. const int CSTATE_DATAKEYSSTATE = 2;
  52. const int CSTATE_GROUPITEMCOUNT = 3;
  53. const int CSTATE_TOTALROWCOUNT = 4;
  54. const int CSTATE_EDITINDEX = 5;
  55. const int CSTATE_SELECTEDINDEX = 6;
  56. const int CSTATE_SORTDIRECTION = 7;
  57. const int CSTATE_SORTEXPRESSION = 8;
  58. const int CSTATE_COUNT = 9;
  59. ITemplate _emptyDataTemplate;
  60. ITemplate _emptyItemTemplate;
  61. ITemplate _insertItemTemplate;
  62. ITemplate _groupSeparatorTemplate;
  63. ITemplate _groupTemplate;
  64. ITemplate _itemSeparatorTemplate;
  65. ITemplate _itemTemplate;
  66. ITemplate _selectedItemTemplate;
  67. ITemplate _alternatingItemTemplate;
  68. ITemplate _editItemTemplate;
  69. ITemplate _layoutTemplate;
  70. int _totalRowCount;
  71. int _startRowIndex = -1;
  72. int _maximumRows = -1;
  73. int _selectedIndex;
  74. int _editIndex;
  75. int _groupItemCount;
  76. string [] _dataKeyNames;
  77. DataKeyArray _dataKeys;
  78. ArrayList _dataKeyArray;
  79. SortDirection _sortDirection = SortDirection.Ascending;
  80. string _sortExpression = String.Empty;
  81. Control _layoutTemplatePlaceholder;
  82. Control _nonGroupedItemsContainer;
  83. int _nonGroupedItemsContainerFirstItemIndex = -1;
  84. int _nonGroupedItemsContainerItemCount;
  85. IOrderedDictionary _lastInsertValues;
  86. #region Events
  87. // Event keys
  88. static readonly object ItemCancellingEvent = new object ();
  89. static readonly object ItemCommandEvent = new object ();
  90. static readonly object ItemCreatedEvent = new object ();
  91. static readonly object ItemDataBoundEvent = new object ();
  92. static readonly object ItemDeletedEvent = new object ();
  93. static readonly object ItemDeletingEvent = new object ();
  94. static readonly object ItemEditingEvent = new object ();
  95. static readonly object ItemInsertedEvent = new object ();
  96. static readonly object ItemInsertingEvent = new object ();
  97. static readonly object ItemUpdatedEvent = new object ();
  98. static readonly object ItemUpdatingEvent = new object ();
  99. static readonly object LayoutCreatedEvent = new object ();
  100. static readonly object PagePropertiesChangedEvent = new object ();
  101. static readonly object PagePropertiesChangingEvent = new object ();
  102. static readonly object SelectedIndexChangedEvent = new object ();
  103. static readonly object SelectedIndexChangingEvent = new object ();
  104. static readonly object SortedEvent = new object ();
  105. static readonly object SortingEvent = new object ();
  106. static readonly object TotalRowCountAvailableEvent = new object ();
  107. [CategoryAttribute ("Action")]
  108. public event EventHandler <ListViewCancelEventArgs> ItemCanceling {
  109. add { Events.AddHandler (ItemCancellingEvent, value); }
  110. remove { Events.RemoveHandler (ItemCancellingEvent, value); }
  111. }
  112. [CategoryAttribute ("Action")]
  113. public event EventHandler <ListViewCommandEventArgs> ItemCommand {
  114. add { Events.AddHandler (ItemCommandEvent, value); }
  115. remove { Events.RemoveHandler (ItemCommandEvent, value); }
  116. }
  117. [CategoryAttribute ("Behavior")]
  118. public event EventHandler <ListViewItemEventArgs> ItemCreated {
  119. add { Events.AddHandler (ItemCreatedEvent, value); }
  120. remove { Events.RemoveHandler (ItemCreatedEvent, value); }
  121. }
  122. [CategoryAttribute ("Data")]
  123. public event EventHandler <ListViewItemEventArgs> ItemDataBound {
  124. add { Events.AddHandler (ItemDataBoundEvent, value); }
  125. remove { Events.RemoveHandler (ItemDataBoundEvent, value); }
  126. }
  127. [CategoryAttribute ("Action")]
  128. public event EventHandler <ListViewDeletedEventArgs> ItemDeleted {
  129. add { Events.AddHandler (ItemDeletedEvent, value); }
  130. remove { Events.RemoveHandler (ItemDeletedEvent, value); }
  131. }
  132. [CategoryAttribute ("Action")]
  133. public event EventHandler <ListViewDeleteEventArgs> ItemDeleting {
  134. add { Events.AddHandler (ItemDeletingEvent, value); }
  135. remove { Events.RemoveHandler (ItemDeletingEvent, value); }
  136. }
  137. [CategoryAttribute ("Action")]
  138. public event EventHandler <ListViewEditEventArgs> ItemEditing {
  139. add { Events.AddHandler (ItemEditingEvent, value); }
  140. remove { Events.RemoveHandler (ItemEditingEvent, value); }
  141. }
  142. [CategoryAttribute ("Action")]
  143. public event EventHandler <ListViewInsertedEventArgs> ItemInserted {
  144. add { Events.AddHandler (ItemInsertedEvent, value); }
  145. remove { Events.RemoveHandler (ItemInsertedEvent, value); }
  146. }
  147. [CategoryAttribute ("Action")]
  148. public event EventHandler <ListViewInsertEventArgs> ItemInserting {
  149. add { Events.AddHandler (ItemInsertingEvent, value); }
  150. remove { Events.RemoveHandler (ItemInsertingEvent, value); }
  151. }
  152. [CategoryAttribute ("Action")]
  153. public event EventHandler <ListViewUpdatedEventArgs> ItemUpdated {
  154. add { Events.AddHandler (ItemUpdatedEvent, value); }
  155. remove { Events.RemoveHandler (ItemUpdatedEvent, value); }
  156. }
  157. [CategoryAttribute ("Action")]
  158. public event EventHandler <ListViewUpdateEventArgs> ItemUpdating {
  159. add { Events.AddHandler (ItemUpdatingEvent, value); }
  160. remove { Events.RemoveHandler (ItemUpdatingEvent, value); }
  161. }
  162. [CategoryAttribute ("Behavior")]
  163. public event EventHandler LayoutCreated {
  164. add { Events.AddHandler (LayoutCreatedEvent, value); }
  165. remove { Events.RemoveHandler (LayoutCreatedEvent, value); }
  166. }
  167. [CategoryAttribute ("Behavior")]
  168. public event EventHandler PagePropertiesChanged {
  169. add { Events.AddHandler (PagePropertiesChangedEvent, value); }
  170. remove { Events.RemoveHandler (PagePropertiesChangedEvent, value); }
  171. }
  172. [CategoryAttribute ("Behavior")]
  173. public event EventHandler <PagePropertiesChangingEventArgs> PagePropertiesChanging {
  174. add { Events.AddHandler (PagePropertiesChangingEvent, value); }
  175. remove { Events.RemoveHandler (PagePropertiesChangingEvent, value); }
  176. }
  177. [CategoryAttribute ("Action")]
  178. public event EventHandler SelectedIndexChanged {
  179. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  180. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  181. }
  182. [CategoryAttribute ("Action")]
  183. public event EventHandler <ListViewSelectEventArgs> SelectedIndexChanging {
  184. add { Events.AddHandler (SelectedIndexChangingEvent, value); }
  185. remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
  186. }
  187. [CategoryAttribute ("Action")]
  188. public event EventHandler Sorted {
  189. add { Events.AddHandler (SortedEvent, value); }
  190. remove { Events.RemoveHandler (SortedEvent, value); }
  191. }
  192. [CategoryAttribute ("Action")]
  193. public event EventHandler <ListViewSortEventArgs> Sorting {
  194. add { Events.AddHandler (SortingEvent, value); }
  195. remove { Events.RemoveHandler (SortingEvent, value); }
  196. }
  197. event EventHandler <PageEventArgs> IPageableItemContainer.TotalRowCountAvailable {
  198. add { Events.AddHandler (TotalRowCountAvailableEvent, value); }
  199. remove { Events.RemoveHandler (TotalRowCountAvailableEvent, value); }
  200. }
  201. #endregion
  202. #region Properties
  203. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  204. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  205. [BrowsableAttribute (false)]
  206. public override string AccessKey {
  207. get { return base.AccessKey; }
  208. set { throw StylingNotSupported (); }
  209. }
  210. [TemplateContainerAttribute (typeof (ListViewDataItem), BindingDirection.TwoWay)]
  211. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  212. [DefaultValue (null)]
  213. [BrowsableAttribute (false)]
  214. public virtual ITemplate AlternatingItemTemplate {
  215. get { return _alternatingItemTemplate; }
  216. set { _alternatingItemTemplate = value; }
  217. }
  218. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  219. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  220. [BrowsableAttribute (false)]
  221. public override Color BackColor {
  222. get { return base.BackColor; }
  223. set { throw StylingNotSupported (); }
  224. }
  225. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  226. [BrowsableAttribute (false)]
  227. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  228. public override Color BorderColor {
  229. get { return base.BorderColor; }
  230. set { throw StylingNotSupported (); }
  231. }
  232. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  233. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  234. [BrowsableAttribute (false)]
  235. public override BorderStyle BorderStyle {
  236. get { return base.BorderStyle; }
  237. set { throw StylingNotSupported (); }
  238. }
  239. [BrowsableAttribute (false)]
  240. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  241. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  242. public override Unit BorderWidth {
  243. get { return base.BorderWidth; }
  244. set { throw StylingNotSupported (); }
  245. }
  246. public override ControlCollection Controls {
  247. get {
  248. EnsureChildControls ();
  249. return base.Controls;
  250. }
  251. }
  252. [CategoryAttribute ("Behavior")]
  253. [DefaultValue (true)]
  254. public virtual bool ConvertEmptyStringToNull {
  255. get {
  256. object o = ViewState ["ConvertEmptyStringToNull"];
  257. if (o != null)
  258. return (bool) o;
  259. return true;
  260. }
  261. set { ViewState ["ConvertEmptyStringToNull"] = value; }
  262. }
  263. public override string CssClass {
  264. get { return base.CssClass; }
  265. set { throw StylingNotSupported (); }
  266. }
  267. [DefaultValue (null)]
  268. [TypeConverterAttribute (typeof (StringArrayConverter))]
  269. [CategoryAttribute ("Data")]
  270. public virtual string [] DataKeyNames {
  271. get {
  272. if (_dataKeyNames != null)
  273. return _dataKeyNames;
  274. return new string [0];
  275. }
  276. set {
  277. if (value == null)
  278. _dataKeyNames = null;
  279. else
  280. _dataKeyNames = (string []) value.Clone ();
  281. // They will eventually be recreated while creating the child controls
  282. _dataKeyArray = null;
  283. _dataKeys = null;
  284. if (Initialized)
  285. RequiresDataBinding = true;
  286. }
  287. }
  288. [BrowsableAttribute (false)]
  289. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  290. public virtual DataKeyArray DataKeys {
  291. get {
  292. if (_dataKeys == null) {
  293. _dataKeys = new DataKeyArray (DataKeyArray);
  294. if (IsTrackingViewState)
  295. ((IStateManager) _dataKeys).TrackViewState ();
  296. }
  297. return _dataKeys;
  298. }
  299. }
  300. ArrayList DataKeyArray {
  301. get {
  302. if (_dataKeyArray == null)
  303. _dataKeyArray = new ArrayList ();
  304. return _dataKeyArray;
  305. }
  306. }
  307. [DefaultValue (-1)]
  308. [CategoryAttribute ("Misc")]
  309. public virtual int EditIndex {
  310. get { return _editIndex; }
  311. set {
  312. if (value < -1)
  313. throw new ArgumentOutOfRangeException ("value");
  314. if (value != _editIndex) {
  315. _editIndex = value;
  316. if (Initialized)
  317. RequiresDataBinding = true;
  318. }
  319. }
  320. }
  321. [BrowsableAttribute (false)]
  322. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  323. public virtual ListViewItem EditItem {
  324. get { throw new NotImplementedException (); }
  325. }
  326. [DefaultValue (null)]
  327. [BrowsableAttribute (false)]
  328. [TemplateContainerAttribute (typeof (ListViewDataItem), BindingDirection.TwoWay)]
  329. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  330. public virtual ITemplate EditItemTemplate {
  331. get { return _editItemTemplate; }
  332. set { _editItemTemplate = value; }
  333. }
  334. [DefaultValue (null)]
  335. [TemplateContainerAttribute (typeof (ListView))]
  336. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  337. [BrowsableAttribute (false)]
  338. public virtual ITemplate EmptyDataTemplate {
  339. get { return _emptyDataTemplate; }
  340. set { _emptyDataTemplate = value; }
  341. }
  342. [TemplateContainerAttribute (typeof (ListViewItem))]
  343. [DefaultValue (null)]
  344. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  345. [BrowsableAttribute (false)]
  346. public virtual ITemplate EmptyItemTemplate {
  347. get { return _emptyItemTemplate; }
  348. set { _emptyItemTemplate = value; }
  349. }
  350. [BrowsableAttribute (false)]
  351. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  352. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  353. public override FontInfo Font {
  354. get { throw StylingNotSupported (); }
  355. }
  356. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  357. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  358. [BrowsableAttribute (false)]
  359. public override Color ForeColor {
  360. get { return base.ForeColor; }
  361. set { throw StylingNotSupported (); }
  362. }
  363. [CategoryAttribute ("Misc")]
  364. [DefaultValue (1)]
  365. public virtual int GroupItemCount {
  366. get { return _groupItemCount; }
  367. set {
  368. if (value < 1)
  369. throw new ArgumentOutOfRangeException ("value");
  370. if (value != _groupItemCount) {
  371. _groupItemCount = value;
  372. if (Initialized)
  373. RequiresDataBinding = true;
  374. }
  375. }
  376. }
  377. [CategoryAttribute ("Behavior")]
  378. [DefaultValue ("groupPlaceholder")]
  379. public virtual string GroupPlaceholderID {
  380. get {
  381. string s = ViewState ["GroupPlaceholderID"] as string;
  382. if (s != null)
  383. return s;
  384. return "groupPlaceHolder";
  385. }
  386. set {
  387. if (String.IsNullOrEmpty (value))
  388. throw new ArgumentOutOfRangeException ("value");
  389. ViewState ["GroupPlaceholderID"] = value;
  390. }
  391. }
  392. [BrowsableAttribute (false)]
  393. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  394. [DefaultValue (null)]
  395. [TemplateContainerAttribute (typeof (ListViewItem))]
  396. public virtual ITemplate GroupSeparatorTemplate {
  397. get { return _groupSeparatorTemplate; }
  398. set { _groupSeparatorTemplate = value; }
  399. }
  400. [TemplateContainerAttribute (typeof (ListViewItem))]
  401. [DefaultValue (null)]
  402. [BrowsableAttribute (false)]
  403. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  404. public virtual ITemplate GroupTemplate {
  405. get { return _groupTemplate; }
  406. set { _groupTemplate = value; }
  407. }
  408. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  409. [BrowsableAttribute (false)]
  410. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  411. public override Unit Height {
  412. get { return base.Height; }
  413. set { throw StylingNotSupported (); }
  414. }
  415. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  416. [BrowsableAttribute (false)]
  417. public virtual ListViewItem InsertItem {
  418. get;
  419. private set;
  420. }
  421. [CategoryAttribute ("Misc")]
  422. [DefaultValue (InsertItemPosition.None)]
  423. public virtual InsertItemPosition InsertItemPosition {
  424. get;
  425. set;
  426. }
  427. [TemplateContainerAttribute (typeof (ListViewItem), BindingDirection.TwoWay)]
  428. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  429. [DefaultValue (null)]
  430. [BrowsableAttribute (false)]
  431. public virtual ITemplate InsertItemTemplate {
  432. get { return _insertItemTemplate; }
  433. set { _insertItemTemplate = value; }
  434. }
  435. [DefaultValue ("itemPlaceholder")]
  436. [CategoryAttribute ("Behavior")]
  437. public virtual string ItemPlaceholderID {
  438. get {
  439. string s = ViewState ["ItemPlaceHolderID"] as string;
  440. if (s != null)
  441. return s;
  442. return "itemPlaceholder";
  443. }
  444. set {
  445. if (String.IsNullOrEmpty (value))
  446. throw new ArgumentOutOfRangeException ("value");
  447. ViewState ["ItemPlaceHolderID"] = value;
  448. }
  449. }
  450. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  451. [BrowsableAttribute (false)]
  452. public virtual IList <ListViewDataItem> Items {
  453. get { throw new NotImplementedException (); }
  454. }
  455. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  456. [BrowsableAttribute (false)]
  457. [TemplateContainerAttribute (typeof (ListViewItem))]
  458. [DefaultValue (null)]
  459. public virtual ITemplate ItemSeparatorTemplate {
  460. get { return _itemSeparatorTemplate; }
  461. set { _itemSeparatorTemplate = value; }
  462. }
  463. [TemplateContainerAttribute (typeof (ListViewDataItem), BindingDirection.TwoWay)]
  464. [DefaultValue (null)]
  465. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  466. [BrowsableAttribute (false)]
  467. public virtual ITemplate ItemTemplate {
  468. get { return _itemTemplate; }
  469. set { _itemTemplate = value; }
  470. }
  471. [TemplateContainerAttribute (typeof (ListView))]
  472. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  473. [DefaultValue (null)]
  474. [BrowsableAttribute (false)]
  475. public virtual ITemplate LayoutTemplate {
  476. get { return _layoutTemplate; }
  477. set { _layoutTemplate = value; }
  478. }
  479. protected virtual int MaximumRows {
  480. get { return _maximumRows; }
  481. }
  482. [BrowsableAttribute (false)]
  483. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  484. public virtual DataKey SelectedDataKey {
  485. get {
  486. if (_dataKeyNames == null || _dataKeyNames.Length == 0)
  487. throw new InvalidOperationException ("No data keys are specified in the DataKeyNames property.");
  488. DataKeyArray dataKeys = DataKeys;
  489. int selIndex = SelectedIndex;
  490. if (selIndex > -1 || selIndex < dataKeys.Count)
  491. return dataKeys [selIndex];
  492. return null;
  493. }
  494. }
  495. [CategoryAttribute ("Misc")]
  496. [DefaultValue (-1)]
  497. public virtual int SelectedIndex {
  498. get { return _selectedIndex; }
  499. set {
  500. if (value < -1)
  501. throw new ArgumentOutOfRangeException ("value");
  502. if (value != _selectedIndex) {
  503. _selectedIndex = value;
  504. if (Initialized)
  505. RequiresDataBinding = true;
  506. }
  507. }
  508. }
  509. [BrowsableAttribute (false)]
  510. [DefaultValue (null)]
  511. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  512. [TemplateContainerAttribute (typeof (ListViewDataItem), BindingDirection.TwoWay)]
  513. public virtual ITemplate SelectedItemTemplate {
  514. get { return _selectedItemTemplate; }
  515. set { _selectedItemTemplate = value; }
  516. }
  517. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  518. [BrowsableAttribute (false)]
  519. public object SelectedValue {
  520. get {
  521. DataKey dk = SelectedDataKey;
  522. if (dk != null)
  523. return dk.Value;
  524. return null;
  525. }
  526. }
  527. [DefaultValue (SortDirection.Ascending)]
  528. [BrowsableAttribute (false)]
  529. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  530. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  531. public virtual SortDirection SortDirection {
  532. get { return _sortDirection; }
  533. }
  534. [BrowsableAttribute (false)]
  535. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  536. public virtual string SortExpression {
  537. get { return _sortExpression; }
  538. }
  539. protected virtual int StartRowIndex {
  540. get {
  541. if (_startRowIndex < 0)
  542. return 0;
  543. return _startRowIndex;
  544. }
  545. }
  546. int IPageableItemContainer.MaximumRows {
  547. get { return MaximumRows; }
  548. }
  549. int IPageableItemContainer.StartRowIndex {
  550. get { return StartRowIndex; }
  551. }
  552. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  553. [BrowsableAttribute (false)]
  554. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  555. public override short TabIndex {
  556. get { return 0; }
  557. set { throw new NotSupportedException ("ListView does not allow setting this property."); }
  558. }
  559. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  560. [BrowsableAttribute (false)]
  561. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  562. public override string ToolTip {
  563. get { return base.ToolTip; }
  564. set { throw StylingNotSupported (); }
  565. }
  566. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  567. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  568. [BrowsableAttribute (false)]
  569. public override Unit Width {
  570. get { return base.Width; }
  571. set { throw StylingNotSupported (); }
  572. }
  573. #endregion
  574. public ListView ()
  575. {
  576. InsertItemPosition = InsertItemPosition.None;
  577. ResetDefaults ();
  578. }
  579. void ResetDefaults ()
  580. {
  581. _totalRowCount = -1;
  582. _selectedIndex = -1;
  583. _editIndex = -1;
  584. _groupItemCount = -1;
  585. }
  586. protected virtual void AddControlToContainer (Control control, Control container, int addLocation)
  587. {
  588. if (control == null || container == null)
  589. return;
  590. Control ctl;
  591. if (container is HtmlTable) {
  592. ctl = new ListViewTableRow ();
  593. ctl.Controls.Add (control);
  594. } else
  595. ctl = control;
  596. container.Controls.AddAt (addLocation, ctl);
  597. }
  598. protected internal override void CreateChildControls ()
  599. {
  600. object itemCount = ViewState ["_!ItemCount"];
  601. if (itemCount != null) {
  602. if (RequiresDataBinding)
  603. EnsureDataBound ();
  604. int c = (int)itemCount;
  605. if (c >= 0) {
  606. // Fake data - we only need to make sure
  607. // OnTotalRowCountAvailable is called now - so that any
  608. // pagers can create child controls.
  609. object[] data = new object [c];
  610. CreateChildControls (data, false);
  611. }
  612. }
  613. base.CreateChildControls ();
  614. }
  615. protected virtual int CreateChildControls (IEnumerable dataSource, bool dataBinding)
  616. {
  617. IList <ListViewDataItem> retList = null;
  618. EnsureLayoutTemplate ();
  619. RemoveItems ();
  620. // If any of the _maximumRows or _startRowIndex is different to their
  621. // defaults, it means we are paging - i.e. SetPageProperties has been
  622. // called.
  623. bool haveDataToPage = _maximumRows > 0 || _startRowIndex > 0;
  624. var pagedDataSource = new ListViewPagedDataSource ();
  625. if (dataBinding) {
  626. DataSourceView view = GetData ();
  627. if (view == null)
  628. throw new InvalidOperationException ("dataSource returned a null reference for DataSourceView.");
  629. int totalRowCount = 0;
  630. if (haveDataToPage && view.CanPage) {
  631. if (view.CanRetrieveTotalRowCount)
  632. totalRowCount = SelectArguments.TotalRowCount;
  633. else {
  634. ICollection ds = dataSource as ICollection;
  635. if (ds == null)
  636. throw new InvalidOperationException ("dataSource does not implement the ICollection interface.");
  637. totalRowCount = ds.Count + StartRowIndex;
  638. }
  639. }
  640. pagedDataSource.TotalRowCount = totalRowCount;
  641. DataKeyArray.Clear ();
  642. } else {
  643. if (!(dataSource is ICollection))
  644. throw new InvalidOperationException ("dataSource does not implement the ICollection interface and dataBinding is false.");
  645. pagedDataSource.TotalRowCount = 0;
  646. }
  647. pagedDataSource.StartRowIndex = StartRowIndex;
  648. pagedDataSource.MaximumRows = MaximumRows;
  649. pagedDataSource.DataSource = dataSource;
  650. bool emptySet = false;
  651. if (dataSource != null) {
  652. if (GroupItemCount <= 0)
  653. retList = CreateItemsWithoutGroups (pagedDataSource, dataBinding, InsertItemPosition, DataKeyArray);
  654. if (InsertItemPosition != InsertItemPosition.None && (retList == null || (retList != null && retList.Count == 0)))
  655. emptySet = true;
  656. if (haveDataToPage)
  657. // Data source has paged data for us, so we must use its total row
  658. // count
  659. _totalRowCount = pagedDataSource.DataSourceCount;
  660. else if (!emptySet)
  661. _totalRowCount = retList.Count;
  662. else
  663. _totalRowCount = 0;
  664. OnTotalRowCountAvailable (new PageEventArgs (_startRowIndex, _maximumRows, _totalRowCount));
  665. } else
  666. emptySet = true;
  667. if (emptySet) {
  668. Controls.Clear ();
  669. CreateEmptyItem ();
  670. }
  671. if (retList == null)
  672. return 0;
  673. return _totalRowCount;
  674. }
  675. protected override Style CreateControlStyle ()
  676. {
  677. throw StylingNotSupported ();
  678. }
  679. protected virtual ListViewDataItem CreateDataItem (int dataItemIndex, int displayIndex)
  680. {
  681. return new ListViewDataItem (dataItemIndex, displayIndex);
  682. }
  683. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  684. {
  685. DataSourceSelectArguments arg = DataSourceSelectArguments.Empty;
  686. DataSourceView view = GetData();
  687. if (view.CanPage) {
  688. arg.StartRowIndex = _startRowIndex;
  689. if (view.CanRetrieveTotalRowCount) {
  690. arg.RetrieveTotalRowCount = true;
  691. arg.MaximumRows = _maximumRows;
  692. } else
  693. arg.MaximumRows = -1;
  694. }
  695. if (IsBoundUsingDataSourceID && !String.IsNullOrEmpty (_sortExpression)) {
  696. if (_sortDirection == SortDirection.Ascending)
  697. arg.SortExpression = _sortExpression;
  698. else
  699. arg.SortExpression = _sortExpression + " DESC";
  700. }
  701. return arg;
  702. }
  703. protected virtual void CreateEmptyDataItem ()
  704. {
  705. if (_emptyDataTemplate != null) {
  706. ListViewItem item = CreateItem (ListViewItemType.EmptyItem);
  707. InstantiateEmptyDataTemplate (item);
  708. OnItemCreated (new ListViewItemEventArgs (item));
  709. AddControlToContainer (item, this, 0);
  710. }
  711. }
  712. protected virtual ListViewItem CreateEmptyItem ()
  713. {
  714. if (_emptyDataTemplate != null) {
  715. ListViewItem item = CreateItem (ListViewItemType.EmptyItem);
  716. InstantiateEmptyItemTemplate (item);
  717. OnItemCreated (new ListViewItemEventArgs (item));
  718. return item;
  719. }
  720. return null;
  721. }
  722. protected virtual ListViewItem CreateInsertItem ()
  723. {
  724. ListViewItem ret = CreateItem (ListViewItemType.InsertItem);
  725. InsertItem = ret;
  726. return ret;
  727. }
  728. protected virtual ListViewItem CreateItem (ListViewItemType itemType)
  729. {
  730. return new ListViewItem (itemType);
  731. }
  732. protected virtual IList <ListViewDataItem> CreateItemsInGroups (ListViewPagedDataSource dataSource, bool dataBinding, InsertItemPosition insertPosition,
  733. ArrayList keyArray)
  734. {
  735. if (_groupTemplate == null)
  736. return null;
  737. throw new NotImplementedException ();
  738. }
  739. protected virtual IList <ListViewDataItem> CreateItemsWithoutGroups (ListViewPagedDataSource dataSource, bool dataBinding,
  740. InsertItemPosition insertPosition, ArrayList keyArray)
  741. {
  742. if (_nonGroupedItemsContainer == null)
  743. _nonGroupedItemsContainer = FindPlaceholder (ItemPlaceholderID, this);
  744. _nonGroupedItemsContainerItemCount = 0;
  745. if (_nonGroupedItemsContainer == null)
  746. throw new InvalidOperationException (
  747. String.Format ("An item placeholder must be specified on ListView '{0}'. Specify an item placeholder by setting a control's ID property to \"itemPlaceholder\". The item placeholder control must also specify runat=\"server\".", ID));
  748. Control parent = _nonGroupedItemsContainer.Parent;
  749. int ipos;
  750. if (_nonGroupedItemsContainerFirstItemIndex == -1) {
  751. ipos = 0;
  752. if (parent != null) {
  753. ipos = parent.Controls.IndexOf (_nonGroupedItemsContainer);
  754. parent.Controls.Remove (_nonGroupedItemsContainer);
  755. _nonGroupedItemsContainer = parent;
  756. if (_nonGroupedItemsContainer != _layoutTemplatePlaceholder)
  757. AddControlToContainer (_nonGroupedItemsContainer, _layoutTemplatePlaceholder, 0);
  758. }
  759. _nonGroupedItemsContainerFirstItemIndex = ipos;
  760. } else
  761. ipos = _nonGroupedItemsContainerFirstItemIndex;
  762. List <ListViewDataItem> ret = new List <ListViewDataItem> ();
  763. ListViewItem lvi;
  764. ListViewItem container;
  765. bool needSeparator = false;
  766. if (insertPosition == InsertItemPosition.FirstItem) {
  767. lvi = CreateInsertItem ();
  768. InstantiateInsertItemTemplate (lvi);
  769. AddControlToContainer (lvi, _nonGroupedItemsContainer, ipos++);
  770. _nonGroupedItemsContainerItemCount++;
  771. needSeparator = true;
  772. }
  773. bool haveSeparatorTemplate = _itemSeparatorTemplate != null;
  774. int displayIndex = 0;
  775. ListViewDataItem lvdi;
  776. int startIndex = dataSource.StartRowIndex;
  777. foreach (object item in dataSource) {
  778. if (needSeparator && haveSeparatorTemplate) {
  779. container = new ListViewItem ();
  780. InstantiateItemSeparatorTemplate (container);
  781. AddControlToContainer (container, _nonGroupedItemsContainer, ipos++);
  782. _nonGroupedItemsContainerItemCount++;
  783. }
  784. lvdi = CreateDataItem (startIndex + displayIndex, displayIndex);
  785. InstantiateItemTemplate (lvdi, displayIndex);
  786. if (dataBinding) {
  787. lvdi.DataItem = item;
  788. OrderedDictionary dict = new OrderedDictionary ();
  789. string[] dataKeyNames = DataKeyNames;
  790. foreach (string s in dataKeyNames)
  791. dict.Add (s, DataBinder.GetPropertyValue (item, s));
  792. DataKey dk = new DataKey (dict, dataKeyNames);
  793. if (keyArray.Count == displayIndex)
  794. keyArray.Add (dk);
  795. else
  796. keyArray [displayIndex] = dk;
  797. }
  798. OnItemCreated (new ListViewItemEventArgs (lvdi));
  799. AddControlToContainer (lvdi, _nonGroupedItemsContainer, ipos++);
  800. _nonGroupedItemsContainerItemCount++;
  801. if (!needSeparator)
  802. needSeparator = true;
  803. if (dataBinding) {
  804. lvdi.DataBind ();
  805. OnItemDataBound (new ListViewItemEventArgs (lvdi));
  806. }
  807. displayIndex++;
  808. ret.Add (lvdi);
  809. }
  810. if (insertPosition == InsertItemPosition.LastItem) {
  811. if (needSeparator && haveSeparatorTemplate) {
  812. container = new ListViewItem ();
  813. InstantiateItemSeparatorTemplate (container);
  814. AddControlToContainer (container, _nonGroupedItemsContainer, ipos++);
  815. _nonGroupedItemsContainerItemCount++;
  816. }
  817. lvi = CreateInsertItem ();
  818. InstantiateInsertItemTemplate (lvi);
  819. AddControlToContainer (lvi, _nonGroupedItemsContainer, ipos++);
  820. _nonGroupedItemsContainerItemCount++;
  821. }
  822. return ret;
  823. }
  824. protected virtual void CreateLayoutTemplate ()
  825. {
  826. if (_layoutTemplate != null) {
  827. _layoutTemplatePlaceholder = new Control ();
  828. _layoutTemplate.InstantiateIn (_layoutTemplatePlaceholder);
  829. Controls.Add (_layoutTemplatePlaceholder);
  830. }
  831. OnLayoutCreated (EventArgs.Empty);
  832. }
  833. public virtual void DeleteItem (int itemIndex)
  834. {
  835. }
  836. protected virtual void EnsureLayoutTemplate ()
  837. {
  838. if (Controls.Count != 0)
  839. return;
  840. CreateLayoutTemplate ();
  841. }
  842. public virtual void ExtractItemValues (IOrderedDictionary itemValues, ListViewItem item, bool includePrimaryKey)
  843. {
  844. if (itemValues == null)
  845. throw new ArgumentNullException ("itemValues");
  846. if (!(item is ListViewDataItem))
  847. throw new InvalidOperationException ("item is not a ListViewDataItem object.");
  848. }
  849. protected virtual Control FindPlaceholder (string containerID, Control container)
  850. {
  851. if (container == null || String.IsNullOrEmpty (containerID))
  852. return null;
  853. return container.FindControl (containerID);
  854. }
  855. public virtual void InsertNewItem (bool causesValidation)
  856. {
  857. ListViewItem insertItem = InsertItem;
  858. if (insertItem == null)
  859. throw new InvalidOperationException ("The ListView control does not have an insert item.");
  860. DataSourceView dsv = null;
  861. ListViewInsertEventArgs eventArgs = null;
  862. if (IsBoundUsingDataSourceID) {
  863. dsv = GetData ();
  864. if (dsv == null)
  865. throw new InvalidOperationException ("Missing data.");
  866. eventArgs = new ListViewInsertEventArgs (insertItem);
  867. ExtractItemValues (eventArgs.Values, insertItem, true);
  868. } else
  869. eventArgs = new ListViewInsertEventArgs (insertItem);
  870. OnItemInserting (eventArgs);
  871. if (!eventArgs.Cancel && IsBoundUsingDataSourceID) {
  872. _lastInsertValues = eventArgs.Values;
  873. dsv.Insert (_lastInsertValues, new DataSourceViewOperationCallback (InsertNewItemCallback));
  874. }
  875. }
  876. bool InsertNewItemCallback (int recordsAffected, Exception ex)
  877. {
  878. var eventArgs = new ListViewInsertedEventArgs (_lastInsertValues, recordsAffected, ex);
  879. OnItemInserted (eventArgs);
  880. _lastInsertValues = null;
  881. if (ex != null && !eventArgs.ExceptionHandled)
  882. return false;
  883. // This will effectively reset the insert values
  884. if (!eventArgs.KeepInInsertMode)
  885. RequiresDataBinding = true;
  886. return true;
  887. }
  888. protected virtual void InstantiateEmptyDataTemplate (Control container)
  889. {
  890. if (_emptyDataTemplate != null)
  891. _emptyDataTemplate.InstantiateIn (container);
  892. }
  893. protected virtual void InstantiateEmptyItemTemplate (Control container)
  894. {
  895. if (_emptyItemTemplate != null)
  896. _emptyItemTemplate.InstantiateIn (container);
  897. }
  898. protected virtual void InstantiateGroupSeparatorTemplate (Control container)
  899. {
  900. if (_groupSeparatorTemplate != null)
  901. _groupSeparatorTemplate.InstantiateIn (container);
  902. }
  903. protected virtual void InstantiateGroupTemplate (Control container)
  904. {
  905. if (_groupTemplate != null)
  906. _groupTemplate.InstantiateIn (container);
  907. }
  908. protected virtual void InstantiateInsertItemTemplate (Control container)
  909. {
  910. if (_insertItemTemplate != null)
  911. _insertItemTemplate.InstantiateIn (container);
  912. }
  913. protected virtual void InstantiateItemSeparatorTemplate (Control container)
  914. {
  915. if (_itemSeparatorTemplate != null)
  916. _itemSeparatorTemplate.InstantiateIn (container);
  917. }
  918. protected virtual void InstantiateItemTemplate (Control container, int displayIndex)
  919. {
  920. if (_itemTemplate == null)
  921. throw new InvalidOperationException ("ItemTemplate is missing");
  922. ITemplate template = _itemTemplate;
  923. if ((displayIndex % 2 != 0) && _alternatingItemTemplate != null)
  924. template = _alternatingItemTemplate;
  925. if ((displayIndex == _selectedIndex) && _selectedItemTemplate != null)
  926. template = _selectedItemTemplate;
  927. if ((displayIndex == _editIndex) && _editItemTemplate != null)
  928. template = _editItemTemplate;
  929. template.InstantiateIn (container);
  930. }
  931. void LoadDataKeysState (object savedState)
  932. {
  933. object[] state = savedState as object[];
  934. int len = state != null ? state.Length : 0;
  935. if (len == 0)
  936. return;
  937. ArrayList dataKeyArray = DataKeyArray;
  938. DataKey dk;
  939. string[] keyNames = DataKeyNames;
  940. for (int i = 0; i < len; i++) {
  941. dk = new DataKey (new OrderedDictionary (), keyNames);
  942. ((IStateManager)dk).LoadViewState (state [i]);
  943. dataKeyArray.Add (dk);
  944. }
  945. _dataKeys = null;
  946. }
  947. protected override void LoadControlState (object savedState)
  948. {
  949. ResetDefaults ();
  950. object[] state = savedState as object[];
  951. if (state == null || state.Length != CSTATE_COUNT)
  952. return;
  953. object o;
  954. base.LoadControlState (state [CSTATE_BASE_STATE]);
  955. if ((o = state [CSTATE_DATAKEYNAMES]) != null)
  956. DataKeyNames = (string[])o;
  957. LoadDataKeysState (state [CSTATE_DATAKEYSSTATE]);
  958. if ((o = state [CSTATE_GROUPITEMCOUNT]) != null)
  959. GroupItemCount = (int)o;
  960. if ((o = state [CSTATE_TOTALROWCOUNT]) != null)
  961. _totalRowCount = (int)o;
  962. if ((o = state [CSTATE_EDITINDEX]) != null)
  963. EditIndex = (int)o;
  964. if ((o = state [CSTATE_SELECTEDINDEX]) != null)
  965. SelectedIndex = (int)o;
  966. if ((o = state [CSTATE_SORTDIRECTION]) != null)
  967. _sortDirection = (SortDirection)o;
  968. if ((o = state [CSTATE_SORTEXPRESSION]) != null)
  969. _sortExpression = (string)o;
  970. }
  971. protected override void LoadViewState (object savedState)
  972. {
  973. object[] state = savedState as object[];
  974. int len = state != null ? state.Length : 0;
  975. if (len == 0)
  976. return;
  977. base.LoadViewState (state [0]);
  978. }
  979. protected override bool OnBubbleEvent (object source, EventArgs e)
  980. {
  981. ListViewCommandEventArgs args = e as ListViewCommandEventArgs;
  982. if (args != null) {
  983. bool causesValidation = false;
  984. IButtonControl button = args.CommandSource as IButtonControl;
  985. if (button != null && button.CausesValidation) {
  986. Page.Validate (button.ValidationGroup);
  987. causesValidation = true;
  988. }
  989. ProcessCommand (args, causesValidation);
  990. return true;
  991. }
  992. return base.OnBubbleEvent (source, e);
  993. }
  994. void ProcessCommand (ListViewCommandEventArgs args, bool causesValidation)
  995. {
  996. OnItemCommand (args);
  997. }
  998. protected override void OnInit (EventArgs e)
  999. {
  1000. Page.RegisterRequiresControlState (this);
  1001. base.OnInit (e);
  1002. }
  1003. void InvokeEvent <T> (object key, T args) where T : EventArgs
  1004. {
  1005. EventHandlerList events = Events;
  1006. if (events != null) {
  1007. EventHandler <T> eh = events [key] as EventHandler <T>;
  1008. if (eh != null)
  1009. eh (this, args);
  1010. }
  1011. }
  1012. void InvokeEvent (object key, EventArgs args)
  1013. {
  1014. EventHandlerList events = Events;
  1015. if (events != null) {
  1016. EventHandler eh = events [key] as EventHandler;
  1017. if (eh != null)
  1018. eh (this, args);
  1019. }
  1020. }
  1021. protected virtual void OnItemCanceling (ListViewCancelEventArgs e)
  1022. {
  1023. InvokeEvent <ListViewCancelEventArgs> (ItemCancellingEvent, e);
  1024. }
  1025. protected virtual void OnItemCommand (ListViewCommandEventArgs e)
  1026. {
  1027. InvokeEvent <ListViewCommandEventArgs> (ItemCommandEvent, e);
  1028. }
  1029. protected virtual void OnItemCreated (ListViewItemEventArgs e)
  1030. {
  1031. InvokeEvent <ListViewItemEventArgs> (ItemCreatedEvent, e);
  1032. }
  1033. protected virtual void OnItemDataBound (ListViewItemEventArgs e)
  1034. {
  1035. InvokeEvent <ListViewItemEventArgs> (ItemDataBoundEvent, e);
  1036. }
  1037. protected virtual void OnItemDeleted (ListViewDeletedEventArgs e)
  1038. {
  1039. InvokeEvent <ListViewDeletedEventArgs> (ItemDeletedEvent, e);
  1040. }
  1041. protected virtual void OnItemDeleting (ListViewDeleteEventArgs e)
  1042. {
  1043. InvokeEvent <ListViewDeleteEventArgs> (ItemDeletingEvent, e);
  1044. }
  1045. protected virtual void OnItemEditing (ListViewEditEventArgs e)
  1046. {
  1047. InvokeEvent <ListViewEditEventArgs> (ItemEditingEvent, e);
  1048. }
  1049. protected virtual void OnItemInserted (ListViewInsertedEventArgs e)
  1050. {
  1051. InvokeEvent <ListViewInsertedEventArgs> (ItemInsertedEvent, e);
  1052. }
  1053. protected virtual void OnItemInserting (ListViewInsertEventArgs e)
  1054. {
  1055. InvokeEvent <ListViewInsertEventArgs> (ItemInsertingEvent, e);
  1056. }
  1057. protected virtual void OnItemUpdated (ListViewUpdatedEventArgs e)
  1058. {
  1059. InvokeEvent <ListViewUpdatedEventArgs> (ItemUpdatedEvent, e);
  1060. }
  1061. protected virtual void OnItemUpdating (ListViewUpdateEventArgs e)
  1062. {
  1063. InvokeEvent <ListViewUpdateEventArgs> (ItemUpdatingEvent, e);
  1064. }
  1065. protected virtual void OnLayoutCreated (EventArgs e)
  1066. {
  1067. InvokeEvent (LayoutCreatedEvent, e);
  1068. }
  1069. protected virtual void OnPagePropertiesChanged (EventArgs e)
  1070. {
  1071. InvokeEvent (PagePropertiesChangedEvent, e);
  1072. }
  1073. protected virtual void OnPagePropertiesChanging (PagePropertiesChangingEventArgs e)
  1074. {
  1075. InvokeEvent (PagePropertiesChangingEvent, e);
  1076. }
  1077. protected virtual void OnSelectedIndexChanged (EventArgs e)
  1078. {
  1079. InvokeEvent (SelectedIndexChangedEvent, e);
  1080. }
  1081. protected virtual void OnSelectedIndexChanging (ListViewSelectEventArgs e)
  1082. {
  1083. InvokeEvent (SelectedIndexChangingEvent, e);
  1084. }
  1085. protected virtual void OnSorted (EventArgs e)
  1086. {
  1087. InvokeEvent (SortedEvent, e);
  1088. }
  1089. protected virtual void OnSorting (ListViewSortEventArgs e)
  1090. {
  1091. InvokeEvent <ListViewSortEventArgs> (SortingEvent, e);
  1092. }
  1093. protected virtual void OnTotalRowCountAvailable (PageEventArgs e)
  1094. {
  1095. InvokeEvent <PageEventArgs> (TotalRowCountAvailableEvent, e);
  1096. }
  1097. protected override void PerformDataBinding (IEnumerable data)
  1098. {
  1099. base.PerformDataBinding (data);
  1100. TrackViewState ();
  1101. int childCount = CreateChildControls (data, true);
  1102. ChildControlsCreated = true;
  1103. ViewState ["_!ItemCount"] = childCount;
  1104. }
  1105. protected override void PerformSelect ()
  1106. {
  1107. EnsureLayoutTemplate ();
  1108. base.PerformSelect ();
  1109. }
  1110. protected virtual void RemoveItems ()
  1111. {
  1112. if (_nonGroupedItemsContainer != null)
  1113. RemoveItems (_nonGroupedItemsContainer, _nonGroupedItemsContainerFirstItemIndex, _nonGroupedItemsContainerItemCount);
  1114. }
  1115. void RemoveItems (Control container, int start, int count)
  1116. {
  1117. int i = count;
  1118. while (i-- > 0)
  1119. container.Controls.RemoveAt (start);
  1120. }
  1121. protected override void Render (HtmlTextWriter writer)
  1122. {
  1123. base.Render (writer);
  1124. // Why override?
  1125. }
  1126. object SaveDataKeysState ()
  1127. {
  1128. DataKeyArray dka = DataKeys;
  1129. int len = dka != null ? dka.Count : 0;
  1130. if (len == 0)
  1131. return null;
  1132. object[] state = new object [len];
  1133. DataKey dk;
  1134. for (int i = 0; i < len; i++) {
  1135. dk = dka [i];
  1136. if (dk == null) {
  1137. state [i] = null;
  1138. continue;
  1139. }
  1140. state [i] = ((IStateManager)dk).SaveViewState ();
  1141. }
  1142. return state;
  1143. }
  1144. protected override object SaveControlState ()
  1145. {
  1146. object[] ret = new object [CSTATE_COUNT];
  1147. string[] dataKeyNames = DataKeyNames;
  1148. object dataKeysState = SaveDataKeysState ();
  1149. ret [CSTATE_BASE_STATE] = base.SaveControlState ();
  1150. ret [CSTATE_DATAKEYNAMES] = dataKeyNames.Length > 0 ? dataKeyNames : null;
  1151. ret [CSTATE_DATAKEYSSTATE] = dataKeysState != null ? dataKeysState : null;
  1152. ret [CSTATE_GROUPITEMCOUNT] = _groupItemCount > 1 ? (object)_groupItemCount : null;
  1153. ret [CSTATE_TOTALROWCOUNT] = _totalRowCount >= 1 ? (object)_totalRowCount : null;
  1154. ret [CSTATE_EDITINDEX] = _editIndex != -1 ? (object)_editIndex : null;
  1155. ret [CSTATE_SELECTEDINDEX] = _selectedIndex != -1 ? (object)_selectedIndex : null;
  1156. ret [CSTATE_SORTDIRECTION] = _sortDirection != SortDirection.Ascending ? (object)_sortDirection : null;
  1157. ret [CSTATE_SORTEXPRESSION] = String.IsNullOrEmpty (_sortExpression) ? null : _sortExpression;
  1158. return ret;
  1159. }
  1160. protected override object SaveViewState ()
  1161. {
  1162. object[] states = new object [2];
  1163. states [0] = base.SaveViewState ();
  1164. states [1] = null; // What goes here?
  1165. return states;
  1166. }
  1167. protected virtual void SetPageProperties (int startRowIndex, int maximumRows, bool databind)
  1168. {
  1169. if (maximumRows < 1)
  1170. throw new ArgumentOutOfRangeException ("maximumRows");
  1171. if (startRowIndex < 0)
  1172. throw new ArgumentOutOfRangeException ("startRowIndex");
  1173. if (maximumRows != _maximumRows || startRowIndex != _startRowIndex) {
  1174. if (databind) {
  1175. var args = new PagePropertiesChangingEventArgs (maximumRows, startRowIndex);
  1176. OnPagePropertiesChanging (args);
  1177. }
  1178. _startRowIndex = startRowIndex;
  1179. _maximumRows = maximumRows;
  1180. if (databind)
  1181. OnPagePropertiesChanged (EventArgs.Empty);
  1182. }
  1183. if (databind)
  1184. RequiresDataBinding = true;
  1185. }
  1186. public virtual void Sort (string sortExpression, SortDirection sortDirection)
  1187. {
  1188. }
  1189. void IPageableItemContainer.SetPageProperties (int startRowIndex, int maximumRows, bool databind)
  1190. {
  1191. SetPageProperties (startRowIndex, maximumRows, databind);
  1192. }
  1193. public virtual void UpdateItem (int itemIndex, bool causesValidation)
  1194. {
  1195. }
  1196. NotSupportedException StylingNotSupported ()
  1197. {
  1198. return new NotSupportedException ("Style properties are not supported on ListView. Apply styling or CSS classes to the elements in the ListView's templates.");
  1199. }
  1200. }
  1201. }
  1202. #endif