FormView.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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; RequireBinding (); }
  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; RequireBinding (); }
  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; RequireBinding (); }
  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; RequireBinding (); }
  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; RequireBinding (); }
  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; RequireBinding (); }
  613. }
  614. [BrowsableAttribute (false)]
  615. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  616. public virtual int PageCount {
  617. get {
  618. return pageCount;
  619. }
  620. }
  621. [WebCategoryAttribute ("Paging")]
  622. [BindableAttribute (true, BindingDirection.OneWay)]
  623. [DefaultValueAttribute (0)]
  624. public virtual int PageIndex {
  625. get {
  626. return pageIndex;
  627. }
  628. set {
  629. if (value < 0)
  630. throw new ArgumentOutOfRangeException ("PageIndex must be non-negative");
  631. if (pageIndex == value)
  632. return;
  633. pageIndex = value;
  634. RequireBinding ();
  635. }
  636. }
  637. [WebCategoryAttribute ("Paging")]
  638. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  639. [NotifyParentPropertyAttribute (true)]
  640. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  641. public virtual PagerSettings PagerSettings {
  642. get {
  643. if (pagerSettings == null) {
  644. pagerSettings = new PagerSettings (this);
  645. if (IsTrackingViewState)
  646. ((IStateManager)pagerSettings).TrackViewState ();
  647. }
  648. return pagerSettings;
  649. }
  650. }
  651. [WebCategoryAttribute ("Styles")]
  652. [PersistenceMode (PersistenceMode.InnerProperty)]
  653. [NotifyParentProperty (true)]
  654. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  655. public TableItemStyle PagerStyle {
  656. get {
  657. if (pagerStyle == null) {
  658. pagerStyle = new TableItemStyle ();
  659. if (IsTrackingViewState)
  660. pagerStyle.TrackViewState();
  661. }
  662. return pagerStyle;
  663. }
  664. }
  665. [DefaultValue (null)]
  666. /* DataControlPagerCell isnt specified in the docs */
  667. //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
  668. [PersistenceMode (PersistenceMode.InnerProperty)]
  669. [Browsable (false)]
  670. public virtual ITemplate PagerTemplate {
  671. get { return pagerTemplate; }
  672. set { pagerTemplate = value; RequireBinding (); }
  673. }
  674. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  675. [BrowsableAttribute (false)]
  676. public virtual FormViewRow Row {
  677. get {
  678. EnsureChildControls ();
  679. return itemRow;
  680. }
  681. }
  682. [WebCategoryAttribute ("Styles")]
  683. [PersistenceMode (PersistenceMode.InnerProperty)]
  684. [NotifyParentProperty (true)]
  685. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  686. [DefaultValue (null)]
  687. public TableItemStyle RowStyle {
  688. get {
  689. if (rowStyle == null) {
  690. rowStyle = new TableItemStyle ();
  691. if (IsTrackingViewState)
  692. rowStyle.TrackViewState();
  693. }
  694. return rowStyle;
  695. }
  696. }
  697. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  698. [BrowsableAttribute (false)]
  699. public object SelectedValue {
  700. get { return DataKey.Value; }
  701. }
  702. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  703. [BrowsableAttribute (false)]
  704. public virtual FormViewRow TopPagerRow {
  705. get {
  706. EnsureChildControls ();
  707. return topPagerRow;
  708. }
  709. }
  710. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  711. [BrowsableAttribute (false)]
  712. public virtual object DataItem {
  713. get {
  714. EnsureDataBound ();
  715. return dataItem;
  716. }
  717. }
  718. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  719. [BrowsableAttribute (false)]
  720. public int DataItemCount {
  721. get { return PageCount; }
  722. }
  723. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  724. [BrowsableAttribute (false)]
  725. public virtual int DataItemIndex {
  726. get { return PageIndex; }
  727. }
  728. int IDataItemContainer.DataItemIndex {
  729. get { return DataItemIndex; }
  730. }
  731. int IDataItemContainer.DisplayIndex {
  732. get { return PageIndex; }
  733. }
  734. public virtual bool IsBindableType (Type type)
  735. {
  736. return type.IsPrimitive || type == typeof (string) || type == typeof (DateTime) || type == typeof (Guid) || type == typeof (Decimal);
  737. }
  738. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  739. {
  740. DataSourceSelectArguments arg = new DataSourceSelectArguments ();
  741. DataSourceView view = GetData ();
  742. if (AllowPaging && view.CanPage) {
  743. arg.StartRowIndex = PageIndex;
  744. if (view.CanRetrieveTotalRowCount) {
  745. arg.RetrieveTotalRowCount = true;
  746. arg.MaximumRows = 1;
  747. }
  748. else {
  749. arg.MaximumRows = -1;
  750. }
  751. }
  752. return arg;
  753. }
  754. protected virtual FormViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
  755. {
  756. FormViewRow row = new FormViewRow (rowIndex, rowType, rowState);
  757. OnItemCreated (EventArgs.Empty);
  758. return row;
  759. }
  760. void RequireBinding ()
  761. {
  762. if (Initialized) {
  763. RequiresDataBinding = true;
  764. }
  765. }
  766. protected virtual Table CreateTable ()
  767. {
  768. return new ContainedTable (this);
  769. }
  770. protected override void EnsureDataBound ()
  771. {
  772. base.EnsureDataBound ();
  773. }
  774. protected override Style CreateControlStyle ()
  775. {
  776. TableStyle style = new TableStyle (ViewState);
  777. style.CellSpacing = 0;
  778. return style;
  779. }
  780. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  781. {
  782. PagedDataSource dataSource = new PagedDataSource ();
  783. dataSource.DataSource = data;
  784. dataSource.AllowPaging = AllowPaging;
  785. dataSource.PageSize = 1;
  786. dataSource.CurrentPageIndex = PageIndex;
  787. if (dataBinding && CurrentMode != FormViewMode.Insert) {
  788. DataSourceView view = GetData ();
  789. if (view != null && view.CanPage) {
  790. dataSource.AllowServerPaging = true;
  791. if (SelectArguments.RetrieveTotalRowCount)
  792. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  793. }
  794. }
  795. pageCount = dataSource.DataSourceCount;
  796. bool showPager = AllowPaging && (pageCount > 1);
  797. Controls.Clear ();
  798. table = CreateTable ();
  799. Controls.Add (table);
  800. // Gets the current data item
  801. IEnumerator e = dataSource.GetEnumerator ();
  802. dataItem = null;
  803. if (AllowPaging) {
  804. if (e.MoveNext ())
  805. dataItem = e.Current;
  806. }
  807. else
  808. for (int page = 0; e.MoveNext (); page++ )
  809. if (page == PageIndex)
  810. dataItem = e.Current;
  811. // Main table creation
  812. if (HeaderText.Length != 0 || headerTemplate != null) {
  813. headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
  814. InitializeRow (headerRow);
  815. table.Rows.Add (headerRow);
  816. }
  817. if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
  818. topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
  819. InitializePager (topPagerRow, dataSource);
  820. table.Rows.Add (topPagerRow);
  821. }
  822. if (pageCount > 0) {
  823. DataControlRowState rstate = GetRowState ();
  824. itemRow = CreateRow (0, DataControlRowType.DataRow, rstate);
  825. InitializeRow (itemRow);
  826. table.Rows.Add (itemRow);
  827. } else {
  828. switch (CurrentMode) {
  829. case FormViewMode.Edit:
  830. itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Edit);
  831. break;
  832. case FormViewMode.Insert:
  833. itemRow = CreateRow (-1, DataControlRowType.DataRow, DataControlRowState.Insert);
  834. break;
  835. default:
  836. itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  837. break;
  838. }
  839. table.Rows.Add (itemRow);
  840. InitializeRow (itemRow);
  841. }
  842. if (FooterText.Length != 0 || footerTemplate != null) {
  843. footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
  844. InitializeRow (footerRow);
  845. table.Rows.Add (footerRow);
  846. }
  847. if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
  848. bottomPagerRow = CreateRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
  849. InitializePager (bottomPagerRow, dataSource);
  850. table.Rows.Add (bottomPagerRow);
  851. }
  852. if (dataBinding)
  853. DataBind (false);
  854. return dataSource.DataSourceCount;
  855. }
  856. DataControlRowState GetRowState ()
  857. {
  858. DataControlRowState rstate = DataControlRowState.Normal;
  859. if (CurrentMode == FormViewMode.Edit) rstate |= DataControlRowState.Edit;
  860. else if (CurrentMode == FormViewMode.Insert) rstate |= DataControlRowState.Insert;
  861. return rstate;
  862. }
  863. protected virtual void InitializePager (FormViewRow row, PagedDataSource dataSource)
  864. {
  865. TableCell cell = new TableCell ();
  866. cell.ColumnSpan = 2;
  867. if (pagerTemplate != null)
  868. pagerTemplate.InstantiateIn (cell);
  869. else
  870. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  871. row.Cells.Add (cell);
  872. }
  873. protected virtual void InitializeRow (FormViewRow row)
  874. {
  875. TableCell cell = new TableCell ();
  876. if (row.RowType == DataControlRowType.DataRow)
  877. {
  878. if ((row.RowState & DataControlRowState.Edit) != 0) {
  879. if (editItemTemplate != null)
  880. editItemTemplate.InstantiateIn (cell);
  881. else
  882. row.Visible = false;
  883. } else if ((row.RowState & DataControlRowState.Insert) != 0) {
  884. if (insertItemTemplate != null)
  885. insertItemTemplate.InstantiateIn (cell);
  886. else
  887. row.Visible = false;
  888. }
  889. else if (itemTemplate != null)
  890. itemTemplate.InstantiateIn (cell);
  891. else
  892. row.Visible = false;
  893. }
  894. else if (row.RowType == DataControlRowType.EmptyDataRow)
  895. {
  896. if (emptyDataTemplate != null)
  897. emptyDataTemplate.InstantiateIn (cell);
  898. else
  899. cell.Text = EmptyDataText;
  900. }
  901. else if (row.RowType == DataControlRowType.Footer)
  902. {
  903. if (footerTemplate != null)
  904. footerTemplate.InstantiateIn (cell);
  905. else
  906. cell.Text = FooterText;
  907. }
  908. else if (row.RowType == DataControlRowType.Header)
  909. {
  910. if (headerTemplate != null)
  911. headerTemplate.InstantiateIn (cell);
  912. else
  913. cell.Text = HeaderText;
  914. }
  915. cell.ColumnSpan = 2;
  916. row.Cells.Add (cell);
  917. }
  918. void FillRowDataKey (object dataItem)
  919. {
  920. KeyTable.Clear ();
  921. if (cachedKeyProperties == null) {
  922. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
  923. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  924. for (int n=0; n<DataKeyNames.Length; n++) {
  925. PropertyDescriptor p = props.Find (DataKeyNames [n], true);
  926. if (p == null)
  927. throw new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
  928. cachedKeyProperties [n] = p;
  929. }
  930. }
  931. foreach (PropertyDescriptor p in cachedKeyProperties)
  932. KeyTable [p.Name] = p.GetValue (dataItem);
  933. }
  934. IOrderedDictionary GetRowValues (bool includePrimaryKey)
  935. {
  936. OrderedDictionary dic = new OrderedDictionary ();
  937. ExtractRowValues (dic, includePrimaryKey);
  938. return dic;
  939. }
  940. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeKeys)
  941. {
  942. if (Row == null)
  943. return;
  944. DataControlRowState rowState = Row.RowState;
  945. IBindableTemplate bt;
  946. if ((rowState & DataControlRowState.Insert) != 0)
  947. bt = insertItemTemplate as IBindableTemplate;
  948. else if ((rowState & DataControlRowState.Edit) != 0)
  949. bt = editItemTemplate as IBindableTemplate;
  950. else
  951. return;
  952. if (bt != null) {
  953. IOrderedDictionary values = bt.ExtractValues (Row.Cells [0]);
  954. foreach (DictionaryEntry e in values) {
  955. if (includeKeys || Array.IndexOf (DataKeyNames, e.Key) == -1)
  956. fieldValues [e.Key] = e.Value;
  957. }
  958. }
  959. }
  960. protected override HtmlTextWriterTag TagKey {
  961. get {
  962. return HtmlTextWriterTag.Table;
  963. }
  964. }
  965. public sealed override void DataBind ()
  966. {
  967. if (CurrentMode == FormViewMode.Insert) {
  968. RequiresDataBinding = false;
  969. PerformDataBinding (new object [] { null });
  970. return;
  971. }
  972. cachedKeyProperties = null;
  973. base.DataBind ();
  974. if (pageCount > 0) {
  975. if (CurrentMode == FormViewMode.Edit)
  976. oldEditValues = new DataKey (GetRowValues (true));
  977. FillRowDataKey (dataItem);
  978. key = new DataKey (KeyTable);
  979. }
  980. }
  981. protected internal override void PerformDataBinding (IEnumerable data)
  982. {
  983. base.PerformDataBinding (data);
  984. }
  985. protected internal virtual void PrepareControlHierarchy ()
  986. {
  987. if (table == null)
  988. return;
  989. table.Caption = Caption;
  990. table.CaptionAlign = CaptionAlign;
  991. foreach (FormViewRow row in table.Rows) {
  992. switch (row.RowType) {
  993. case DataControlRowType.Header:
  994. if (headerStyle != null && !headerStyle.IsEmpty)
  995. row.ControlStyle.CopyFrom (headerStyle);
  996. break;
  997. case DataControlRowType.Footer:
  998. if (footerStyle != null && !footerStyle.IsEmpty)
  999. row.ControlStyle.CopyFrom (footerStyle);
  1000. break;
  1001. case DataControlRowType.Pager:
  1002. if (pagerStyle != null && !pagerStyle.IsEmpty)
  1003. row.ControlStyle.CopyFrom (pagerStyle);
  1004. break;
  1005. case DataControlRowType.EmptyDataRow:
  1006. if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
  1007. row.ControlStyle.CopyFrom (emptyDataRowStyle);
  1008. break;
  1009. case DataControlRowType.DataRow:
  1010. if (rowStyle != null && !rowStyle.IsEmpty)
  1011. row.ControlStyle.CopyFrom (rowStyle);
  1012. if ((row.RowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
  1013. row.ControlStyle.CopyFrom (editRowStyle);
  1014. if ((row.RowState & DataControlRowState.Insert) != 0 && insertRowStyle != null && !insertRowStyle.IsEmpty)
  1015. row.ControlStyle.CopyFrom (insertRowStyle);
  1016. break;
  1017. default:
  1018. break;
  1019. }
  1020. }
  1021. }
  1022. protected internal override void OnInit (EventArgs e)
  1023. {
  1024. Page.RegisterRequiresControlState (this);
  1025. base.OnInit (e);
  1026. }
  1027. protected override bool OnBubbleEvent (object source, EventArgs e)
  1028. {
  1029. FormViewCommandEventArgs args = e as FormViewCommandEventArgs;
  1030. if (args != null) {
  1031. OnItemCommand (args);
  1032. ProcessEvent (args.CommandName, args.CommandArgument as string);
  1033. return true;
  1034. }
  1035. return base.OnBubbleEvent (source, e);
  1036. }
  1037. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  1038. {
  1039. RaisePostBackEvent (eventArgument);
  1040. }
  1041. protected virtual void RaisePostBackEvent (string eventArgument)
  1042. {
  1043. int i = eventArgument.IndexOf ('$');
  1044. if (i != -1)
  1045. ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
  1046. else
  1047. ProcessEvent (eventArgument, null);
  1048. }
  1049. void ProcessEvent (string eventName, string param)
  1050. {
  1051. switch (eventName)
  1052. {
  1053. case DataControlCommands.PageCommandName:
  1054. int newIndex = -1;
  1055. switch (param) {
  1056. case DataControlCommands.FirstPageCommandArgument:
  1057. newIndex = 0;
  1058. break;
  1059. case DataControlCommands.LastPageCommandArgument:
  1060. newIndex = PageCount - 1;
  1061. break;
  1062. case DataControlCommands.NextPageCommandArgument:
  1063. newIndex = PageIndex + 1;
  1064. break;
  1065. case DataControlCommands.PreviousPageCommandArgument:
  1066. newIndex = PageIndex - 1;
  1067. break;
  1068. default:
  1069. int paramIndex = 0;
  1070. int.TryParse (param, out paramIndex);
  1071. newIndex = paramIndex - 1;
  1072. break;
  1073. }
  1074. ShowPage (newIndex);
  1075. break;
  1076. case DataControlCommands.FirstPageCommandArgument:
  1077. ShowPage (0);
  1078. break;
  1079. case DataControlCommands.LastPageCommandArgument:
  1080. ShowPage (PageCount - 1);
  1081. break;
  1082. case DataControlCommands.NextPageCommandArgument:
  1083. if (PageIndex < PageCount - 1)
  1084. ShowPage (PageIndex + 1);
  1085. break;
  1086. case DataControlCommands.PreviousPageCommandArgument:
  1087. if (PageIndex > 0)
  1088. ShowPage (PageIndex - 1);
  1089. break;
  1090. case DataControlCommands.EditCommandName:
  1091. ProcessChangeMode (FormViewMode.Edit);
  1092. break;
  1093. case DataControlCommands.NewCommandName:
  1094. ProcessChangeMode (FormViewMode.Insert);
  1095. break;
  1096. case DataControlCommands.UpdateCommandName:
  1097. UpdateItem (param, true);
  1098. break;
  1099. case DataControlCommands.CancelCommandName:
  1100. CancelEdit ();
  1101. break;
  1102. case DataControlCommands.DeleteCommandName:
  1103. DeleteItem ();
  1104. break;
  1105. case DataControlCommands.InsertCommandName:
  1106. InsertItem (true);
  1107. break;
  1108. }
  1109. }
  1110. void ShowPage (int index)
  1111. {
  1112. FormViewPageEventArgs args = new FormViewPageEventArgs (index);
  1113. OnPageIndexChanging (args);
  1114. if (args.Cancel || !IsBoundUsingDataSourceID)
  1115. return;
  1116. int newIndex = args.NewPageIndex;
  1117. if (newIndex < 0 || newIndex >= PageCount)
  1118. return;
  1119. EndRowEdit (false);
  1120. PageIndex = newIndex;
  1121. OnPageIndexChanged (EventArgs.Empty);
  1122. }
  1123. public void ChangeMode (FormViewMode newMode)
  1124. {
  1125. CurrentMode = newMode;
  1126. RequireBinding ();
  1127. }
  1128. void ProcessChangeMode (FormViewMode newMode)
  1129. {
  1130. FormViewModeEventArgs args = new FormViewModeEventArgs (newMode, false);
  1131. OnModeChanging (args);
  1132. if (args.Cancel || !IsBoundUsingDataSourceID)
  1133. return;
  1134. ChangeMode (args.NewMode);
  1135. OnModeChanged (EventArgs.Empty);
  1136. }
  1137. void CancelEdit ()
  1138. {
  1139. FormViewModeEventArgs args = new FormViewModeEventArgs (FormViewMode.ReadOnly, true);
  1140. OnModeChanging (args);
  1141. if (args.Cancel || !IsBoundUsingDataSourceID)
  1142. return;
  1143. EndRowEdit ();
  1144. }
  1145. public virtual void UpdateItem (bool causesValidation)
  1146. {
  1147. UpdateItem (null, causesValidation);
  1148. }
  1149. void UpdateItem (string param, bool causesValidation)
  1150. {
  1151. if (causesValidation && Page != null)
  1152. Page.Validate ();
  1153. if (currentMode != FormViewMode.Edit) throw new HttpException ("Must be in Edit mode");
  1154. currentEditOldValues = OldEditValues.Values;
  1155. currentEditRowKeys = DataKey.Values;
  1156. currentEditNewValues = GetRowValues (false);
  1157. FormViewUpdateEventArgs args = new FormViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1158. OnItemUpdating (args);
  1159. if (args.Cancel || !IsBoundUsingDataSourceID)
  1160. return;
  1161. DataSourceView view = GetData ();
  1162. if (view == null)
  1163. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1164. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1165. }
  1166. bool UpdateCallback (int recordsAffected, Exception exception)
  1167. {
  1168. FormViewUpdatedEventArgs dargs = new FormViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1169. OnItemUpdated (dargs);
  1170. if (!dargs.KeepInEditMode)
  1171. EndRowEdit ();
  1172. return dargs.ExceptionHandled;
  1173. }
  1174. public virtual void InsertItem (bool causesValidation)
  1175. {
  1176. InsertItem (null, causesValidation);
  1177. }
  1178. void InsertItem (string param, bool causesValidation)
  1179. {
  1180. if (causesValidation && Page != null)
  1181. Page.Validate ();
  1182. if (currentMode != FormViewMode.Insert) throw new HttpException ("Must be in Insert mode");
  1183. currentEditNewValues = GetRowValues (true);
  1184. FormViewInsertEventArgs args = new FormViewInsertEventArgs (param, currentEditNewValues);
  1185. OnItemInserting (args);
  1186. if (args.Cancel || !IsBoundUsingDataSourceID)
  1187. return;
  1188. DataSourceView view = GetData ();
  1189. if (view == null)
  1190. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1191. view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
  1192. }
  1193. bool InsertCallback (int recordsAffected, Exception exception)
  1194. {
  1195. FormViewInsertedEventArgs dargs = new FormViewInsertedEventArgs (recordsAffected, exception, currentEditNewValues);
  1196. OnItemInserted (dargs);
  1197. if (!dargs.KeepInInsertMode)
  1198. EndRowEdit ();
  1199. return dargs.ExceptionHandled;
  1200. }
  1201. public virtual void DeleteItem ()
  1202. {
  1203. currentEditRowKeys = DataKey.Values;
  1204. currentEditNewValues = GetRowValues (true);
  1205. FormViewDeleteEventArgs args = new FormViewDeleteEventArgs (PageIndex, currentEditRowKeys, currentEditNewValues);
  1206. OnItemDeleting (args);
  1207. if (args.Cancel || !IsBoundUsingDataSourceID)
  1208. return;
  1209. if (PageIndex > 0 && PageIndex == PageCount - 1)
  1210. PageIndex--;
  1211. RequireBinding ();
  1212. DataSourceView view = GetData ();
  1213. if (view != null)
  1214. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1215. else {
  1216. FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1217. OnItemDeleted (dargs);
  1218. }
  1219. }
  1220. bool DeleteCallback (int recordsAffected, Exception exception)
  1221. {
  1222. FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1223. OnItemDeleted (dargs);
  1224. return dargs.ExceptionHandled;
  1225. }
  1226. void EndRowEdit ()
  1227. {
  1228. EndRowEdit (true);
  1229. }
  1230. void EndRowEdit (bool switchToDefaultMode)
  1231. {
  1232. if (switchToDefaultMode)
  1233. ChangeMode (DefaultMode);
  1234. oldEditValues = new DataKey (new OrderedDictionary ());
  1235. currentEditRowKeys = null;
  1236. currentEditOldValues = null;
  1237. currentEditNewValues = null;
  1238. RequireBinding ();
  1239. }
  1240. protected internal override void LoadControlState (object ob)
  1241. {
  1242. if (ob == null) return;
  1243. object[] state = (object[]) ob;
  1244. base.LoadControlState (state[0]);
  1245. pageIndex = (int) state[1];
  1246. pageCount = (int) state[2];
  1247. CurrentMode = (FormViewMode) state[3];
  1248. defaultMode = (FormViewMode) state[4];
  1249. dataKeyNames = (string[]) state[5];
  1250. if (state [6] != null)
  1251. ((IStateManager) DataKey).LoadViewState (state [6]);
  1252. if (state [7] != null)
  1253. ((IStateManager) OldEditValues).LoadViewState (state [7]);
  1254. }
  1255. protected internal override object SaveControlState ()
  1256. {
  1257. object bstate = base.SaveControlState ();
  1258. return new object [] {
  1259. bstate,
  1260. pageIndex,
  1261. pageCount,
  1262. CurrentMode,
  1263. defaultMode,
  1264. dataKeyNames,
  1265. (key == null ? null : ((IStateManager)key).SaveViewState()),
  1266. (oldEditValues == null ? null : ((IStateManager) oldEditValues).SaveViewState ())
  1267. };
  1268. }
  1269. protected override void TrackViewState()
  1270. {
  1271. base.TrackViewState();
  1272. if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
  1273. if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
  1274. if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
  1275. if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
  1276. if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
  1277. if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
  1278. if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
  1279. if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
  1280. }
  1281. protected override object SaveViewState()
  1282. {
  1283. object[] states = new object [10];
  1284. states[0] = base.SaveViewState();
  1285. states[1] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
  1286. states[2] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
  1287. states[3] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
  1288. states[4] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
  1289. states[5] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
  1290. states[6] = (insertRowStyle == null ? null : ((IStateManager)insertRowStyle).SaveViewState());
  1291. states[7] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
  1292. states[8] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
  1293. for (int i = states.Length - 1; i >= 0; i--) {
  1294. if (states [i] != null)
  1295. return states;
  1296. }
  1297. return null;
  1298. }
  1299. protected override void LoadViewState (object savedState)
  1300. {
  1301. if (savedState == null) {
  1302. base.LoadViewState (null);
  1303. return;
  1304. }
  1305. object [] states = (object []) savedState;
  1306. base.LoadViewState (states[0]);
  1307. if (states[1] != null) ((IStateManager)PagerSettings).LoadViewState (states[1]);
  1308. if (states[2] != null) ((IStateManager)FooterStyle).LoadViewState (states[2]);
  1309. if (states[3] != null) ((IStateManager)HeaderStyle).LoadViewState (states[3]);
  1310. if (states[4] != null) ((IStateManager)PagerStyle).LoadViewState (states[4]);
  1311. if (states[5] != null) ((IStateManager)RowStyle).LoadViewState (states[5]);
  1312. if (states[6] != null) ((IStateManager)InsertRowStyle).LoadViewState (states[6]);
  1313. if (states[7] != null) ((IStateManager)EditRowStyle).LoadViewState (states[7]);
  1314. if (states[8] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[8]);
  1315. }
  1316. protected internal override void Render (HtmlTextWriter writer)
  1317. {
  1318. PrepareControlHierarchy ();
  1319. if (table == null)
  1320. return;
  1321. table.Render (writer);
  1322. }
  1323. PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
  1324. {
  1325. if (control == null)
  1326. throw new ArgumentNullException ("control");
  1327. if (control.CausesValidation)
  1328. 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.");
  1329. PostBackOptions options = new PostBackOptions (this);
  1330. options.Argument = control.CommandName + "$" + control.CommandArgument;
  1331. options.RequiresJavaScriptProtocol = true;
  1332. return options;
  1333. }
  1334. }
  1335. }
  1336. #endif