PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/danipen/mono
C# | 2038 lines | 1747 code | 257 blank | 34 comment | 467 complexity | 89c30ce0d52de655798c10528cab76db MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. //
  2. // System.Web.UI.WebControls.DetailsView.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual (lluis@novell.com)
  6. //
  7. // (C) 2005-2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Specialized;
  31. using System.ComponentModel;
  32. using System.Web.UI;
  33. using System.Security.Permissions;
  34. using System.Text;
  35. using System.IO;
  36. using System.Reflection;
  37. namespace System.Web.UI.WebControls
  38. {
  39. [SupportsEventValidation]
  40. [ToolboxDataAttribute ("<{0}:DetailsView runat=\"server\" Width=\"125px\" Height=\"50px\"></{0}:DetailsView>")]
  41. [DesignerAttribute ("System.Web.UI.Design.WebControls.DetailsViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  42. [ControlValuePropertyAttribute ("SelectedValue")]
  43. [DefaultEventAttribute ("PageIndexChanging")]
  44. #if NET_4_0
  45. [DataKeyProperty ("DataKey")]
  46. #endif
  47. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  48. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  49. public class DetailsView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IDataItemContainer, INamingContainer, IPostBackEventHandler, IPostBackContainer
  50. #if NET_4_0
  51. , IDataBoundItemControl, IDataBoundControl, IFieldControl
  52. #endif
  53. {
  54. object dataItem;
  55. Table table;
  56. DetailsViewRowCollection rows;
  57. DetailsViewRow headerRow;
  58. DetailsViewRow footerRow;
  59. DetailsViewRow bottomPagerRow;
  60. DetailsViewRow topPagerRow;
  61. IOrderedDictionary currentEditRowKeys;
  62. IOrderedDictionary currentEditNewValues;
  63. IOrderedDictionary currentEditOldValues;
  64. ITemplate pagerTemplate;
  65. ITemplate emptyDataTemplate;
  66. ITemplate headerTemplate;
  67. ITemplate footerTemplate;
  68. PropertyDescriptor[] cachedKeyProperties;
  69. readonly string[] emptyKeys = new string[0];
  70. readonly string unhandledEventExceptionMessage = "The DetailsView '{0}' fired event {1} which wasn't handled.";
  71. // View state
  72. DataControlFieldCollection columns;
  73. PagerSettings pagerSettings;
  74. TableItemStyle alternatingRowStyle;
  75. TableItemStyle editRowStyle;
  76. TableItemStyle insertRowStyle;
  77. TableItemStyle emptyDataRowStyle;
  78. TableItemStyle footerStyle;
  79. TableItemStyle headerStyle;
  80. TableItemStyle pagerStyle;
  81. TableItemStyle rowStyle;
  82. TableItemStyle commandRowStyle;
  83. TableItemStyle fieldHeaderStyle;
  84. IOrderedDictionary _keyTable;
  85. DataKey key;
  86. DataKey oldEditValues;
  87. AutoGeneratedFieldProperties[] autoFieldProperties;
  88. static readonly object PageIndexChangedEvent = new object();
  89. static readonly object PageIndexChangingEvent = new object();
  90. static readonly object ItemCommandEvent = new object();
  91. static readonly object ItemCreatedEvent = new object();
  92. static readonly object ItemDeletedEvent = new object();
  93. static readonly object ItemDeletingEvent = new object();
  94. static readonly object ItemInsertedEvent = new object();
  95. static readonly object ItemInsertingEvent = new object();
  96. static readonly object ModeChangingEvent = new object();
  97. static readonly object ModeChangedEvent = new object();
  98. static readonly object ItemUpdatedEvent = new object();
  99. static readonly object ItemUpdatingEvent = new object();
  100. // Control state
  101. int pageIndex;
  102. DetailsViewMode currentMode = DetailsViewMode.ReadOnly;
  103. bool hasCurrentMode;
  104. int pageCount;
  105. public DetailsView ()
  106. {
  107. rows = new DetailsViewRowCollection (new ArrayList ());
  108. }
  109. public event EventHandler PageIndexChanged {
  110. add { Events.AddHandler (PageIndexChangedEvent, value); }
  111. remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
  112. }
  113. public event DetailsViewPageEventHandler PageIndexChanging {
  114. add { Events.AddHandler (PageIndexChangingEvent, value); }
  115. remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
  116. }
  117. public event DetailsViewCommandEventHandler ItemCommand {
  118. add { Events.AddHandler (ItemCommandEvent, value); }
  119. remove { Events.RemoveHandler (ItemCommandEvent, value); }
  120. }
  121. public event EventHandler ItemCreated {
  122. add { Events.AddHandler (ItemCreatedEvent, value); }
  123. remove { Events.RemoveHandler (ItemCreatedEvent, value); }
  124. }
  125. public event DetailsViewDeletedEventHandler ItemDeleted {
  126. add { Events.AddHandler (ItemDeletedEvent, value); }
  127. remove { Events.RemoveHandler (ItemDeletedEvent, value); }
  128. }
  129. public event DetailsViewDeleteEventHandler ItemDeleting {
  130. add { Events.AddHandler (ItemDeletingEvent, value); }
  131. remove { Events.RemoveHandler (ItemDeletingEvent, value); }
  132. }
  133. public event DetailsViewInsertedEventHandler ItemInserted {
  134. add { Events.AddHandler (ItemInsertedEvent, value); }
  135. remove { Events.RemoveHandler (ItemInsertedEvent, value); }
  136. }
  137. public event DetailsViewInsertEventHandler ItemInserting {
  138. add { Events.AddHandler (ItemInsertingEvent, value); }
  139. remove { Events.RemoveHandler (ItemInsertingEvent, value); }
  140. }
  141. public event DetailsViewModeEventHandler ModeChanging {
  142. add { Events.AddHandler (ModeChangingEvent, value); }
  143. remove { Events.RemoveHandler (ModeChangingEvent, value); }
  144. }
  145. public event EventHandler ModeChanged {
  146. add { Events.AddHandler (ModeChangedEvent, value); }
  147. remove { Events.RemoveHandler (ModeChangedEvent, value); }
  148. }
  149. public event DetailsViewUpdatedEventHandler ItemUpdated {
  150. add { Events.AddHandler (ItemUpdatedEvent, value); }
  151. remove { Events.RemoveHandler (ItemUpdatedEvent, value); }
  152. }
  153. public event DetailsViewUpdateEventHandler ItemUpdating {
  154. add { Events.AddHandler (ItemUpdatingEvent, value); }
  155. remove { Events.RemoveHandler (ItemUpdatingEvent, value); }
  156. }
  157. protected virtual void OnPageIndexChanged (EventArgs e)
  158. {
  159. if (Events != null) {
  160. EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
  161. if (eh != null) eh (this, e);
  162. }
  163. }
  164. protected virtual void OnPageIndexChanging (DetailsViewPageEventArgs e)
  165. {
  166. if (Events != null) {
  167. DetailsViewPageEventHandler eh = (DetailsViewPageEventHandler) Events [PageIndexChangingEvent];
  168. if (eh != null) {
  169. eh (this, e);
  170. return;
  171. }
  172. }
  173. if (!IsBoundUsingDataSourceID)
  174. throw new HttpException (String.Format (unhandledEventExceptionMessage, ID, "PageIndexChanging"));
  175. }
  176. protected virtual void OnItemCommand (DetailsViewCommandEventArgs e)
  177. {
  178. if (Events != null) {
  179. DetailsViewCommandEventHandler eh = (DetailsViewCommandEventHandler) Events [ItemCommandEvent];
  180. if (eh != null) eh (this, e);
  181. }
  182. }
  183. protected virtual void OnItemCreated (EventArgs e)
  184. {
  185. if (Events != null) {
  186. EventHandler eh = (EventHandler) Events [ItemCreatedEvent];
  187. if (eh != null) eh (this, e);
  188. }
  189. }
  190. protected virtual void OnItemDeleted (DetailsViewDeletedEventArgs e)
  191. {
  192. if (Events != null) {
  193. DetailsViewDeletedEventHandler eh = (DetailsViewDeletedEventHandler) Events [ItemDeletedEvent];
  194. if (eh != null) eh (this, e);
  195. }
  196. }
  197. protected virtual void OnItemInserted (DetailsViewInsertedEventArgs e)
  198. {
  199. if (Events != null) {
  200. DetailsViewInsertedEventHandler eh = (DetailsViewInsertedEventHandler) Events [ItemInsertedEvent];
  201. if (eh != null) eh (this, e);
  202. }
  203. }
  204. protected virtual void OnItemInserting (DetailsViewInsertEventArgs e)
  205. {
  206. if (Events != null) {
  207. DetailsViewInsertEventHandler eh = (DetailsViewInsertEventHandler) Events [ItemInsertingEvent];
  208. if (eh != null) {
  209. eh (this, e);
  210. return;
  211. }
  212. }
  213. if (!IsBoundUsingDataSourceID)
  214. throw new HttpException (String.Format (unhandledEventExceptionMessage, ID, "ItemInserting"));
  215. }
  216. protected virtual void OnItemDeleting (DetailsViewDeleteEventArgs e)
  217. {
  218. if (Events != null) {
  219. DetailsViewDeleteEventHandler eh = (DetailsViewDeleteEventHandler) Events [ItemDeletingEvent];
  220. if (eh != null) {
  221. eh (this, e);
  222. return;
  223. }
  224. }
  225. if (!IsBoundUsingDataSourceID)
  226. throw new HttpException (String.Format (unhandledEventExceptionMessage, ID, "ItemDeleting"));
  227. }
  228. protected virtual void OnModeChanged (EventArgs e)
  229. {
  230. if (Events != null) {
  231. EventHandler eh = (EventHandler) Events [ModeChangedEvent];
  232. if (eh != null) eh (this, e);
  233. }
  234. }
  235. protected virtual void OnModeChanging (DetailsViewModeEventArgs e)
  236. {
  237. if (Events != null) {
  238. DetailsViewModeEventHandler eh = (DetailsViewModeEventHandler) Events [ModeChangingEvent];
  239. if (eh != null) {
  240. eh (this, e);
  241. return;
  242. }
  243. }
  244. if (!IsBoundUsingDataSourceID)
  245. throw new HttpException (String.Format (unhandledEventExceptionMessage, ID, "ModeChanging"));
  246. }
  247. protected virtual void OnItemUpdated (DetailsViewUpdatedEventArgs e)
  248. {
  249. if (Events != null) {
  250. DetailsViewUpdatedEventHandler eh = (DetailsViewUpdatedEventHandler) Events [ItemUpdatedEvent];
  251. if (eh != null) eh (this, e);
  252. }
  253. }
  254. protected virtual void OnItemUpdating (DetailsViewUpdateEventArgs e)
  255. {
  256. if (Events != null) {
  257. DetailsViewUpdateEventHandler eh = (DetailsViewUpdateEventHandler) Events [ItemUpdatingEvent];
  258. if (eh != null) {
  259. eh (this, e);
  260. return;
  261. }
  262. }
  263. if (!IsBoundUsingDataSourceID)
  264. throw new HttpException (String.Format (unhandledEventExceptionMessage, ID, "ItemUpdating"));
  265. }
  266. #if NET_4_0
  267. DataBoundControlMode IDataBoundItemControl.Mode {
  268. get {
  269. switch (CurrentMode) {
  270. case DetailsViewMode.ReadOnly:
  271. return DataBoundControlMode.ReadOnly;
  272. case DetailsViewMode.Edit:
  273. return DataBoundControlMode.Edit;
  274. case DetailsViewMode.Insert:
  275. return DataBoundControlMode.Insert;
  276. default:
  277. throw new InvalidOperationException (String.Format ("Unsupported CurrentMode value '{0}'", CurrentMode));
  278. }
  279. }
  280. }
  281. IDataSource IDataBoundControl.DataSourceObject {
  282. get { return base.DataSourceObject; }
  283. }
  284. IAutoFieldGenerator IFieldControl.FieldsGenerator {
  285. get {
  286. throw new NotImplementedException ();
  287. }
  288. set {
  289. throw new NotImplementedException ();
  290. }
  291. }
  292. #endif
  293. [WebCategoryAttribute ("Paging")]
  294. [DefaultValueAttribute (false)]
  295. public virtual bool AllowPaging {
  296. get {
  297. object ob = ViewState ["AllowPaging"];
  298. if (ob != null)
  299. return (bool) ob;
  300. return false;
  301. }
  302. set {
  303. ViewState ["AllowPaging"] = value;
  304. RequireBinding ();
  305. }
  306. }
  307. [DefaultValueAttribute (null)]
  308. [WebCategoryAttribute ("Styles")]
  309. [PersistenceMode (PersistenceMode.InnerProperty)]
  310. [NotifyParentProperty (true)]
  311. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  312. public TableItemStyle AlternatingRowStyle {
  313. get {
  314. if (alternatingRowStyle == null) {
  315. alternatingRowStyle = new TableItemStyle ();
  316. if (IsTrackingViewState)
  317. alternatingRowStyle.TrackViewState();
  318. }
  319. return alternatingRowStyle;
  320. }
  321. }
  322. [WebCategoryAttribute ("Behavior")]
  323. [DefaultValueAttribute (false)]
  324. public virtual bool AutoGenerateEditButton {
  325. get {
  326. object ob = ViewState ["AutoGenerateEditButton"];
  327. if (ob != null)
  328. return (bool) ob;
  329. return false;
  330. }
  331. set {
  332. ViewState ["AutoGenerateEditButton"] = value;
  333. RequireBinding ();
  334. }
  335. }
  336. [WebCategoryAttribute ("Behavior")]
  337. [DefaultValueAttribute (false)]
  338. public virtual bool AutoGenerateDeleteButton {
  339. get {
  340. object ob = ViewState ["AutoGenerateDeleteButton"];
  341. if (ob != null)
  342. return (bool) ob;
  343. return false;
  344. }
  345. set {
  346. ViewState ["AutoGenerateDeleteButton"] = value;
  347. RequireBinding ();
  348. }
  349. }
  350. [WebCategoryAttribute ("Behavior")]
  351. [DefaultValueAttribute (false)]
  352. public virtual bool AutoGenerateInsertButton {
  353. get {
  354. object ob = ViewState ["AutoGenerateInsertButton"];
  355. if (ob != null)
  356. return (bool) ob;
  357. return false;
  358. }
  359. set {
  360. ViewState ["AutoGenerateInsertButton"] = value;
  361. RequireBinding ();
  362. }
  363. }
  364. [WebCategoryAttribute ("Behavior")]
  365. [DefaultValueAttribute (true)]
  366. public virtual bool AutoGenerateRows {
  367. get {
  368. object ob = ViewState ["AutoGenerateRows"];
  369. if (ob != null)
  370. return (bool) ob;
  371. return true;
  372. }
  373. set {
  374. ViewState ["AutoGenerateRows"] = value;
  375. RequireBinding ();
  376. }
  377. }
  378. [UrlPropertyAttribute]
  379. [WebCategoryAttribute ("Appearance")]
  380. [DefaultValueAttribute ("")]
  381. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  382. public virtual string BackImageUrl {
  383. get {
  384. if (ControlStyleCreated)
  385. return ((TableStyle) ControlStyle).BackImageUrl;
  386. return String.Empty;
  387. }
  388. set {
  389. ((TableStyle) ControlStyle).BackImageUrl = value;
  390. }
  391. }
  392. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  393. [BrowsableAttribute (false)]
  394. public virtual DetailsViewRow BottomPagerRow {
  395. get {
  396. EnsureChildControls ();
  397. return bottomPagerRow;
  398. }
  399. }
  400. [WebCategoryAttribute ("Accessibility")]
  401. [DefaultValueAttribute ("")]
  402. [LocalizableAttribute (true)]
  403. public virtual string Caption {
  404. get {
  405. object ob = ViewState ["Caption"];
  406. if (ob != null)
  407. return (string) ob;
  408. return String.Empty;
  409. }
  410. set {
  411. ViewState ["Caption"] = value;
  412. RequireBinding ();
  413. }
  414. }
  415. [WebCategoryAttribute ("Accessibility")]
  416. [DefaultValueAttribute (TableCaptionAlign.NotSet)]
  417. public virtual TableCaptionAlign CaptionAlign {
  418. get {
  419. object o = ViewState ["CaptionAlign"];
  420. if(o != null)
  421. return (TableCaptionAlign) o;
  422. return TableCaptionAlign.NotSet;
  423. }
  424. set {
  425. ViewState ["CaptionAlign"] = value;
  426. RequireBinding ();
  427. }
  428. }
  429. [WebCategoryAttribute ("Layout")]
  430. [DefaultValueAttribute (-1)]
  431. public virtual int CellPadding {
  432. get {
  433. if (ControlStyleCreated)
  434. return ((TableStyle) ControlStyle).CellPadding;
  435. return -1;
  436. }
  437. set { ((TableStyle) ControlStyle).CellPadding = value; }
  438. }
  439. [WebCategoryAttribute ("Layout")]
  440. [DefaultValueAttribute (0)]
  441. public virtual int CellSpacing {
  442. get {
  443. if (ControlStyleCreated)
  444. return ((TableStyle) ControlStyle).CellSpacing;
  445. return 0;
  446. }
  447. set { ((TableStyle) ControlStyle).CellSpacing = value; }
  448. }
  449. [DefaultValueAttribute (null)]
  450. [WebCategoryAttribute ("Styles")]
  451. [PersistenceMode (PersistenceMode.InnerProperty)]
  452. [NotifyParentProperty (true)]
  453. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  454. public TableItemStyle CommandRowStyle {
  455. get {
  456. if (commandRowStyle == null) {
  457. commandRowStyle = new TableItemStyle ();
  458. if (IsTrackingViewState)
  459. commandRowStyle.TrackViewState();
  460. }
  461. return commandRowStyle;
  462. }
  463. }
  464. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  465. [BrowsableAttribute (false)]
  466. public DetailsViewMode CurrentMode {
  467. get { return hasCurrentMode ? currentMode : DefaultMode; }
  468. private set {
  469. hasCurrentMode = true;
  470. currentMode = value;
  471. }
  472. }
  473. DetailsViewMode defaultMode = DetailsViewMode.ReadOnly;
  474. [DefaultValueAttribute (DetailsViewMode.ReadOnly)]
  475. [WebCategoryAttribute ("Behavior")]
  476. public virtual DetailsViewMode DefaultMode {
  477. get { return defaultMode; }
  478. set {
  479. defaultMode = value;
  480. RequireBinding ();
  481. }
  482. }
  483. [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  484. [MergablePropertyAttribute (false)]
  485. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  486. [DefaultValueAttribute (null)]
  487. [WebCategoryAttribute ("Misc")]
  488. public virtual DataControlFieldCollection Fields {
  489. get {
  490. if (columns == null) {
  491. columns = new DataControlFieldCollection ();
  492. columns.FieldsChanged += new EventHandler (OnFieldsChanged);
  493. if (IsTrackingViewState)
  494. ((IStateManager)columns).TrackViewState ();
  495. }
  496. return columns;
  497. }
  498. }
  499. string[] dataKeyNames = null;
  500. [DefaultValueAttribute (null)]
  501. [WebCategoryAttribute ("Data")]
  502. [TypeConverter (typeof(StringArrayConverter))]
  503. [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  504. public virtual string[] DataKeyNames {
  505. get {
  506. if (dataKeyNames == null)
  507. return emptyKeys;
  508. else
  509. return dataKeyNames;
  510. }
  511. set {
  512. dataKeyNames = value;
  513. RequireBinding ();
  514. }
  515. }
  516. IOrderedDictionary KeyTable {
  517. get {
  518. if (_keyTable == null)
  519. _keyTable = new OrderedDictionary (DataKeyNames.Length);
  520. return _keyTable;
  521. }
  522. }
  523. [BrowsableAttribute (false)]
  524. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  525. public virtual DataKey DataKey {
  526. get {
  527. if (key == null)
  528. key = new DataKey (KeyTable);
  529. return key;
  530. }
  531. }
  532. DataKey OldEditValues {
  533. get {
  534. if (oldEditValues == null)
  535. oldEditValues = new DataKey (new OrderedDictionary ());
  536. return oldEditValues;
  537. }
  538. }
  539. [WebCategoryAttribute ("Styles")]
  540. [PersistenceMode (PersistenceMode.InnerProperty)]
  541. [NotifyParentProperty (true)]
  542. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  543. [DefaultValueAttribute (null)]
  544. public TableItemStyle EditRowStyle {
  545. get {
  546. if (editRowStyle == null) {
  547. editRowStyle = new TableItemStyle ();
  548. if (IsTrackingViewState)
  549. editRowStyle.TrackViewState();
  550. }
  551. return editRowStyle;
  552. }
  553. }
  554. [WebCategoryAttribute ("Styles")]
  555. [PersistenceMode (PersistenceMode.InnerProperty)]
  556. [NotifyParentProperty (true)]
  557. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  558. [DefaultValueAttribute (null)]
  559. public TableItemStyle EmptyDataRowStyle {
  560. get {
  561. if (emptyDataRowStyle == null) {
  562. emptyDataRowStyle = new TableItemStyle ();
  563. if (IsTrackingViewState)
  564. emptyDataRowStyle.TrackViewState();
  565. }
  566. return emptyDataRowStyle;
  567. }
  568. }
  569. [DefaultValue (null)]
  570. [TemplateContainer (typeof(DetailsView), BindingDirection.OneWay)]
  571. [PersistenceMode (PersistenceMode.InnerProperty)]
  572. [Browsable (false)]
  573. public virtual ITemplate EmptyDataTemplate {
  574. get { return emptyDataTemplate; }
  575. set { emptyDataTemplate = value; }
  576. }
  577. [LocalizableAttribute (true)]
  578. [WebCategoryAttribute ("Appearance")]
  579. [DefaultValueAttribute ("")]
  580. public virtual string EmptyDataText {
  581. get {
  582. object ob = ViewState ["EmptyDataText"];
  583. if (ob != null)
  584. return (string) ob;
  585. return String.Empty;
  586. }
  587. set {
  588. ViewState ["EmptyDataText"] = value;
  589. RequireBinding ();
  590. }
  591. }
  592. [WebCategoryAttribute ("Behavior")]
  593. [DefaultValueAttribute (false)]
  594. public virtual bool EnablePagingCallbacks {
  595. get {
  596. object ob = ViewState ["EnablePagingCallbacks"];
  597. if (ob != null)
  598. return (bool) ob;
  599. return false;
  600. }
  601. set {
  602. ViewState ["EnablePagingCallbacks"] = value;
  603. RequireBinding ();
  604. }
  605. }
  606. [WebCategoryAttribute ("Styles")]
  607. [PersistenceMode (PersistenceMode.InnerProperty)]
  608. [NotifyParentProperty (true)]
  609. [DefaultValue (null)]
  610. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  611. public TableItemStyle FieldHeaderStyle {
  612. get {
  613. if (fieldHeaderStyle == null) {
  614. fieldHeaderStyle = new TableItemStyle ();
  615. if (IsTrackingViewState)
  616. fieldHeaderStyle.TrackViewState();
  617. }
  618. return fieldHeaderStyle;
  619. }
  620. }
  621. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  622. [BrowsableAttribute (false)]
  623. public virtual DetailsViewRow FooterRow {
  624. get {
  625. EnsureChildControls ();
  626. return footerRow;
  627. }
  628. }
  629. [DefaultValue (null)]
  630. [TemplateContainer (typeof(DetailsView), BindingDirection.OneWay)]
  631. [PersistenceMode (PersistenceMode.InnerProperty)]
  632. [Browsable (false)]
  633. public virtual ITemplate FooterTemplate {
  634. get { return footerTemplate; }
  635. set { footerTemplate = value; }
  636. }
  637. [LocalizableAttribute (true)]
  638. [WebCategoryAttribute ("Appearance")]
  639. [DefaultValueAttribute ("")]
  640. public virtual string FooterText {
  641. get {
  642. object ob = ViewState ["FooterText"];
  643. if (ob != null)
  644. return (string) ob;
  645. return String.Empty;
  646. }
  647. set {
  648. ViewState ["FooterText"] = value;
  649. RequireBinding ();
  650. }
  651. }
  652. [WebCategoryAttribute ("Styles")]
  653. [PersistenceMode (PersistenceMode.InnerProperty)]
  654. [NotifyParentProperty (true)]
  655. [DefaultValue (null)]
  656. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  657. public TableItemStyle FooterStyle {
  658. get {
  659. if (footerStyle == null) {
  660. footerStyle = new TableItemStyle ();
  661. if (IsTrackingViewState)
  662. footerStyle.TrackViewState();
  663. }
  664. return footerStyle;
  665. }
  666. }
  667. [WebCategoryAttribute ("Appearance")]
  668. [DefaultValueAttribute (GridLines.Both)]
  669. public virtual GridLines GridLines {
  670. get {
  671. if (ControlStyleCreated)
  672. return ((TableStyle) ControlStyle).GridLines;
  673. return GridLines.Both;
  674. }
  675. set { ((TableStyle) ControlStyle).GridLines = value; }
  676. }
  677. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  678. [BrowsableAttribute (false)]
  679. public virtual DetailsViewRow HeaderRow {
  680. get {
  681. EnsureChildControls ();
  682. return headerRow;
  683. }
  684. }
  685. [WebCategoryAttribute ("Styles")]
  686. [PersistenceMode (PersistenceMode.InnerProperty)]
  687. [NotifyParentProperty (true)]
  688. [DefaultValue (null)]
  689. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  690. public TableItemStyle HeaderStyle {
  691. get {
  692. if (headerStyle == null) {
  693. headerStyle = new TableItemStyle ();
  694. if (IsTrackingViewState)
  695. headerStyle.TrackViewState();
  696. }
  697. return headerStyle;
  698. }
  699. }
  700. [DefaultValue (null)]
  701. [TemplateContainer (typeof(DetailsView), BindingDirection.OneWay)]
  702. [PersistenceMode (PersistenceMode.InnerProperty)]
  703. [Browsable (false)]
  704. public virtual ITemplate HeaderTemplate {
  705. get { return headerTemplate; }
  706. set { headerTemplate = value; }
  707. }
  708. [LocalizableAttribute (true)]
  709. [WebCategoryAttribute ("Appearance")]
  710. [DefaultValueAttribute ("")]
  711. public virtual string HeaderText {
  712. get {
  713. object ob = ViewState ["HeaderText"];
  714. if (ob != null)
  715. return (string) ob;
  716. return String.Empty;
  717. }
  718. set {
  719. ViewState ["HeaderText"] = value;
  720. RequireBinding ();
  721. }
  722. }
  723. [Category ("Layout")]
  724. [DefaultValueAttribute (HorizontalAlign.NotSet)]
  725. public virtual HorizontalAlign HorizontalAlign {
  726. get {
  727. if (ControlStyleCreated)
  728. return ((TableStyle) ControlStyle).HorizontalAlign;
  729. return HorizontalAlign.NotSet;
  730. }
  731. set { ((TableStyle) ControlStyle).HorizontalAlign = value; }
  732. }
  733. [WebCategoryAttribute ("Styles")]
  734. [PersistenceMode (PersistenceMode.InnerProperty)]
  735. [NotifyParentProperty (true)]
  736. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  737. [DefaultValueAttribute (null)]
  738. public TableItemStyle InsertRowStyle {
  739. get {
  740. if (insertRowStyle == null) {
  741. insertRowStyle = new TableItemStyle ();
  742. if (IsTrackingViewState)
  743. insertRowStyle.TrackViewState();
  744. }
  745. return insertRowStyle;
  746. }
  747. }
  748. [BrowsableAttribute (false)]
  749. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  750. public virtual int PageCount {
  751. get { return pageCount; }
  752. private set { pageCount = value; }
  753. }
  754. [WebCategoryAttribute ("Paging")]
  755. [BindableAttribute (true, BindingDirection.OneWay)]
  756. [DefaultValueAttribute (0)]
  757. public virtual int PageIndex {
  758. get {
  759. if (CurrentMode == DetailsViewMode.Insert)
  760. return -1;
  761. return pageIndex;
  762. }
  763. set {
  764. if (value < -1)
  765. throw new ArgumentOutOfRangeException ("PageIndex must be non-negative");
  766. if (pageIndex == value || value == -1)
  767. return;
  768. pageIndex = value;
  769. RequireBinding ();
  770. }
  771. }
  772. [WebCategoryAttribute ("Paging")]
  773. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  774. [NotifyParentPropertyAttribute (true)]
  775. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  776. public virtual PagerSettings PagerSettings {
  777. get {
  778. if (pagerSettings == null) {
  779. pagerSettings = new PagerSettings (this);
  780. if (IsTrackingViewState)
  781. ((IStateManager)pagerSettings).TrackViewState ();
  782. }
  783. return pagerSettings;
  784. }
  785. }
  786. [WebCategoryAttribute ("Styles")]
  787. [PersistenceMode (PersistenceMode.InnerProperty)]
  788. [NotifyParentProperty (true)]
  789. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  790. public TableItemStyle PagerStyle {
  791. get {
  792. if (pagerStyle == null) {
  793. pagerStyle = new TableItemStyle ();
  794. if (IsTrackingViewState)
  795. pagerStyle.TrackViewState();
  796. }
  797. return pagerStyle;
  798. }
  799. }
  800. [DefaultValue (null)]
  801. [TemplateContainer (typeof (DetailsView), BindingDirection.OneWay)]
  802. [PersistenceMode (PersistenceMode.InnerProperty)]
  803. [Browsable (false)]
  804. public virtual ITemplate PagerTemplate {
  805. get { return pagerTemplate; }
  806. set { pagerTemplate = value; }
  807. }
  808. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  809. [BrowsableAttribute (false)]
  810. public virtual DetailsViewRowCollection Rows {
  811. get {
  812. EnsureChildControls ();
  813. return rows;
  814. }
  815. }
  816. [BrowsableAttribute(false)]
  817. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  818. public IAutoFieldGenerator RowsGenerator {
  819. get;
  820. set;
  821. }
  822. [WebCategoryAttribute ("Styles")]
  823. [PersistenceMode (PersistenceMode.InnerProperty)]
  824. [NotifyParentProperty (true)]
  825. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  826. [DefaultValue (null)]
  827. public TableItemStyle RowStyle {
  828. get {
  829. if (rowStyle == null) {
  830. rowStyle = new TableItemStyle ();
  831. if (IsTrackingViewState)
  832. rowStyle.TrackViewState();
  833. }
  834. return rowStyle;
  835. }
  836. }
  837. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  838. [BrowsableAttribute (false)]
  839. public object SelectedValue {
  840. get { return DataKey.Value; }
  841. }
  842. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  843. [BrowsableAttribute (false)]
  844. public virtual DetailsViewRow TopPagerRow {
  845. get {
  846. EnsureChildControls ();
  847. return topPagerRow;
  848. }
  849. }
  850. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  851. [BrowsableAttribute (false)]
  852. public virtual object DataItem {
  853. get {
  854. return dataItem;
  855. }
  856. }
  857. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  858. [BrowsableAttribute (false)]
  859. public int DataItemCount {
  860. get { return PageCount; }
  861. }
  862. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  863. [BrowsableAttribute (false)]
  864. public virtual int DataItemIndex {
  865. get { return PageIndex; }
  866. }
  867. int IDataItemContainer.DisplayIndex {
  868. get { return PageIndex; }
  869. }
  870. int IDataItemContainer.DataItemIndex {
  871. get { return DataItemIndex; }
  872. }
  873. [MonoTODO ("Make use of it in the code")]
  874. [DefaultValue (true)]
  875. public virtual bool EnableModelValidation {
  876. get;
  877. set;
  878. }
  879. public virtual bool IsBindableType (Type type)
  880. {
  881. return type.IsPrimitive || type == typeof (string) || type == typeof (DateTime) || type == typeof (Guid) || type == typeof (Decimal);
  882. }
  883. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  884. {
  885. DataSourceSelectArguments arg = new DataSourceSelectArguments ();
  886. DataSourceView view = GetData ();
  887. if (AllowPaging && view.CanPage) {
  888. arg.StartRowIndex = PageIndex;
  889. if (view.CanRetrieveTotalRowCount) {
  890. arg.RetrieveTotalRowCount = true;
  891. arg.MaximumRows = 1;
  892. } else
  893. arg.MaximumRows = -1;
  894. }
  895. return arg;
  896. }
  897. protected virtual ICollection CreateFieldSet (object dataItem, bool useDataSource)
  898. {
  899. bool autoGenerate = AutoGenerateRows;
  900. if (autoGenerate) {
  901. IAutoFieldGenerator fieldGenerator = RowsGenerator;
  902. if (fieldGenerator != null)
  903. return fieldGenerator.GenerateFields (this);
  904. }
  905. ArrayList fields = new ArrayList ();
  906. if (AutoGenerateRows) {
  907. if (useDataSource) {
  908. if (dataItem != null)
  909. fields.AddRange (CreateAutoGeneratedRows (dataItem));
  910. } else {
  911. if (autoFieldProperties != null) {
  912. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  913. fields.Add (CreateAutoGeneratedRow (props));
  914. }
  915. }
  916. }
  917. fields.AddRange (Fields);
  918. if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateInsertButton) {
  919. CommandField field = new CommandField ();
  920. field.ShowEditButton = AutoGenerateEditButton;
  921. field.ShowDeleteButton = AutoGenerateDeleteButton;
  922. field.ShowInsertButton = AutoGenerateInsertButton;
  923. fields.Add (field);
  924. }
  925. return fields;
  926. }
  927. protected virtual ICollection CreateAutoGeneratedRows (object dataItem)
  928. {
  929. if (dataItem == null)
  930. return null;
  931. ArrayList list = new ArrayList ();
  932. autoFieldProperties = CreateAutoFieldProperties (dataItem);
  933. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  934. list.Add (CreateAutoGeneratedRow (props));
  935. return list;
  936. }
  937. protected virtual AutoGeneratedField CreateAutoGeneratedRow (AutoGeneratedFieldProperties fieldProperties)
  938. {
  939. return new AutoGeneratedField (fieldProperties);
  940. }
  941. AutoGeneratedFieldProperties[] CreateAutoFieldProperties (object dataItem)
  942. {
  943. if (IsBindableType (dataItem.GetType ())) {
  944. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  945. ((IStateManager) field).TrackViewState ();
  946. field.Name = "Item";
  947. field.DataField = BoundField.ThisExpression;
  948. field.Type = dataItem.GetType ();
  949. return new AutoGeneratedFieldProperties [] { field };
  950. }
  951. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem, false);
  952. if (props != null && props.Count > 0) {
  953. ArrayList retVal = new ArrayList ();
  954. foreach (PropertyDescriptor current in props) {
  955. if (IsBindableType (current.PropertyType)) {
  956. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  957. ((IStateManager) field).TrackViewState ();
  958. field.Name = current.Name;
  959. field.DataField = current.Name;
  960. for (int i = 0; i < DataKeyNames.Length; i++) {
  961. if (string.Compare (DataKeyNames [i], current.Name, StringComparison.InvariantCultureIgnoreCase) == 0) {
  962. field.IsReadOnly = true;
  963. break;
  964. }
  965. }
  966. field.Type = current.PropertyType;
  967. retVal.Add (field);
  968. }
  969. }
  970. if (retVal.Count > 0)
  971. return (AutoGeneratedFieldProperties []) retVal.ToArray (typeof (AutoGeneratedFieldProperties));
  972. }
  973. throw new HttpException (String.Format ("DetailsView with id '{0}' did not have any properties or attributes from which to generate fields. Ensure that your data source has content.", ID));
  974. }
  975. protected virtual DetailsViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
  976. {
  977. DetailsViewRow row;
  978. if (rowType == DataControlRowType.Pager)
  979. row = new DetailsViewPagerRow (rowIndex, rowType, rowState);
  980. else
  981. row = new DetailsViewRow (rowIndex, rowType, rowState);
  982. return row;
  983. }
  984. void RequireBinding ()
  985. {
  986. if (Initialized)
  987. RequiresDataBinding = true;
  988. }
  989. protected virtual Table CreateTable ()
  990. {
  991. return new ContainedTable (this);
  992. }
  993. protected override Style CreateControlStyle ()
  994. {
  995. TableStyle style = new TableStyle ();
  996. style.GridLines = GridLines.Both;
  997. style.CellSpacing = 0;
  998. return style;
  999. }
  1000. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  1001. {
  1002. PagedDataSource dataSource = new PagedDataSource ();
  1003. dataSource.DataSource = CurrentMode != DetailsViewMode.Insert ? data : null;
  1004. dataSource.AllowPaging = AllowPaging;
  1005. dataSource.PageSize = 1;
  1006. dataSource.CurrentPageIndex = PageIndex;
  1007. if (dataBinding && CurrentMode != DetailsViewMode.Insert) {
  1008. DataSourceView view = GetData ();
  1009. if (view != null && view.CanPage) {
  1010. dataSource.AllowServerPaging = true;
  1011. if (SelectArguments.RetrieveTotalRowCount)
  1012. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  1013. }
  1014. }
  1015. bool showPager = AllowPaging && (dataSource.PageCount > 1);
  1016. Controls.Clear ();
  1017. table = CreateTable ();
  1018. Controls.Add (table);
  1019. headerRow = null;
  1020. footerRow = null;
  1021. topPagerRow = null;
  1022. bottomPagerRow = null;
  1023. ArrayList list = new ArrayList ();
  1024. // Gets the current data item
  1025. if (AllowPaging) {
  1026. PageCount = dataSource.DataSourceCount;
  1027. if (PageIndex >= PageCount && PageCount > 0)
  1028. pageIndex = dataSource.CurrentPageIndex = PageCount - 1;
  1029. if (dataSource.DataSource != null) {
  1030. IEnumerator e = dataSource.GetEnumerator ();
  1031. if (e.MoveNext ())
  1032. dataItem = e.Current;
  1033. }
  1034. } else {
  1035. int page = 0;
  1036. object lastItem = null;
  1037. if (dataSource.DataSource != null) {
  1038. IEnumerator e = dataSource.GetEnumerator ();
  1039. for (; e.MoveNext (); page++) {
  1040. lastItem = e.Current;
  1041. if (page == PageIndex)
  1042. dataItem = e.Current;
  1043. }
  1044. }
  1045. PageCount = page;
  1046. if (PageIndex >= PageCount && PageCount > 0) {
  1047. pageIndex = PageCount - 1;
  1048. dataItem = lastItem;
  1049. }
  1050. }
  1051. if (PageCount == 0 && CurrentMode != DetailsViewMode.Insert) {
  1052. DetailsViewRow row = CreateEmptyRow ();
  1053. if (row != null) {
  1054. table.Rows.Add (row);
  1055. list.Add (row);
  1056. }
  1057. } else {
  1058. // Creates the set of fields to show
  1059. ICollection fieldCollection = CreateFieldSet (dataItem, dataBinding && dataItem != null);
  1060. DataControlField [] fields = new DataControlField [fieldCollection.Count];
  1061. fieldCollection.CopyTo (fields, 0);
  1062. foreach (DataControlField field in fields) {
  1063. field.Initialize (false, this);
  1064. if (EnablePagingCallbacks)
  1065. field.ValidateSupportsCallback ();
  1066. }
  1067. // Main table creation
  1068. headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
  1069. DataControlFieldCell headerCell = new DataControlFieldCell (null);
  1070. headerCell.ColumnSpan = 2;
  1071. if (headerTemplate != null)
  1072. headerTemplate.InstantiateIn (headerCell);
  1073. else if (!String.IsNullOrEmpty (HeaderText))
  1074. headerCell.Text = HeaderText;
  1075. else
  1076. headerRow.Visible = false;
  1077. headerRow.Cells.Add (headerCell);
  1078. table.Rows.Add (headerRow);
  1079. if (showPager && PagerSettings.Position == PagerPosition.Top ||
  1080. PagerSettings.Position == PagerPosition.TopAndBottom) {
  1081. topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
  1082. InitializePager (topPagerRow, dataSource);
  1083. table.Rows.Add (topPagerRow);
  1084. }
  1085. foreach (DataControlField field in fields) {
  1086. DataControlRowState rstate = GetRowState (list.Count);
  1087. DetailsViewRow row = CreateRow (PageIndex, DataControlRowType.DataRow, rstate);
  1088. InitializeRow (row, field);
  1089. table.Rows.Add (row);
  1090. list.Add (row);
  1091. }
  1092. footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
  1093. DataControlFieldCell footerCell = new DataControlFieldCell (null);
  1094. footerCell.ColumnSpan = 2;
  1095. if (footerTemplate != null)
  1096. footerTemplate.InstantiateIn (footerCell);
  1097. else if (!String.IsNullOrEmpty (FooterText))
  1098. footerCell.Text = FooterText;
  1099. else
  1100. footerRow.Visible = false;
  1101. footerRow.Cells.Add (footerCell);
  1102. table.Rows.Add (footerRow);
  1103. if (showPager && PagerSettings.Position == PagerPosition.Bottom ||
  1104. PagerSettings.Position == PagerPosition.TopAndBottom) {
  1105. bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
  1106. InitializePager (bottomPagerRow, dataSource);
  1107. table.Rows.Add (bottomPagerRow);
  1108. }
  1109. }
  1110. rows = new DetailsViewRowCollection (list);
  1111. if (dataBinding)
  1112. DataBind (false);
  1113. OnItemCreated (EventArgs.Empty);
  1114. return PageCount;
  1115. }
  1116. protected override void EnsureDataBound ()
  1117. {
  1118. if (CurrentMode == DetailsViewMode.Insert) {
  1119. if (RequiresDataBinding) {
  1120. OnDataBinding (EventArgs.Empty);
  1121. RequiresDataBinding = false;
  1122. InternalPerformDataBinding (null);
  1123. MarkAsDataBound ();
  1124. OnDataBound (EventArgs.Empty);
  1125. }
  1126. } else
  1127. base.EnsureDataBound ();
  1128. }
  1129. DataControlRowState GetRowState (int index)
  1130. {
  1131. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1132. if (CurrentMode == DetailsViewMode.Edit)
  1133. rstate |= DataControlRowState.Edit;
  1134. else if (CurrentMode == DetailsViewMode.Insert)
  1135. rstate |= DataControlRowState.Insert;
  1136. return rstate;
  1137. }
  1138. protected virtual void InitializePager (DetailsViewRow row, PagedDataSource dataSource)
  1139. {
  1140. TableCell cell = new TableCell ();
  1141. cell.ColumnSpan = 2;
  1142. if (pagerTemplate != null)
  1143. pagerTemplate.InstantiateIn (cell);
  1144. else
  1145. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  1146. row.Cells.Add (cell);
  1147. }
  1148. DetailsViewRow CreateEmptyRow ()
  1149. {
  1150. TableCell cell = new TableCell ();
  1151. if (emptyDataTemplate != null)
  1152. emptyDataTemplate.InstantiateIn (cell);
  1153. else if (!String.IsNullOrEmpty (EmptyDataText))
  1154. cell.Text = EmptyDataText;
  1155. else
  1156. return null;
  1157. DetailsViewRow row = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1158. row.Cells.Add (cell);
  1159. return row;
  1160. }
  1161. protected virtual void InitializeRow (DetailsViewRow row, DataControlField field)
  1162. {
  1163. if (!field.Visible) {
  1164. row.Visible = false;
  1165. return;
  1166. }
  1167. row.ContainingField = field;
  1168. DataControlFieldCell cell;
  1169. if (field.ShowHeader) {
  1170. cell = new DataControlFieldCell (field);
  1171. row.Cells.Add (cell);
  1172. field.InitializeCell (cell, DataControlCellType.Header, row.RowState, row.RowIndex);
  1173. }
  1174. cell = new DataControlFieldCell (field);
  1175. if (!field.ShowHeader)
  1176. cell.ColumnSpan = 2;
  1177. row.Cells.Add (cell);
  1178. field.InitializeCell (cell, DataControlCellType.DataCell, row.RowState, row.RowIndex);
  1179. if (CurrentMode == DetailsViewMode.Insert && !field.InsertVisible)
  1180. row.Visible = false;
  1181. }
  1182. void FillRowDataKey (object dataItem)
  1183. {
  1184. KeyTable.Clear ();
  1185. if (cachedKeyProperties == null) {
  1186. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
  1187. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  1188. for (int n=0; n<DataKeyNames.Length; n++) {
  1189. PropertyDescriptor p = props.Find (DataKeyNames [n], true);
  1190. if (p == null)
  1191. throw new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
  1192. cachedKeyProperties [n] = p;
  1193. }
  1194. }
  1195. foreach (PropertyDescriptor p in cachedKeyProperties)
  1196. KeyTable [p.Name] = p.GetValue (dataItem);
  1197. }
  1198. IOrderedDictionary GetRowValues (bool includeReadOnlyFields, bool includePrimaryKey)
  1199. {
  1200. OrderedDictionary dic = new OrderedDictionary ();
  1201. ExtractRowValues (dic, includeReadOnlyFields, includePrimaryKey);
  1202. return dic;
  1203. }
  1204. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeReadOnlyFields, bool includePrimaryKey)
  1205. {
  1206. foreach (DetailsViewRow row in Rows) {
  1207. if (row.Cells.Count < 1)
  1208. continue;
  1209. DataControlFieldCell c = row.Cells[row.Cells.Count-1] as DataControlFieldCell;
  1210. if (c != null)
  1211. c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
  1212. }
  1213. if (!includePrimaryKey && DataKeyNames != null)
  1214. foreach (string key in DataKeyNames)
  1215. fieldValues.Remove (key);
  1216. }
  1217. protected override HtmlTextWriterTag TagKey {
  1218. get {
  1219. if (EnablePagingCallbacks)
  1220. return HtmlTextWriterTag.Div;
  1221. else
  1222. return HtmlTextWriterTag.Table;
  1223. }
  1224. }
  1225. public sealed override void DataBind ()
  1226. {
  1227. cachedKeyProperties = null;
  1228. base.DataBind ();
  1229. if (dataItem != null) {
  1230. if (CurrentMode == DetailsViewMode.Edit)
  1231. oldEditValues = new DataKey (GetRowValues (false, true));
  1232. FillRowDataKey (dataItem);
  1233. key = new DataKey (KeyTable);
  1234. }
  1235. }
  1236. protected internal override void PerformDataBinding (IEnumerable data)
  1237. {
  1238. base.PerformDataBinding (data);
  1239. }
  1240. protected internal virtual void PrepareControlHierarchy ()
  1241. {
  1242. if (table == null)
  1243. return;
  1244. table.Caption = Caption;
  1245. table.CaptionAlign = CaptionAlign;
  1246. foreach (DetailsViewRow row in table.Rows) {
  1247. switch (row.RowType) {
  1248. case DataControlRowType.Header:
  1249. if (headerStyle != null && !headerStyle.IsEmpty)
  1250. row.ControlStyle.CopyFrom (headerStyle);
  1251. break;
  1252. case DataControlRowType.Footer:
  1253. if (footerStyle != null && !footerStyle.IsEmpty)
  1254. row.ControlStyle.CopyFrom (footerStyle);
  1255. break;
  1256. case DataControlRowType.Pager:
  1257. if (pagerStyle != null && !pagerStyle.IsEmpty)
  1258. row.ControlStyle.CopyFrom (pagerStyle);
  1259. break;
  1260. case DataControlRowType.EmptyDataRow:
  1261. if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
  1262. row.ControlStyle.CopyFrom (emptyDataRowStyle);
  1263. break;
  1264. case DataControlRowType.DataRow:
  1265. if (rowStyle != null && !rowStyle.IsEmpty)
  1266. row.ControlStyle.CopyFrom (rowStyle);
  1267. if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
  1268. row.ControlStyle.CopyFrom (alternatingRowStyle);
  1269. break;
  1270. default:
  1271. break;
  1272. }
  1273. if (row.ContainingField is CommandField) {
  1274. if (commandRowStyle != null && !commandRowStyle.IsEmpty)
  1275. row.ControlStyle.CopyFrom (commandRowStyle);
  1276. } else {
  1277. if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
  1278. row.ControlStyle.CopyFrom (editRowStyle);
  1279. if ((row.RowState & DataControlRowState.Insert) != 0) {
  1280. if (insertRowStyle != null && !insertRowStyle.IsEmpty)
  1281. row.ControlStyle.CopyFrom (insertRowStyle);
  1282. else if (editRowStyle != null && !editRowStyle.IsEmpty)
  1283. row.ControlStyle.CopyFrom (editRowStyle);
  1284. }
  1285. }
  1286. for (int n = 0; n < row.Cells.Count; n++) {
  1287. DataControlFieldCell fcell = row.Cells [n] as DataControlFieldCell;
  1288. if (fcell != null && fcell.ContainingField != null) {
  1289. DataControlField field = fcell.ContainingField;
  1290. if (n == 0 && field.ShowHeader) {
  1291. if (fieldHeaderStyle != null && !fieldHeaderStyle.IsEmpty)
  1292. fcell.ControlStyle.CopyFrom (fieldHeaderStyle);
  1293. if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
  1294. fcell.ControlStyle.CopyFrom (field.HeaderStyle);
  1295. } else {
  1296. if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty) {
  1297. foreach (Control c in fcell.Controls) {
  1298. WebControl wc = c as WebControl;
  1299. if (wc != null)
  1300. wc.ControlStyle.MergeWith (field.ControlStyle);
  1301. }
  1302. }
  1303. if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
  1304. fcell.ControlStyle.CopyFrom (field.ItemStyle);
  1305. }
  1306. }
  1307. }
  1308. }
  1309. }
  1310. protected internal override void OnInit (EventArgs e)
  1311. {
  1312. Page page = Page;
  1313. if (page != null)
  1314. page.RegisterRequiresControlState (this);
  1315. base.OnInit (e);
  1316. }
  1317. void OnFieldsChanged (object sender, EventArgs args)
  1318. {
  1319. RequireBinding ();
  1320. }
  1321. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  1322. {
  1323. base.OnDataSourceViewChanged (sender, e);
  1324. RequireBinding ();
  1325. }
  1326. protected override bool OnBubbleEvent (object source, EventArgs e)
  1327. {
  1328. DetailsViewCommandEventArgs args = e as DetailsViewCommandEventArgs;
  1329. if (args != null) {
  1330. bool causesValidation = false;
  1331. IButtonControl button = args.CommandSource as IButtonControl;
  1332. if (button != null && button.CausesValidation) {
  1333. Page.Validate (button.ValidationGroup);
  1334. causesValidation = true;
  1335. }
  1336. ProcessCommand (args, causesValidation);
  1337. return true;
  1338. }
  1339. return base.OnBubbleEvent (source, e);
  1340. }
  1341. void ProcessCommand (DetailsViewCommandEventArgs args, bool causesValidation) {
  1342. OnItemCommand (args);
  1343. ProcessEvent (args.CommandName, args.CommandArgument as string, causesValidation);
  1344. }
  1345. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  1346. {
  1347. RaisePostBackEvent (eventArgument);
  1348. }
  1349. // TODO: This is prolly obsolete
  1350. protected virtual void RaisePostBackEvent (string eventArgument)
  1351. {
  1352. ValidateEvent (UniqueID, eventArgument);
  1353. int i = eventArgument.IndexOf ('$');
  1354. CommandEventArgs arg;
  1355. if (i != -1)
  1356. arg = new CommandEventArgs (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
  1357. else
  1358. arg = new CommandEventArgs (eventArgument, null);
  1359. ProcessCommand (new DetailsViewCommandEventArgs (this, arg), false);
  1360. }
  1361. void ProcessEvent (string eventName, string param, bool causesValidation)
  1362. {
  1363. switch (eventName) {
  1364. case DataControlCommands.PageCommandName:
  1365. int newIndex = -1;
  1366. switch (param) {
  1367. case DataControlCommands.FirstPageCommandArgument:
  1368. newIndex = 0;
  1369. break;
  1370. case DataControlCommands.LastPageCommandArgument:
  1371. newIndex = PageCount - 1;
  1372. break;
  1373. case DataControlCommands.NextPageCommandArgument:
  1374. newIndex = PageIndex + 1;
  1375. break;
  1376. case DataControlCommands.PreviousPageCommandArgument:
  1377. newIndex = PageIndex - 1;
  1378. break;
  1379. default:
  1380. int paramIndex = 0;
  1381. int.TryParse (param, out paramIndex);
  1382. newIndex = paramIndex - 1;
  1383. break;
  1384. }
  1385. SetPageIndex (newIndex);
  1386. break;
  1387. case DataControlCommands.FirstPageCommandArgument:
  1388. SetPageIndex (0);
  1389. break;
  1390. case DataControlCommands.LastPageCommandArgument:
  1391. SetPageIndex (PageCount - 1);
  1392. break;
  1393. case DataControlCommands.NextPageCommandArgument:
  1394. if (PageIndex < PageCount - 1)
  1395. SetPageIndex (PageIndex + 1);
  1396. break;
  1397. case DataControlCommands.PreviousPageCommandArgument:
  1398. if (PageIndex > 0)
  1399. SetPageIndex (PageIndex - 1);
  1400. break;
  1401. case DataControlCommands.EditCommandName:
  1402. ProcessChangeMode (DetailsViewMode.Edit);
  1403. break;
  1404. case DataControlCommands.NewCommandName:
  1405. ProcessChangeMode (DetailsViewMode.Insert);
  1406. break;
  1407. case DataControlCommands.UpdateCommandName:
  1408. UpdateItem (param, causesValidation);
  1409. break;
  1410. case DataControlCommands.CancelCommandName:
  1411. CancelEdit ();
  1412. break;
  1413. case DataControlCommands.DeleteCommandName:
  1414. DeleteItem ();
  1415. break;
  1416. case DataControlCommands.InsertCommandName:
  1417. InsertItem (causesValidation);
  1418. break;
  1419. }
  1420. }
  1421. #if NET_4_0
  1422. public
  1423. #endif
  1424. void SetPageIndex (int newIndex)
  1425. {
  1426. DetailsViewPageEventArgs args = new DetailsViewPageEventArgs (newIndex);
  1427. OnPageIndexChanging (args);
  1428. if (args.Cancel || !IsBoundUsingDataSourceID)
  1429. return;
  1430. if (args.NewPageIndex < 0 || args.NewPageIndex >= PageCount)
  1431. return;
  1432. EndRowEdit (false);
  1433. PageIndex = args.NewPageIndex;
  1434. OnPageIndexChanged (EventArgs.Empty);
  1435. }
  1436. public void ChangeMode (DetailsViewMode newMode)
  1437. {
  1438. CurrentMode = newMode;
  1439. RequireBinding ();
  1440. }
  1441. void ProcessChangeMode (DetailsViewMode newMode)
  1442. {
  1443. DetailsViewModeEventArgs args = new DetailsViewModeEventArgs (newMode, false);
  1444. OnModeChanging (args);
  1445. if (args.Cancel || !IsBoundUsingDataSourceID)
  1446. return;
  1447. ChangeMode (args.NewMode);
  1448. OnModeChanged (EventArgs.Empty);
  1449. }
  1450. void CancelEdit ()
  1451. {
  1452. DetailsViewModeEventArgs args = new DetailsViewModeEventArgs (DetailsViewMode.ReadOnly, true);
  1453. OnModeChanging (args);
  1454. if (args.Cancel || !IsBoundUsingDataSourceID)
  1455. return;
  1456. EndRowEdit ();
  1457. }
  1458. public virtual void UpdateItem (bool causesValidation)
  1459. {
  1460. UpdateItem (null, causesValidation);
  1461. }
  1462. void UpdateItem (string param, bool causesValidation)
  1463. {
  1464. if (causesValidation && Page != null && !Page.IsValid)
  1465. return;
  1466. if (CurrentMode != DetailsViewMode.Edit)
  1467. throw new HttpException ();
  1468. currentEditOldValues = OldEditValues.Values;
  1469. currentEditRowKeys = DataKey.Values;
  1470. currentEditNewValues = GetRowValues (false, false);
  1471. DetailsViewUpdateEventArgs args = new DetailsViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1472. OnItemUpdating (args);
  1473. if (args.Cancel || !IsBoundUsingDataSourceID)
  1474. return;
  1475. DataSourceView view = GetData ();
  1476. if (view == null)
  1477. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1478. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1479. }
  1480. bool UpdateCallback (int recordsAff

Large files files are truncated, but you can click here to view the full file