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