FormView.cs 46 KB

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