FormView.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. //
  2. // System.Web.UI.WebControls.FormView.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 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. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.ComponentModel;
  33. using System.Web.UI;
  34. using System.Security.Permissions;
  35. using System.Text;
  36. using System.IO;
  37. using System.Reflection;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [SupportsEventValidation]
  41. [DesignerAttribute ("System.Web.UI.Design.WebControls.FormViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  42. [ControlValuePropertyAttribute ("SelectedValue")]
  43. [DefaultEventAttribute ("PageIndexChanging")]
  44. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  45. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  46. public class FormView: CompositeDataBoundControl, IDataItemContainer, INamingContainer, IPostBackEventHandler, IPostBackContainer
  47. {
  48. object dataItem;
  49. Table table;
  50. FormViewRow headerRow;
  51. FormViewRow footerRow;
  52. FormViewRow bottomPagerRow;
  53. FormViewRow topPagerRow;
  54. FormViewRow itemRow;
  55. IOrderedDictionary currentEditRowKeys;
  56. IOrderedDictionary currentEditNewValues;
  57. IOrderedDictionary currentEditOldValues;
  58. ITemplate pagerTemplate;
  59. ITemplate emptyDataTemplate;
  60. ITemplate headerTemplate;
  61. ITemplate footerTemplate;
  62. ITemplate editItemTemplate;
  63. ITemplate insertItemTemplate;
  64. ITemplate itemTemplate;
  65. PropertyDescriptor[] cachedKeyProperties;
  66. readonly string[] emptyKeys = new string[0];
  67. // View state
  68. PagerSettings pagerSettings;
  69. TableItemStyle editRowStyle;
  70. TableItemStyle insertRowStyle;
  71. TableItemStyle emptyDataRowStyle;
  72. TableItemStyle footerStyle;
  73. TableItemStyle headerStyle;
  74. TableItemStyle pagerStyle;
  75. TableItemStyle rowStyle;
  76. DataKey key;
  77. DataKey oldEditValues;
  78. private static readonly object PageIndexChangedEvent = new object();
  79. private static readonly object PageIndexChangingEvent = new object();
  80. private static readonly object ItemCommandEvent = new object();
  81. private static readonly object ItemCreatedEvent = new object();
  82. private static readonly object ItemDeletedEvent = new object();
  83. private static readonly object ItemDeletingEvent = new object();
  84. private static readonly object ItemInsertedEvent = new object();
  85. private static readonly object ItemInsertingEvent = new object();
  86. private static readonly object ModeChangingEvent = new object();
  87. private static readonly object ModeChangedEvent = new object();
  88. private static readonly object ItemUpdatedEvent = new object();
  89. private static readonly object ItemUpdatingEvent = new object();
  90. // Control state
  91. int pageIndex;
  92. FormViewMode currentMode = FormViewMode.ReadOnly;
  93. int pageCount = -1;
  94. public FormView ()
  95. {
  96. }
  97. public event EventHandler PageIndexChanged {
  98. add { Events.AddHandler (PageIndexChangedEvent, value); }
  99. remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
  100. }
  101. public event FormViewPageEventHandler PageIndexChanging {
  102. add { Events.AddHandler (PageIndexChangingEvent, value); }
  103. remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
  104. }
  105. public event FormViewCommandEventHandler ItemCommand {
  106. add { Events.AddHandler (ItemCommandEvent, value); }
  107. remove { Events.RemoveHandler (ItemCommandEvent, value); }
  108. }
  109. public event EventHandler ItemCreated {
  110. add { Events.AddHandler (ItemCreatedEvent, value); }
  111. remove { Events.RemoveHandler (ItemCreatedEvent, value); }
  112. }
  113. public event FormViewDeletedEventHandler ItemDeleted {
  114. add { Events.AddHandler (ItemDeletedEvent, value); }
  115. remove { Events.RemoveHandler (ItemDeletedEvent, value); }
  116. }
  117. public event FormViewDeleteEventHandler ItemDeleting {
  118. add { Events.AddHandler (ItemDeletingEvent, value); }
  119. remove { Events.RemoveHandler (ItemDeletingEvent, value); }
  120. }
  121. public event FormViewInsertedEventHandler ItemInserted {
  122. add { Events.AddHandler (ItemInsertedEvent, value); }
  123. remove { Events.RemoveHandler (ItemInsertedEvent, value); }
  124. }
  125. public event FormViewInsertEventHandler ItemInserting {
  126. add { Events.AddHandler (ItemInsertingEvent, value); }
  127. remove { Events.RemoveHandler (ItemInsertingEvent, value); }
  128. }
  129. public event FormViewModeEventHandler ModeChanging {
  130. add { Events.AddHandler (ModeChangingEvent, value); }
  131. remove { Events.RemoveHandler (ModeChangingEvent, value); }
  132. }
  133. public event EventHandler ModeChanged {
  134. add { Events.AddHandler (ModeChangedEvent, value); }
  135. remove { Events.RemoveHandler (ModeChangedEvent, value); }
  136. }
  137. public event FormViewUpdatedEventHandler ItemUpdated {
  138. add { Events.AddHandler (ItemUpdatedEvent, value); }
  139. remove { Events.RemoveHandler (ItemUpdatedEvent, value); }
  140. }
  141. public event FormViewUpdateEventHandler ItemUpdating {
  142. add { Events.AddHandler (ItemUpdatingEvent, value); }
  143. remove { Events.RemoveHandler (ItemUpdatingEvent, value); }
  144. }
  145. protected virtual void OnPageIndexChanged (EventArgs e)
  146. {
  147. if (Events != null) {
  148. EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
  149. if (eh != null) eh (this, e);
  150. }
  151. }
  152. protected virtual void OnPageIndexChanging (FormViewPageEventArgs e)
  153. {
  154. if (Events != null) {
  155. FormViewPageEventHandler eh = (FormViewPageEventHandler) Events [PageIndexChangingEvent];
  156. if (eh != null) eh (this, e);
  157. }
  158. }
  159. protected virtual void OnItemCommand (FormViewCommandEventArgs e)
  160. {
  161. if (Events != null) {
  162. FormViewCommandEventHandler eh = (FormViewCommandEventHandler) Events [ItemCommandEvent];
  163. if (eh != null) eh (this, e);
  164. }
  165. }
  166. protected virtual void OnItemCreated (EventArgs e)
  167. {
  168. if (Events != null) {
  169. EventHandler eh = (EventHandler) Events [ItemCreatedEvent];
  170. if (eh != null) eh (this, e);
  171. }
  172. }
  173. protected virtual void OnItemDeleted (FormViewDeletedEventArgs e)
  174. {
  175. if (Events != null) {
  176. FormViewDeletedEventHandler eh = (FormViewDeletedEventHandler) Events [ItemDeletedEvent];
  177. if (eh != null) eh (this, e);
  178. }
  179. }
  180. protected virtual void OnItemInserted (FormViewInsertedEventArgs e)
  181. {
  182. if (Events != null) {
  183. FormViewInsertedEventHandler eh = (FormViewInsertedEventHandler) Events [ItemInsertedEvent];
  184. if (eh != null) eh (this, e);
  185. }
  186. }
  187. protected virtual void OnItemInserting (FormViewInsertEventArgs e)
  188. {
  189. if (Events != null) {
  190. FormViewInsertEventHandler eh = (FormViewInsertEventHandler) Events [ItemInsertingEvent];
  191. if (eh != null) eh (this, e);
  192. }
  193. }
  194. protected virtual void OnItemDeleting (FormViewDeleteEventArgs e)
  195. {
  196. if (Events != null) {
  197. FormViewDeleteEventHandler eh = (FormViewDeleteEventHandler) Events [ItemDeletingEvent];
  198. if (eh != null) eh (this, e);
  199. }
  200. }
  201. protected virtual void OnModeChanged (EventArgs e)
  202. {
  203. if (Events != null) {
  204. EventHandler eh = (EventHandler) Events [ModeChangedEvent];
  205. if (eh != null) eh (this, e);
  206. }
  207. }
  208. protected virtual void OnModeChanging (FormViewModeEventArgs e)
  209. {
  210. if (Events != null) {
  211. FormViewModeEventHandler eh = (FormViewModeEventHandler) Events [ModeChangingEvent];
  212. if (eh != null) eh (this, e);
  213. }
  214. }
  215. protected virtual void OnItemUpdated (FormViewUpdatedEventArgs e)
  216. {
  217. if (Events != null) {
  218. FormViewUpdatedEventHandler eh = (FormViewUpdatedEventHandler) Events [ItemUpdatedEvent];
  219. if (eh != null) eh (this, e);
  220. }
  221. }
  222. protected virtual void OnItemUpdating (FormViewUpdateEventArgs e)
  223. {
  224. if (Events != null) {
  225. FormViewUpdateEventHandler eh = (FormViewUpdateEventHandler) Events [ItemUpdatingEvent];
  226. if (eh != null) eh (this, e);
  227. }
  228. }
  229. [WebCategoryAttribute ("Paging")]
  230. [DefaultValueAttribute (false)]
  231. public virtual bool AllowPaging {
  232. get {
  233. object ob = ViewState ["AllowPaging"];
  234. if (ob != null) return (bool) ob;
  235. return false;
  236. }
  237. set {
  238. ViewState ["AllowPaging"] = value;
  239. RequireBinding ();
  240. }
  241. }
  242. [UrlPropertyAttribute]
  243. [WebCategoryAttribute ("Appearance")]
  244. [DefaultValueAttribute ("")]
  245. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  246. public virtual string BackImageUrl {
  247. get {
  248. object ob = ViewState ["BackImageUrl"];
  249. if (ob != null) return (string) ob;
  250. return string.Empty;
  251. }
  252. set {
  253. ViewState ["BackImageUrl"] = value;
  254. RequireBinding ();
  255. }
  256. }
  257. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  258. [BrowsableAttribute (false)]
  259. public virtual FormViewRow BottomPagerRow {
  260. get {
  261. EnsureDataBound ();
  262. return bottomPagerRow;
  263. }
  264. }
  265. [WebCategoryAttribute ("Accessibility")]
  266. [DefaultValueAttribute ("")]
  267. [LocalizableAttribute (true)]
  268. public virtual string Caption {
  269. get {
  270. object ob = ViewState ["Caption"];
  271. if (ob != null) return (string) ob;
  272. return string.Empty;
  273. }
  274. set {
  275. ViewState ["Caption"] = value;
  276. RequireBinding ();
  277. }
  278. }
  279. [WebCategoryAttribute ("Accessibility")]
  280. [DefaultValueAttribute (TableCaptionAlign.NotSet)]
  281. public virtual TableCaptionAlign CaptionAlign
  282. {
  283. get {
  284. object o = ViewState ["CaptionAlign"];
  285. if(o != null) return (TableCaptionAlign) o;
  286. return TableCaptionAlign.NotSet;
  287. }
  288. set {
  289. ViewState ["CaptionAlign"] = value;
  290. RequireBinding ();
  291. }
  292. }
  293. [WebCategoryAttribute ("Layout")]
  294. [DefaultValueAttribute (-1)]
  295. public virtual int CellPadding
  296. {
  297. get {
  298. object o = ViewState ["CellPadding"];
  299. if (o != null) return (int) o;
  300. return -1;
  301. }
  302. set {
  303. ViewState ["CellPadding"] = value;
  304. RequireBinding ();
  305. }
  306. }
  307. [WebCategoryAttribute ("Layout")]
  308. [DefaultValueAttribute (0)]
  309. public virtual int CellSpacing
  310. {
  311. get {
  312. object o = ViewState ["CellSpacing"];
  313. if (o != null) return (int) o;
  314. return 0;
  315. }
  316. set {
  317. ViewState ["CellSpacing"] = value;
  318. RequireBinding ();
  319. }
  320. }
  321. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  322. [BrowsableAttribute (false)]
  323. public FormViewMode CurrentMode {
  324. get {
  325. return currentMode;
  326. }
  327. }
  328. [DefaultValueAttribute (FormViewMode.ReadOnly)]
  329. [WebCategoryAttribute ("Behavior")]
  330. public virtual FormViewMode DefaultMode {
  331. get {
  332. object o = ViewState ["DefaultMode"];
  333. if (o != null) return (FormViewMode) o;
  334. return FormViewMode.ReadOnly;
  335. }
  336. set {
  337. ViewState ["DefaultMode"] = value;
  338. RequireBinding ();
  339. }
  340. }
  341. [DefaultValueAttribute (null)]
  342. [WebCategoryAttribute ("Data")]
  343. [TypeConverter (typeof(StringArrayConverter))]
  344. [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  345. public virtual string[] DataKeyNames
  346. {
  347. get {
  348. object o = ViewState ["DataKeyNames"];
  349. if (o != null) return (string[]) o;
  350. return emptyKeys;
  351. }
  352. set {
  353. ViewState ["DataKeyNames"] = value;
  354. RequireBinding ();
  355. }
  356. }
  357. [BrowsableAttribute (false)]
  358. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  359. public virtual DataKey DataKey {
  360. get {
  361. EnsureDataBound ();
  362. return key;
  363. }
  364. }
  365. [DefaultValue (null)]
  366. [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
  367. [PersistenceMode (PersistenceMode.InnerProperty)]
  368. [Browsable (false)]
  369. public virtual ITemplate EditItemTemplate {
  370. get { return editItemTemplate; }
  371. set { editItemTemplate = value; RequireBinding (); }
  372. }
  373. [WebCategoryAttribute ("Styles")]
  374. [PersistenceMode (PersistenceMode.InnerProperty)]
  375. [NotifyParentProperty (true)]
  376. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  377. [DefaultValueAttribute (null)]
  378. public TableItemStyle EditRowStyle {
  379. get {
  380. if (editRowStyle == null) {
  381. editRowStyle = new TableItemStyle ();
  382. if (IsTrackingViewState)
  383. editRowStyle.TrackViewState();
  384. }
  385. return editRowStyle;
  386. }
  387. }
  388. [WebCategoryAttribute ("Styles")]
  389. [PersistenceMode (PersistenceMode.InnerProperty)]
  390. [NotifyParentProperty (true)]
  391. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  392. [DefaultValueAttribute (null)]
  393. public TableItemStyle EmptyDataRowStyle {
  394. get {
  395. if (emptyDataRowStyle == null) {
  396. emptyDataRowStyle = new TableItemStyle ();
  397. if (IsTrackingViewState)
  398. emptyDataRowStyle.TrackViewState();
  399. }
  400. return emptyDataRowStyle;
  401. }
  402. }
  403. [DefaultValue (null)]
  404. [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
  405. [PersistenceMode (PersistenceMode.InnerProperty)]
  406. [Browsable (false)]
  407. public virtual ITemplate EmptyDataTemplate {
  408. get { return emptyDataTemplate; }
  409. set { emptyDataTemplate = value; RequireBinding (); }
  410. }
  411. [LocalizableAttribute (true)]
  412. [WebCategoryAttribute ("Appearance")]
  413. [DefaultValueAttribute ("")]
  414. public virtual string EmptyDataText {
  415. get {
  416. object ob = ViewState ["EmptyDataText"];
  417. if (ob != null) return (string) ob;
  418. return string.Empty;
  419. }
  420. set {
  421. ViewState ["EmptyDataText"] = value;
  422. RequireBinding ();
  423. }
  424. }
  425. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  426. [BrowsableAttribute (false)]
  427. public virtual FormViewRow FooterRow {
  428. get {
  429. EnsureChildControls ();
  430. return footerRow;
  431. }
  432. }
  433. [DefaultValue (null)]
  434. [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
  435. [PersistenceMode (PersistenceMode.InnerProperty)]
  436. [Browsable (false)]
  437. public virtual ITemplate FooterTemplate {
  438. get { return footerTemplate; }
  439. set { footerTemplate = value; RequireBinding (); }
  440. }
  441. [LocalizableAttribute (true)]
  442. [WebCategoryAttribute ("Appearance")]
  443. [DefaultValueAttribute ("")]
  444. public virtual string FooterText {
  445. get {
  446. object ob = ViewState ["FooterText"];
  447. if (ob != null) return (string) ob;
  448. return string.Empty;
  449. }
  450. set {
  451. ViewState ["FooterText"] = value;
  452. RequireBinding ();
  453. }
  454. }
  455. [WebCategoryAttribute ("Styles")]
  456. [PersistenceMode (PersistenceMode.InnerProperty)]
  457. [NotifyParentProperty (true)]
  458. [DefaultValue (null)]
  459. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  460. public TableItemStyle FooterStyle {
  461. get {
  462. if (footerStyle == null) {
  463. footerStyle = new TableItemStyle ();
  464. if (IsTrackingViewState)
  465. footerStyle.TrackViewState();
  466. }
  467. return footerStyle;
  468. }
  469. }
  470. [WebCategoryAttribute ("Appearance")]
  471. [DefaultValueAttribute (GridLines.None)]
  472. public virtual GridLines GridLines {
  473. get {
  474. object ob = ViewState ["GridLines"];
  475. if (ob != null) return (GridLines) ob;
  476. return GridLines.None;
  477. }
  478. set {
  479. ViewState ["GridLines"] = value;
  480. }
  481. }
  482. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  483. [BrowsableAttribute (false)]
  484. public virtual FormViewRow HeaderRow {
  485. get {
  486. EnsureChildControls ();
  487. return headerRow;
  488. }
  489. }
  490. [WebCategoryAttribute ("Styles")]
  491. [PersistenceMode (PersistenceMode.InnerProperty)]
  492. [NotifyParentProperty (true)]
  493. [DefaultValue (null)]
  494. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  495. public TableItemStyle HeaderStyle {
  496. get {
  497. if (headerStyle == null) {
  498. headerStyle = new TableItemStyle ();
  499. if (IsTrackingViewState)
  500. headerStyle.TrackViewState();
  501. }
  502. return headerStyle;
  503. }
  504. }
  505. [DefaultValue (null)]
  506. [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
  507. [PersistenceMode (PersistenceMode.InnerProperty)]
  508. [Browsable (false)]
  509. public virtual ITemplate HeaderTemplate {
  510. get { return headerTemplate; }
  511. set { headerTemplate = value; RequireBinding (); }
  512. }
  513. [LocalizableAttribute (true)]
  514. [WebCategoryAttribute ("Appearance")]
  515. [DefaultValueAttribute ("")]
  516. public virtual string HeaderText {
  517. get {
  518. object ob = ViewState ["HeaderText"];
  519. if (ob != null) return (string) ob;
  520. return string.Empty;
  521. }
  522. set {
  523. ViewState ["HeaderText"] = value;
  524. RequireBinding ();
  525. }
  526. }
  527. [Category ("Layout")]
  528. [DefaultValueAttribute (HorizontalAlign.NotSet)]
  529. public virtual HorizontalAlign HorizontalAlign {
  530. get {
  531. object ob = ViewState ["HorizontalAlign"];
  532. if (ob != null) return (HorizontalAlign) ob;
  533. return HorizontalAlign.NotSet;
  534. }
  535. set {
  536. ViewState ["HorizontalAlign"] = value;
  537. RequireBinding ();
  538. }
  539. }
  540. [DefaultValue (null)]
  541. [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
  542. [PersistenceMode (PersistenceMode.InnerProperty)]
  543. [Browsable (false)]
  544. public virtual ITemplate InsertItemTemplate {
  545. get { return insertItemTemplate; }
  546. set { insertItemTemplate = value; RequireBinding (); }
  547. }
  548. [WebCategoryAttribute ("Styles")]
  549. [PersistenceMode (PersistenceMode.InnerProperty)]
  550. [NotifyParentProperty (true)]
  551. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  552. [DefaultValueAttribute (null)]
  553. public TableItemStyle InsertRowStyle {
  554. get {
  555. if (insertRowStyle == null) {
  556. insertRowStyle = new TableItemStyle ();
  557. if (IsTrackingViewState)
  558. insertRowStyle.TrackViewState();
  559. }
  560. return insertRowStyle;
  561. }
  562. }
  563. [DefaultValue (null)]
  564. [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
  565. [PersistenceMode (PersistenceMode.InnerProperty)]
  566. [Browsable (false)]
  567. public virtual ITemplate ItemTemplate {
  568. get { return itemTemplate; }
  569. set { itemTemplate = value; RequireBinding (); }
  570. }
  571. [BrowsableAttribute (false)]
  572. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  573. public virtual int PageCount {
  574. get {
  575. if (pageCount != -1) return pageCount;
  576. EnsureDataBound ();
  577. return pageCount;
  578. }
  579. }
  580. [WebCategoryAttribute ("Paging")]
  581. [BindableAttribute (true, BindingDirection.OneWay)]
  582. [DefaultValueAttribute (0)]
  583. public virtual int PageIndex {
  584. get {
  585. return pageIndex;
  586. }
  587. set {
  588. if (pageIndex == value)
  589. return;
  590. pageIndex = value;
  591. RequireBinding ();
  592. }
  593. }
  594. [WebCategoryAttribute ("Paging")]
  595. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  596. [NotifyParentPropertyAttribute (true)]
  597. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  598. public virtual PagerSettings PagerSettings {
  599. get {
  600. if (pagerSettings == null) {
  601. pagerSettings = new PagerSettings (this);
  602. if (IsTrackingViewState)
  603. ((IStateManager)pagerSettings).TrackViewState ();
  604. }
  605. return pagerSettings;
  606. }
  607. }
  608. [WebCategoryAttribute ("Styles")]
  609. [PersistenceMode (PersistenceMode.InnerProperty)]
  610. [NotifyParentProperty (true)]
  611. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  612. public TableItemStyle PagerStyle {
  613. get {
  614. if (pagerStyle == null) {
  615. pagerStyle = new TableItemStyle ();
  616. if (IsTrackingViewState)
  617. pagerStyle.TrackViewState();
  618. }
  619. return pagerStyle;
  620. }
  621. }
  622. [DefaultValue (null)]
  623. /* DataControlPagerCell isnt specified in the docs */
  624. //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
  625. [PersistenceMode (PersistenceMode.InnerProperty)]
  626. [Browsable (false)]
  627. public virtual ITemplate PagerTemplate {
  628. get { return pagerTemplate; }
  629. set { pagerTemplate = value; RequireBinding (); }
  630. }
  631. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  632. [BrowsableAttribute (false)]
  633. public virtual FormViewRow Row {
  634. get {
  635. EnsureDataBound ();
  636. return itemRow;
  637. }
  638. }
  639. [WebCategoryAttribute ("Styles")]
  640. [PersistenceMode (PersistenceMode.InnerProperty)]
  641. [NotifyParentProperty (true)]
  642. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  643. [DefaultValue (null)]
  644. public TableItemStyle RowStyle {
  645. get {
  646. if (rowStyle == null) {
  647. rowStyle = new TableItemStyle ();
  648. if (IsTrackingViewState)
  649. rowStyle.TrackViewState();
  650. }
  651. return rowStyle;
  652. }
  653. }
  654. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  655. [BrowsableAttribute (false)]
  656. public object SelectedValue {
  657. get { return DataKey.Value; }
  658. }
  659. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  660. [BrowsableAttribute (false)]
  661. public virtual FormViewRow TopPagerRow {
  662. get {
  663. EnsureDataBound ();
  664. return topPagerRow;
  665. }
  666. }
  667. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  668. [BrowsableAttribute (false)]
  669. public virtual object DataItem {
  670. get {
  671. EnsureDataBound ();
  672. return dataItem;
  673. }
  674. }
  675. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  676. [BrowsableAttribute (false)]
  677. public int DataItemCount {
  678. get { return PageCount; }
  679. }
  680. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  681. [BrowsableAttribute (false)]
  682. public virtual int DataItemIndex {
  683. get { return PageIndex; }
  684. }
  685. int IDataItemContainer.DisplayIndex {
  686. get { return PageIndex; }
  687. }
  688. public virtual bool IsBindableType (Type type)
  689. {
  690. return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
  691. }
  692. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  693. {
  694. return base.CreateDataSourceSelectArguments ();
  695. }
  696. protected virtual FormViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
  697. {
  698. FormViewRow row = new FormViewRow (rowIndex, rowType, rowState);
  699. OnItemCreated (EventArgs.Empty);
  700. return row;
  701. }
  702. void RequireBinding ()
  703. {
  704. if (Initialized) {
  705. pageCount = -1;
  706. RequiresDataBinding = true;
  707. }
  708. }
  709. protected virtual Table CreateTable ()
  710. {
  711. Table table = new Table ();
  712. table.Caption = Caption;
  713. table.CaptionAlign = CaptionAlign;
  714. table.CellPadding = CellPadding;
  715. table.CellSpacing = CellSpacing;
  716. table.HorizontalAlign = HorizontalAlign;
  717. table.BackImageUrl = BackImageUrl;
  718. return table;
  719. }
  720. [MonoTODO]
  721. protected override void EnsureDataBound ()
  722. {
  723. base.EnsureDataBound ();
  724. }
  725. [MonoTODO]
  726. protected override Style CreateControlStyle ()
  727. {
  728. return base.CreateControlStyle ();
  729. }
  730. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  731. {
  732. PagedDataSource dataSource = new PagedDataSource ();
  733. dataSource.DataSource = data;
  734. dataSource.AllowPaging = AllowPaging;
  735. dataSource.PageSize = 1;
  736. dataSource.CurrentPageIndex = PageIndex;
  737. if (dataBinding) {
  738. DataSourceView view = GetData ();
  739. if (view.CanPage) {
  740. dataSource.AllowServerPaging = true;
  741. if (view.CanRetrieveTotalRowCount)
  742. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  743. else {
  744. dataSource.DataSourceView = view;
  745. dataSource.DataSourceSelectArguments = SelectArguments;
  746. dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
  747. }
  748. }
  749. }
  750. pageCount = dataSource.DataSourceCount;
  751. bool showPager = AllowPaging && (dataSource.Count > 1);
  752. Controls.Clear ();
  753. table = CreateTable ();
  754. Controls.Add (table);
  755. if (!Page.IsPostBack)
  756. currentMode = DefaultMode;
  757. // Gets the current data item
  758. IEnumerator e = dataSource.GetEnumerator ();
  759. dataItem = null;
  760. if (AllowPaging) {
  761. if (e.MoveNext ())
  762. dataItem = e.Current;
  763. }
  764. else
  765. for (int page = 0; e.MoveNext (); page++ )
  766. if (page == PageIndex)
  767. dataItem = e.Current;
  768. // Main table creation
  769. if (HeaderText.Length != 0 || headerTemplate != null) {
  770. headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
  771. InitializeRow (headerRow);
  772. table.Rows.Add (headerRow);
  773. }
  774. if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
  775. topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
  776. InitializePager (topPagerRow, dataSource);
  777. table.Rows.Add (topPagerRow);
  778. }
  779. if (pageCount > 0) {
  780. DataControlRowState rstate = GetRowState ();
  781. itemRow = CreateRow (0, DataControlRowType.DataRow, rstate);
  782. InitializeRow (itemRow);
  783. table.Rows.Add (itemRow);
  784. if (!dataBinding) {
  785. if (CurrentMode == FormViewMode.Edit)
  786. oldEditValues = new DataKey (new OrderedDictionary ());
  787. key = new DataKey (new OrderedDictionary (), DataKeyNames);
  788. }
  789. } else {
  790. itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  791. table.Rows.Add (itemRow);
  792. InitializeRow (itemRow);
  793. }
  794. if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
  795. bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
  796. InitializePager (bottomPagerRow, dataSource);
  797. table.Rows.Add (bottomPagerRow);
  798. }
  799. if (FooterText.Length != 0 || footerTemplate != null) {
  800. footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
  801. InitializeRow (footerRow);
  802. table.Rows.Add (footerRow);
  803. }
  804. if (dataBinding)
  805. DataBind (false);
  806. return dataSource.DataSourceCount;
  807. }
  808. DataControlRowState GetRowState ()
  809. {
  810. DataControlRowState rstate = DataControlRowState.Normal;
  811. if (CurrentMode == FormViewMode.Edit) rstate |= DataControlRowState.Edit;
  812. else if (CurrentMode == FormViewMode.Insert) rstate |= DataControlRowState.Insert;
  813. return rstate;
  814. }
  815. protected virtual void InitializePager (FormViewRow row, PagedDataSource dataSource)
  816. {
  817. TableCell cell = new TableCell ();
  818. if (pagerTemplate != null)
  819. pagerTemplate.InstantiateIn (cell);
  820. else
  821. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  822. row.Cells.Add (cell);
  823. }
  824. protected virtual void InitializeRow (FormViewRow row)
  825. {
  826. TableCell cell = new TableCell ();
  827. if (row.RowType == DataControlRowType.DataRow)
  828. {
  829. if ((row.RowState & DataControlRowState.Edit) != 0) {
  830. if (editItemTemplate != null)
  831. editItemTemplate.InstantiateIn (cell);
  832. } else if ((row.RowState & DataControlRowState.Insert) != 0) {
  833. if (insertItemTemplate != null)
  834. insertItemTemplate.InstantiateIn (cell);
  835. } else if (itemTemplate != null)
  836. itemTemplate.InstantiateIn (cell);
  837. }
  838. else if (row.RowType == DataControlRowType.EmptyDataRow)
  839. {
  840. if (emptyDataTemplate != null)
  841. emptyDataTemplate.InstantiateIn (cell);
  842. else
  843. cell.Text = EmptyDataText;
  844. }
  845. else if (row.RowType == DataControlRowType.Footer)
  846. {
  847. if (footerTemplate != null)
  848. footerTemplate.InstantiateIn (cell);
  849. else
  850. cell.Text = FooterText;
  851. }
  852. else if (row.RowType == DataControlRowType.Header)
  853. {
  854. if (headerTemplate != null)
  855. headerTemplate.InstantiateIn (cell);
  856. else
  857. cell.Text = HeaderText;
  858. }
  859. row.Cells.Add (cell);
  860. }
  861. IOrderedDictionary CreateRowDataKey (object dataItem)
  862. {
  863. if (cachedKeyProperties == null) {
  864. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
  865. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  866. for (int n=0; n<DataKeyNames.Length; n++) {
  867. PropertyDescriptor p = props [DataKeyNames[n]];
  868. if (p == null)
  869. new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
  870. cachedKeyProperties [n] = p;
  871. }
  872. }
  873. OrderedDictionary dic = new OrderedDictionary ();
  874. foreach (PropertyDescriptor p in cachedKeyProperties)
  875. dic [p.Name] = p.GetValue (dataItem);
  876. return dic;
  877. }
  878. IOrderedDictionary GetRowValues (bool includePrimaryKey)
  879. {
  880. OrderedDictionary dic = new OrderedDictionary ();
  881. ExtractRowValues (dic, includePrimaryKey);
  882. return dic;
  883. }
  884. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeKeys)
  885. {
  886. DataControlRowState rowState = Row.RowState;
  887. IBindableTemplate bt;
  888. if ((rowState & DataControlRowState.Insert) != 0)
  889. bt = insertItemTemplate as IBindableTemplate;
  890. else if ((rowState & DataControlRowState.Edit) != 0)
  891. bt = editItemTemplate as IBindableTemplate;
  892. else
  893. return;
  894. if (bt != null) {
  895. IOrderedDictionary values = bt.ExtractValues (Row.Cells [0]);
  896. foreach (DictionaryEntry e in values) {
  897. if (includeKeys || Array.IndexOf (DataKeyNames, e.Key) == -1)
  898. fieldValues [e.Key] = e.Value;
  899. }
  900. }
  901. }
  902. protected override HtmlTextWriterTag TagKey {
  903. get {
  904. return HtmlTextWriterTag.Table;
  905. }
  906. }
  907. public sealed override void DataBind ()
  908. {
  909. DataSourceView view = GetData ();
  910. if (AllowPaging && view.CanPage) {
  911. SelectArguments.StartRowIndex = PageIndex;
  912. SelectArguments.MaximumRows = 1;
  913. if (view.CanRetrieveTotalRowCount)
  914. SelectArguments.RetrieveTotalRowCount = true;
  915. }
  916. cachedKeyProperties = null;
  917. base.DataBind ();
  918. if (pageCount > 0) {
  919. if (CurrentMode == FormViewMode.Edit)
  920. oldEditValues = new DataKey (GetRowValues (true));
  921. key = new DataKey (CreateRowDataKey (dataItem), DataKeyNames);
  922. }
  923. }
  924. protected internal override void PerformDataBinding (IEnumerable data)
  925. {
  926. base.PerformDataBinding (data);
  927. }
  928. [MonoTODO]
  929. protected internal virtual void PrepareControlHierarchy ()
  930. {
  931. throw new NotImplementedException ();
  932. }
  933. protected internal override void OnInit (EventArgs e)
  934. {
  935. Page.RegisterRequiresControlState (this);
  936. base.OnInit (e);
  937. }
  938. protected override bool OnBubbleEvent (object source, EventArgs e)
  939. {
  940. FormViewCommandEventArgs args = e as FormViewCommandEventArgs;
  941. if (args != null) {
  942. OnItemCommand (args);
  943. ProcessEvent (args.CommandName, args.CommandArgument as string);
  944. }
  945. return base.OnBubbleEvent (source, e);
  946. }
  947. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  948. {
  949. RaisePostBackEvent (eventArgument);
  950. }
  951. protected virtual void RaisePostBackEvent (string eventArgument)
  952. {
  953. int i = eventArgument.IndexOf ('$');
  954. if (i != -1)
  955. ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
  956. else
  957. ProcessEvent (eventArgument, null);
  958. }
  959. void ProcessEvent (string eventName, string param)
  960. {
  961. switch (eventName)
  962. {
  963. case DataControlCommands.PageCommandName:
  964. int newIndex = -1;
  965. switch (param) {
  966. case DataControlCommands.FirstPageCommandArgument:
  967. newIndex = 0;
  968. break;
  969. case DataControlCommands.LastPageCommandArgument:
  970. newIndex = PageCount - 1;
  971. break;
  972. case DataControlCommands.NextPageCommandArgument:
  973. if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
  974. break;
  975. case DataControlCommands.PreviousPageCommandArgument:
  976. if (PageIndex > 0) newIndex = PageIndex - 1;
  977. break;
  978. default:
  979. newIndex = int.Parse (param) - 1;
  980. break;
  981. }
  982. ShowPage (newIndex);
  983. break;
  984. case DataControlCommands.FirstPageCommandArgument:
  985. ShowPage (0);
  986. break;
  987. case DataControlCommands.LastPageCommandArgument:
  988. ShowPage (PageCount - 1);
  989. break;
  990. case DataControlCommands.NextPageCommandArgument:
  991. if (PageIndex < PageCount - 1)
  992. ShowPage (PageIndex + 1);
  993. break;
  994. case DataControlCommands.PreviousPageCommandArgument:
  995. if (PageIndex > 0)
  996. ShowPage (PageIndex - 1);
  997. break;
  998. case DataControlCommands.EditCommandName:
  999. ChangeMode (FormViewMode.Edit);
  1000. break;
  1001. case DataControlCommands.NewCommandName:
  1002. ChangeMode (FormViewMode.Insert);
  1003. break;
  1004. case DataControlCommands.UpdateCommandName:
  1005. UpdateItem (param, true);
  1006. break;
  1007. case DataControlCommands.CancelCommandName:
  1008. CancelEdit ();
  1009. break;
  1010. case DataControlCommands.DeleteCommandName:
  1011. DeleteItem ();
  1012. break;
  1013. case DataControlCommands.InsertCommandName:
  1014. InsertItem (true);
  1015. break;
  1016. }
  1017. }
  1018. void ShowPage (int newIndex)
  1019. {
  1020. FormViewPageEventArgs args = new FormViewPageEventArgs (newIndex);
  1021. OnPageIndexChanging (args);
  1022. if (!args.Cancel) {
  1023. EndRowEdit ();
  1024. PageIndex = args.NewPageIndex;
  1025. OnPageIndexChanged (EventArgs.Empty);
  1026. }
  1027. }
  1028. public void ChangeMode (FormViewMode newMode)
  1029. {
  1030. FormViewModeEventArgs args = new FormViewModeEventArgs (newMode, false);
  1031. OnModeChanging (args);
  1032. if (!args.Cancel) {
  1033. currentMode = args.NewMode;
  1034. OnModeChanged (EventArgs.Empty);
  1035. RequireBinding ();
  1036. }
  1037. }
  1038. void CancelEdit ()
  1039. {
  1040. FormViewModeEventArgs args = new FormViewModeEventArgs (FormViewMode.ReadOnly, true);
  1041. OnModeChanging (args);
  1042. if (!args.Cancel) {
  1043. EndRowEdit ();
  1044. }
  1045. }
  1046. public virtual void UpdateItem (bool causesValidation)
  1047. {
  1048. UpdateItem (null, causesValidation);
  1049. }
  1050. void UpdateItem (string param, bool causesValidation)
  1051. {
  1052. if (causesValidation)
  1053. Page.Validate ();
  1054. if (currentMode != FormViewMode.Edit) throw new NotSupportedException ();
  1055. currentEditOldValues = oldEditValues.Values;
  1056. currentEditRowKeys = DataKey.Values;
  1057. currentEditNewValues = GetRowValues (false);
  1058. FormViewUpdateEventArgs args = new FormViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1059. OnItemUpdating (args);
  1060. if (!args.Cancel) {
  1061. DataSourceView view = GetData ();
  1062. if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
  1063. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1064. } else
  1065. EndRowEdit ();
  1066. }
  1067. bool UpdateCallback (int recordsAffected, Exception exception)
  1068. {
  1069. FormViewUpdatedEventArgs dargs = new FormViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1070. OnItemUpdated (dargs);
  1071. if (!dargs.KeepInEditMode)
  1072. EndRowEdit ();
  1073. return dargs.ExceptionHandled;
  1074. }
  1075. public virtual void InsertItem (bool causesValidation)
  1076. {
  1077. InsertItem (null, causesValidation);
  1078. }
  1079. void InsertItem (string param, bool causesValidation)
  1080. {
  1081. if (causesValidation)
  1082. Page.Validate ();
  1083. if (currentMode != FormViewMode.Insert) throw new NotSupportedException ();
  1084. currentEditNewValues = GetRowValues (true);
  1085. FormViewInsertEventArgs args = new FormViewInsertEventArgs (param, currentEditNewValues);
  1086. OnItemInserting (args);
  1087. if (!args.Cancel) {
  1088. DataSourceView view = GetData ();
  1089. if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
  1090. view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
  1091. } else
  1092. EndRowEdit ();
  1093. }
  1094. bool InsertCallback (int recordsAffected, Exception exception)
  1095. {
  1096. FormViewInsertedEventArgs dargs = new FormViewInsertedEventArgs (recordsAffected, exception, currentEditNewValues);
  1097. OnItemInserted (dargs);
  1098. if (!dargs.KeepInInsertMode)
  1099. EndRowEdit ();
  1100. return dargs.ExceptionHandled;
  1101. }
  1102. public virtual void DeleteItem ()
  1103. {
  1104. currentEditRowKeys = DataKey.Values;
  1105. currentEditNewValues = GetRowValues (true);
  1106. FormViewDeleteEventArgs args = new FormViewDeleteEventArgs (PageIndex, currentEditRowKeys, currentEditNewValues);
  1107. OnItemDeleting (args);
  1108. if (!args.Cancel) {
  1109. if (PageIndex == PageCount - 1)
  1110. PageIndex --;
  1111. RequireBinding ();
  1112. DataSourceView view = GetData ();
  1113. if (view != null)
  1114. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1115. else {
  1116. FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1117. OnItemDeleted (dargs);
  1118. }
  1119. }
  1120. }
  1121. bool DeleteCallback (int recordsAffected, Exception exception)
  1122. {
  1123. FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1124. OnItemDeleted (dargs);
  1125. return dargs.ExceptionHandled;
  1126. }
  1127. void EndRowEdit ()
  1128. {
  1129. ChangeMode (DefaultMode);
  1130. oldEditValues = new DataKey (new OrderedDictionary ());
  1131. currentEditRowKeys = null;
  1132. currentEditOldValues = null;
  1133. currentEditNewValues = null;
  1134. RequireBinding ();
  1135. }
  1136. protected internal override void LoadControlState (object ob)
  1137. {
  1138. if (ob == null) return;
  1139. object[] state = (object[]) ob;
  1140. base.LoadControlState (state[0]);
  1141. pageIndex = (int) state[1];
  1142. pageCount = (int) state[2];
  1143. currentMode = (FormViewMode) state[3];
  1144. }
  1145. protected internal override object SaveControlState ()
  1146. {
  1147. object bstate = base.SaveControlState ();
  1148. return new object[] {
  1149. bstate, pageIndex, pageCount, currentMode
  1150. };
  1151. }
  1152. protected override void TrackViewState()
  1153. {
  1154. base.TrackViewState();
  1155. if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
  1156. if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
  1157. if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
  1158. if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
  1159. if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
  1160. if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
  1161. if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
  1162. if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
  1163. if (key != null) ((IStateManager)key).TrackViewState();
  1164. }
  1165. protected override object SaveViewState()
  1166. {
  1167. object[] states = new object [14];
  1168. states[0] = base.SaveViewState();
  1169. states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
  1170. states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
  1171. states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
  1172. states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
  1173. states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
  1174. states[8] = (insertRowStyle == null ? null : ((IStateManager)insertRowStyle).SaveViewState());
  1175. states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
  1176. states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
  1177. states[11] = (key == null ? null : ((IStateManager)key).SaveViewState());
  1178. states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
  1179. for (int i = states.Length - 1; i >= 0; i--) {
  1180. if (states [i] != null)
  1181. return states;
  1182. }
  1183. return null;
  1184. }
  1185. protected override void LoadViewState (object savedState)
  1186. {
  1187. if (savedState == null) {
  1188. base.LoadViewState (null);
  1189. return;
  1190. }
  1191. object [] states = (object []) savedState;
  1192. base.LoadViewState (states[0]);
  1193. EnsureChildControls ();
  1194. if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
  1195. if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
  1196. if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
  1197. if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
  1198. if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
  1199. if (states[8] != null) ((IStateManager)InsertRowStyle).LoadViewState (states[8]);
  1200. if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
  1201. if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
  1202. if (states[11] != null && DataKey != null) ((IStateManager)DataKey).LoadViewState (states[11]);
  1203. if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
  1204. }
  1205. protected internal override void Render (HtmlTextWriter writer)
  1206. {
  1207. switch (GridLines) {
  1208. case GridLines.Horizontal:
  1209. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
  1210. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
  1211. break;
  1212. case GridLines.Vertical:
  1213. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
  1214. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
  1215. break;
  1216. case GridLines.Both:
  1217. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
  1218. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
  1219. break;
  1220. default:
  1221. writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
  1222. break;
  1223. }
  1224. writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
  1225. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
  1226. table.RenderBeginTag (writer);
  1227. foreach (FormViewRow row in table.Rows)
  1228. {
  1229. switch (row.RowType) {
  1230. case DataControlRowType.Header:
  1231. if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
  1232. break;
  1233. case DataControlRowType.Footer:
  1234. if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
  1235. break;
  1236. case DataControlRowType.Pager:
  1237. if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
  1238. break;
  1239. case DataControlRowType.EmptyDataRow:
  1240. if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
  1241. break;
  1242. default:
  1243. if (rowStyle != null) rowStyle.AddAttributesToRender (writer, row);
  1244. break;
  1245. }
  1246. if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
  1247. editRowStyle.AddAttributesToRender (writer, row);
  1248. if ((row.RowState & DataControlRowState.Insert) != 0 && insertRowStyle != null)
  1249. insertRowStyle.AddAttributesToRender (writer, row);
  1250. row.RenderBeginTag (writer);
  1251. for (int n=0; n<row.Cells.Count; n++)
  1252. row.Cells[n].Render (writer);
  1253. row.RenderEndTag (writer);
  1254. }
  1255. table.RenderEndTag (writer);
  1256. }
  1257. [MonoTODO]
  1258. PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
  1259. {
  1260. throw new NotImplementedException ();
  1261. }
  1262. }
  1263. }
  1264. #endif