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