FormView.cs 46 KB

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