FormView.cs 42 KB

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