FormView.cs 43 KB

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