FormView.cs 44 KB

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