FormView.cs 45 KB

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