2
0

FormView.cs 43 KB

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