FormView.cs 43 KB

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