New tests.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / GridView.cs
1 //
2 // System.Web.UI.WebControls.GridView.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         [SupportsEventValidation]
44         [DesignerAttribute ("System.Web.UI.Design.WebControls.GridViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45         [ControlValuePropertyAttribute ("SelectedValue")]
46         [DefaultEventAttribute ("SelectedIndexChanged")]
47         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
49         public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IPostBackEventHandler, IPostBackContainer, IPersistedSelector
50         {
51                 Table table;
52                 GridViewRowCollection rows;
53                 GridViewRow bottomPagerRow;
54                 GridViewRow topPagerRow;
55                 
56                 IOrderedDictionary currentEditRowKeys;
57                 IOrderedDictionary currentEditNewValues;
58                 IOrderedDictionary currentEditOldValues;
59                 
60                 ITemplate pagerTemplate;
61                 ITemplate emptyDataTemplate;
62                 
63                 PropertyDescriptor[] cachedKeyProperties;
64                         
65                 // View state
66                 DataControlFieldCollection columns;
67                 PagerSettings pagerSettings;
68                 
69                 TableItemStyle alternatingRowStyle;
70                 TableItemStyle editRowStyle;
71                 TableItemStyle emptyDataRowStyle;
72                 TableItemStyle footerStyle;
73                 TableItemStyle headerStyle;
74                 TableItemStyle pagerStyle;
75                 TableItemStyle rowStyle;
76                 TableItemStyle selectedRowStyle;
77                 ArrayList _dataKeyArrayList;
78                 DataKeyArray keys;
79                 DataKey oldEditValues;
80                 AutoGeneratedFieldProperties[] autoFieldProperties;
81                 string [] dataKeyNames = null;
82                 readonly string[] emptyKeys = new string[0];
83                 IEnumerator _dataEnumerator;
84                 
85                 static readonly object PageIndexChangedEvent = new object();
86                 static readonly object PageIndexChangingEvent = new object();
87                 static readonly object RowCancelingEditEvent = new object();
88                 static readonly object RowCommandEvent = new object();
89                 static readonly object RowCreatedEvent = new object();
90                 static readonly object RowDataBoundEvent = new object();
91                 static readonly object RowDeletedEvent = new object();
92                 static readonly object RowDeletingEvent = new object();
93                 static readonly object RowEditingEvent = new object();
94                 static readonly object RowUpdatedEvent = new object();
95                 static readonly object RowUpdatingEvent = new object();
96                 static readonly object SelectedIndexChangedEvent = new object();
97                 static readonly object SelectedIndexChangingEvent = new object();
98                 static readonly object SortedEvent = new object();
99                 static readonly object SortingEvent = new object();
100                 
101                 // Control state
102                 int pageIndex;
103                 int selectedIndex = -1;
104                 int editIndex = -1;
105                 SortDirection sortDirection = SortDirection.Ascending;
106                 string sortExpression;
107                 
108                 public GridView ()
109                 {
110                 }
111                 
112                 public event EventHandler PageIndexChanged {
113                         add { Events.AddHandler (PageIndexChangedEvent, value); }
114                         remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
115                 }
116                 
117                 public event GridViewPageEventHandler PageIndexChanging {
118                         add { Events.AddHandler (PageIndexChangingEvent, value); }
119                         remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
120                 }
121                 
122                 public event GridViewCancelEditEventHandler RowCancelingEdit {
123                         add { Events.AddHandler (RowCancelingEditEvent, value); }
124                         remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
125                 }
126                 
127                 public event GridViewCommandEventHandler RowCommand {
128                         add { Events.AddHandler (RowCommandEvent, value); }
129                         remove { Events.RemoveHandler (RowCommandEvent, value); }
130                 }
131                 
132                 public event GridViewRowEventHandler RowCreated {
133                         add { Events.AddHandler (RowCreatedEvent, value); }
134                         remove { Events.RemoveHandler (RowCreatedEvent, value); }
135                 }
136                 
137                 public event GridViewRowEventHandler RowDataBound {
138                         add { Events.AddHandler (RowDataBoundEvent, value); }
139                         remove { Events.RemoveHandler (RowDataBoundEvent, value); }
140                 }
141                 
142                 public event GridViewDeletedEventHandler RowDeleted {
143                         add { Events.AddHandler (RowDeletedEvent, value); }
144                         remove { Events.RemoveHandler (RowDeletedEvent, value); }
145                 }
146                 
147                 public event GridViewDeleteEventHandler RowDeleting {
148                         add { Events.AddHandler (RowDeletingEvent, value); }
149                         remove { Events.RemoveHandler (RowDeletingEvent, value); }
150                 }
151                 
152                 public event GridViewEditEventHandler RowEditing {
153                         add { Events.AddHandler (RowEditingEvent, value); }
154                         remove { Events.RemoveHandler (RowEditingEvent, value); }
155                 }
156                 
157                 public event GridViewUpdatedEventHandler RowUpdated {
158                         add { Events.AddHandler (RowUpdatedEvent, value); }
159                         remove { Events.RemoveHandler (RowUpdatedEvent, value); }
160                 }
161                 
162                 public event GridViewUpdateEventHandler RowUpdating {
163                         add { Events.AddHandler (RowUpdatingEvent, value); }
164                         remove { Events.RemoveHandler (RowUpdatingEvent, value); }
165                 }
166                 
167                 public event EventHandler SelectedIndexChanged {
168                         add { Events.AddHandler (SelectedIndexChangedEvent, value); }
169                         remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
170                 }
171                 
172                 public event GridViewSelectEventHandler SelectedIndexChanging {
173                         add { Events.AddHandler (SelectedIndexChangingEvent, value); }
174                         remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
175                 }
176                 
177                 public event EventHandler Sorted {
178                         add { Events.AddHandler (SortedEvent, value); }
179                         remove { Events.RemoveHandler (SortedEvent, value); }
180                 }
181                 
182                 public event GridViewSortEventHandler Sorting {
183                         add { Events.AddHandler (SortingEvent, value); }
184                         remove { Events.RemoveHandler (SortingEvent, value); }
185                 }
186                 
187                 protected virtual void OnPageIndexChanged (EventArgs e)
188                 {
189                         if (Events != null) {
190                                 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
191                                 if (eh != null) eh (this, e);
192                         }
193                 }
194                 
195                 protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
196                 {
197                         if (Events != null) {
198                                 GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
199                                 if (eh != null) {
200                                         eh (this, e);
201                                         return;
202                                 }
203                         }
204                         if (!IsBoundUsingDataSourceID)
205                                 throw new HttpException (String.Format ("The GridView '{0}' fired event PageIndexChanging which wasn't handled.", ID));
206                 }
207                 
208                 protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
209                 {
210                         if (Events != null) {
211                                 GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
212                                 if (eh != null) {
213                                         eh (this, e);
214                                         return;
215                                 }
216                         }
217                         if (!IsBoundUsingDataSourceID)
218                                 throw new HttpException (String.Format ("The GridView '{0}' fired event RowCancelingEdit which wasn't handled.", ID));
219                 }
220                 
221                 protected virtual void OnRowCommand (GridViewCommandEventArgs e)
222                 {
223                         if (Events != null) {
224                                 GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
225                                 if (eh != null) eh (this, e);
226                         }
227                 }
228                 
229                 protected virtual void OnRowCreated (GridViewRowEventArgs e)
230                 {
231                         if (Events != null) {
232                                 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
233                                 if (eh != null) eh (this, e);
234                         }
235                 }
236                 
237                 protected virtual void OnRowDataBound (GridViewRowEventArgs e)
238                 {
239                         if (Events != null) {
240                                 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
241                                 if (eh != null) eh (this, e);
242                         }
243                 }
244                 
245                 protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
246                 {
247                         if (Events != null) {
248                                 GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
249                                 if (eh != null) eh (this, e);
250                         }
251                 }
252                 
253                 protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
254                 {
255                         if (Events != null) {
256                                 GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
257                                 if (eh != null) {
258                                         eh (this, e);
259                                         return;
260                                 }
261                         }
262                         if (!IsBoundUsingDataSourceID)
263                                 throw new HttpException (String.Format ("The GridView '{0}' fired event RowDeleting which wasn't handled.", ID));
264                 }
265                 
266                 protected virtual void OnRowEditing (GridViewEditEventArgs e)
267                 {
268                         if (Events != null) {
269                                 GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
270                                 if (eh != null) {
271                                         eh (this, e);
272                                         return;
273                                 }
274                         }
275                         if (!IsBoundUsingDataSourceID)
276                                 throw new HttpException (String.Format ("The GridView '{0}' fired event RowEditing which wasn't handled.", ID));
277                 }
278                 
279                 protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
280                 {
281                         if (Events != null) {
282                                 GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
283                                 if (eh != null) eh (this, e);
284                         }
285                 }
286                 
287                 protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
288                 {
289                         if (Events != null) {
290                                 GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
291                                 if (eh != null) {
292                                         eh (this, e);
293                                         return;
294                                 }
295                         }
296                         if (!IsBoundUsingDataSourceID)
297                                 throw new HttpException (String.Format ("The GridView '{0}' fired event RowUpdating which wasn't handled.", ID));
298                 }
299                 
300                 protected virtual void OnSelectedIndexChanged (EventArgs e)
301                 {
302                         if (Events != null) {
303                                 EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
304                                 if (eh != null) eh (this, e);
305                         }
306                 }
307                 
308                 protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
309                 {
310                         if (Events != null) {
311                                 GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
312                                 if (eh != null) eh (this, e);
313                         }
314                 }
315                 
316                 protected virtual void OnSorted (EventArgs e)
317                 {
318                         if (Events != null) {
319                                 EventHandler eh = (EventHandler) Events [SortedEvent];
320                                 if (eh != null) eh (this, e);
321                         }
322                 }
323                 
324                 protected virtual void OnSorting (GridViewSortEventArgs e)
325                 {
326                         if (Events != null) {
327                                 GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
328                                 if (eh != null) {
329                                         eh (this, e);
330                                         return;
331                                 }
332                         }
333                         if (!IsBoundUsingDataSourceID)
334                                 throw new HttpException (String.Format ("The GridView '{0}' fired event Sorting which wasn't handled.", ID));
335                 }
336                 
337                 
338                 [WebCategoryAttribute ("Paging")]
339                 [DefaultValueAttribute (false)]
340                 public virtual bool AllowPaging {
341                         get {
342                                 object ob = ViewState ["AllowPaging"];
343                                 if (ob != null) return (bool) ob;
344                                 return false;
345                         }
346                         set {
347                                 if (value == AllowPaging)
348                                         return;
349                                 ViewState ["AllowPaging"] = value;
350                                 RequireBinding ();
351                         }
352                 }
353                 
354                 [WebCategoryAttribute ("Behavior")]
355                 [DefaultValueAttribute (false)]
356                 public virtual bool AllowSorting {
357                         get {
358                                 object ob = ViewState ["AllowSorting"];
359                                 if (ob != null) return (bool) ob;
360                                 return false;
361                         }
362                         set {
363                                 if (value == AllowSorting)
364                                         return;
365                                 ViewState ["AllowSorting"] = value;
366                                 RequireBinding ();
367                         }
368                 }
369                 
370                 [WebCategoryAttribute ("Styles")]
371                 [PersistenceMode (PersistenceMode.InnerProperty)]
372                 [NotifyParentProperty (true)]
373                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
374                 public TableItemStyle AlternatingRowStyle {
375                         get {
376                                 if (alternatingRowStyle == null) {
377                                         alternatingRowStyle = new TableItemStyle ();
378                                         if (IsTrackingViewState)
379                                                 alternatingRowStyle.TrackViewState();
380                                 }
381                                 return alternatingRowStyle;
382                         }
383                 }
384
385                 [WebCategoryAttribute ("Behavior")]
386                 [DefaultValueAttribute (false)]
387                 public virtual bool AutoGenerateEditButton {
388                         get {
389                                 object ob = ViewState ["AutoGenerateEditButton"];
390                                 if (ob != null) return (bool) ob;
391                                 return false;
392                         }
393                         set {
394                                 if (value == AutoGenerateEditButton)
395                                         return;
396                                 ViewState ["AutoGenerateEditButton"] = value;
397                                 RequireBinding ();
398                         }
399                 }
400
401                 [WebCategoryAttribute ("Behavior")]
402                 [DefaultValueAttribute (false)]
403                 public virtual bool AutoGenerateDeleteButton {
404                         get {
405                                 object ob = ViewState ["AutoGenerateDeleteButton"];
406                                 if (ob != null) return (bool) ob;
407                                 return false;
408                         }
409                         set {
410                                 if (value == AutoGenerateDeleteButton)
411                                         return;
412                                 ViewState ["AutoGenerateDeleteButton"] = value;
413                                 RequireBinding ();
414                         }
415                 }
416
417                 [WebCategoryAttribute ("Behavior")]
418                 [DefaultValueAttribute (false)]
419                 public virtual bool AutoGenerateSelectButton {
420                         get {
421                                 object ob = ViewState ["AutoGenerateSelectButton"];
422                                 if (ob != null) return (bool) ob;
423                                 return false;
424                         }
425                         set {
426                                 if (value == AutoGenerateSelectButton)
427                                         return;
428                                 ViewState ["AutoGenerateSelectButton"] = value;
429                                 RequireBinding ();
430                         }
431                 }
432
433                 [WebCategoryAttribute ("Behavior")]
434                 [DefaultValueAttribute (true)]
435                 public virtual bool AutoGenerateColumns {
436                         get {
437                                 object ob = ViewState ["AutoGenerateColumns"];
438                                 if (ob != null) return (bool) ob;
439                                 return true;
440                         }
441                         set {
442                                 if (value == AutoGenerateColumns)
443                                         return;
444                                 ViewState ["AutoGenerateColumns"] = value;
445                                 RequireBinding ();
446                         }
447                 }
448                 
449                 [UrlPropertyAttribute]
450                 [WebCategoryAttribute ("Appearance")]
451                 [DefaultValueAttribute ("")]
452                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
453                 public virtual string BackImageUrl {
454                         get {
455                                 if (ControlStyleCreated)
456                                         return ((TableStyle) ControlStyle).BackImageUrl;
457                                 return String.Empty;
458                         }
459                         set {
460                                 ((TableStyle) ControlStyle).BackImageUrl = value;
461                         }
462                 }
463
464                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
465                 [BrowsableAttribute (false)]
466                 public virtual GridViewRow BottomPagerRow {
467                         get {
468                                 EnsureDataBound ();
469                                 return bottomPagerRow;
470                         }
471                 }
472         
473                 [WebCategoryAttribute ("Accessibility")]
474                 [DefaultValueAttribute ("")]
475                 [LocalizableAttribute (true)]
476                 public virtual string Caption {
477                         get {
478                                 object ob = ViewState ["Caption"];
479                                 if (ob != null) return (string) ob;
480                                 return string.Empty;
481                         }
482                         set {
483                                 ViewState ["Caption"] = value;
484                         }
485                 }
486                 
487                 [WebCategoryAttribute ("Accessibility")]
488                 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
489                 public virtual TableCaptionAlign CaptionAlign
490                 {
491                         get {
492                                 object o = ViewState ["CaptionAlign"];
493                                 if(o != null) return (TableCaptionAlign) o;
494                                 return TableCaptionAlign.NotSet;
495                         }
496                         set {
497                                 ViewState ["CaptionAlign"] = value;
498                         }
499                 }
500
501                 [WebCategoryAttribute ("Layout")]
502                 [DefaultValueAttribute (-1)]
503                 public virtual int CellPadding
504                 {
505                         get {
506                                 if (ControlStyleCreated)
507                                         return ((TableStyle) ControlStyle).CellPadding;
508                                 return -1;
509                         }
510                         set {
511                                 ((TableStyle) ControlStyle).CellPadding = value;
512                         }
513                 }
514
515                 [WebCategoryAttribute ("Layout")]
516                 [DefaultValueAttribute (0)]
517                 public virtual int CellSpacing
518                 {
519                         get {
520                                 if (ControlStyleCreated)
521                                         return ((TableStyle) ControlStyle).CellSpacing;
522                                 return 0;
523                         }
524                         set {
525                                 ((TableStyle) ControlStyle).CellSpacing = value;
526                         }
527                 }
528                 
529                 [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
530                 [MergablePropertyAttribute (false)]
531                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
532                 [DefaultValueAttribute (null)]
533                 [WebCategoryAttribute ("Misc")]
534                 public virtual DataControlFieldCollection Columns {
535                         get {
536                                 if (columns == null) {
537                                         columns = new DataControlFieldCollection ();
538                                         columns.FieldsChanged += new EventHandler (OnFieldsChanged);
539                                         if (IsTrackingViewState)
540                                                 ((IStateManager)columns).TrackViewState ();
541                                 }
542                                 return columns;
543                         }
544                 }
545
546                 [BrowsableAttribute(false)]
547                 public IAutoFieldGenerator ColumnsGenerator {
548                         get;
549                         set;
550                 }
551
552                 [DefaultValueAttribute (null)]
553                 [WebCategoryAttribute ("Data")]
554                 [TypeConverter (typeof(StringArrayConverter))]
555                 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
556                 public virtual string[] DataKeyNames
557                 {
558                         get {
559                                 if (dataKeyNames != null)
560                                         return dataKeyNames;
561                                 return emptyKeys;
562                         }
563                         set {
564                                 dataKeyNames = value;
565                                 RequireBinding ();
566                         }
567                 }
568
569                 ArrayList DataKeyArrayList {
570                         get {
571                                 if (_dataKeyArrayList == null) {
572                                         _dataKeyArrayList = new ArrayList ();
573                                 }
574                                 return _dataKeyArrayList;
575                         }
576                 }
577                 
578                 [BrowsableAttribute (false)]
579                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
580                 public virtual DataKeyArray DataKeys {
581                         get {
582                                 if (keys == null) {
583                                         keys = new DataKeyArray (DataKeyArrayList);
584                                         if (IsTrackingViewState)
585                                                 ((IStateManager) keys).TrackViewState ();
586                                 }
587                                 return keys;
588                         }
589                 }
590
591                 DataKey OldEditValues {
592                         get {
593                                 if (oldEditValues == null) {
594                                         oldEditValues = new DataKey (new OrderedDictionary ());
595                                 }
596                                 return oldEditValues;
597                         }
598                 }
599
600                 [WebCategoryAttribute ("Misc")]
601                 [DefaultValueAttribute (-1)]
602                 public virtual int EditIndex {
603                         get {
604                                 return editIndex;
605                         }
606                         set {
607                                 if (value == editIndex)
608                                         return;
609                                 editIndex = value;
610                                 RequireBinding ();
611                         }
612                 }
613         
614                 [WebCategoryAttribute ("Styles")]
615                 [PersistenceMode (PersistenceMode.InnerProperty)]
616                 [NotifyParentProperty (true)]
617                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
618                 public TableItemStyle EditRowStyle {
619                         get {
620                                 if (editRowStyle == null) {
621                                         editRowStyle = new TableItemStyle ();
622                                         if (IsTrackingViewState)
623                                                 editRowStyle.TrackViewState();
624                                 }
625                                 return editRowStyle;
626                         }
627                 }
628                 
629                 [WebCategoryAttribute ("Styles")]
630                 [PersistenceMode (PersistenceMode.InnerProperty)]
631                 [NotifyParentProperty (true)]
632                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
633                 public TableItemStyle EmptyDataRowStyle {
634                         get {
635                                 if (emptyDataRowStyle == null) {
636                                         emptyDataRowStyle = new TableItemStyle ();
637                                         if (IsTrackingViewState)
638                                                 emptyDataRowStyle.TrackViewState();
639                                 }
640                                 return emptyDataRowStyle;
641                         }
642                 }
643                 
644                 [DefaultValue (null)]
645                 [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
646                 [PersistenceMode (PersistenceMode.InnerProperty)]
647                 [Browsable (false)]
648                 public virtual ITemplate EmptyDataTemplate {
649                         get { return emptyDataTemplate; }
650                         set { emptyDataTemplate = value; }
651                 }
652                 
653                 [LocalizableAttribute (true)]
654                 [WebCategoryAttribute ("Appearance")]
655                 [DefaultValueAttribute ("")]
656                 public virtual string EmptyDataText {
657                         get {
658                                 object ob = ViewState ["EmptyDataText"];
659                                 if (ob != null) return (string) ob;
660                                 return string.Empty;
661                         }
662                         set {
663                                 if (value == EmptyDataText)
664                                         return;
665                                 ViewState ["EmptyDataText"] = value;
666                                 RequireBinding ();
667                         }
668                 }
669         
670                 [WebCategoryAttribute ("Behavior")]
671                 [DefaultValueAttribute (false)]
672                 public virtual bool EnableSortingAndPagingCallbacks {
673                         get {
674                                 object ob = ViewState ["EnableSortingAndPagingCallbacks"];
675                                 if (ob != null) return (bool) ob;
676                                 return false;
677                         }
678                         set {
679                                 if (value == EnableSortingAndPagingCallbacks)
680                                         return;
681                                 ViewState ["EnableSortingAndPagingCallbacks"] = value;
682                                 RequireBinding ();
683                         }
684                 }
685         
686                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
687                 [BrowsableAttribute (false)]
688                 public virtual GridViewRow FooterRow {
689                         get {
690                                 if (table != null) {
691                                         for (int index = table.Rows.Count - 1; index >= 0; index--) {
692                                                 GridViewRow row = (GridViewRow) table.Rows [index];
693                                                 switch (row.RowType) {
694                                                 case DataControlRowType.Separator:
695                                                 case DataControlRowType.Pager:
696                                                         continue;
697                                                 case DataControlRowType.Footer:
698                                                         return row;
699                                                 default:
700                                                         break;
701                                                 }
702                                         }
703                                 }
704                                 return null;
705                         }
706                 }
707         
708                 [WebCategoryAttribute ("Styles")]
709                 [PersistenceMode (PersistenceMode.InnerProperty)]
710                 [NotifyParentProperty (true)]
711                 [DefaultValue (null)]
712                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
713                 public TableItemStyle FooterStyle {
714                         get {
715                                 if (footerStyle == null) {
716                                         footerStyle = new TableItemStyle ();
717                                         if (IsTrackingViewState)
718                                                 footerStyle.TrackViewState();
719                                 }
720                                 return footerStyle;
721                         }
722                 }
723                 
724                 [WebCategoryAttribute ("Appearance")]
725                 [DefaultValueAttribute (GridLines.Both)]
726                 public virtual GridLines GridLines {
727                         get {
728                                 if (ControlStyleCreated)
729                                         return ((TableStyle) ControlStyle).GridLines;
730                                 return GridLines.Both;
731                         }
732                         set {
733                                 ((TableStyle) ControlStyle).GridLines = value;
734                         }
735                 }
736
737                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
738                 [BrowsableAttribute (false)]
739                 public virtual GridViewRow HeaderRow {
740                         get {
741                                 if (table != null) {
742                                         for (int index = 0, total = table.Rows.Count; index < total; index++) {
743                                                 GridViewRow row = (GridViewRow) table.Rows [index];
744                                                 switch (row.RowType) {
745                                                 case DataControlRowType.Separator:
746                                                 case DataControlRowType.Pager:
747                                                         continue;
748                                                 case DataControlRowType.Header:
749                                                         return row;
750                                                 default:
751                                                         break;
752                                                 }
753                                         }
754                                 }
755                                 return null;
756                         }
757                 }
758         
759                 [WebCategoryAttribute ("Styles")]
760                 [PersistenceMode (PersistenceMode.InnerProperty)]
761                 [NotifyParentProperty (true)]
762                 [DefaultValue (null)]
763                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
764                 public TableItemStyle HeaderStyle {
765                         get {
766                                 if (headerStyle == null) {
767                                         headerStyle = new TableItemStyle ();
768                                         if (IsTrackingViewState)
769                                                 headerStyle.TrackViewState();
770                                 }
771                                 return headerStyle;
772                         }
773                 }
774                 
775                 [Category ("Layout")]
776                 [DefaultValueAttribute (HorizontalAlign.NotSet)]
777                 public virtual HorizontalAlign HorizontalAlign {
778                         get {
779                                 if (ControlStyleCreated)
780                                         return ((TableStyle) ControlStyle).HorizontalAlign;
781                                 return HorizontalAlign.NotSet;
782                         }
783                         set {
784                                 ((TableStyle) ControlStyle).HorizontalAlign = value;
785                         }
786                 }
787
788                 [BrowsableAttribute (false)]
789                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
790                 public virtual int PageCount {
791                         get {
792                                 return ViewState.GetInt ("PageCount", 0);
793                         }
794                         private set {
795                                 ViewState ["PageCount"] = value;
796                         }
797                 }
798
799                 [WebCategoryAttribute ("Paging")]
800                 [BrowsableAttribute (true)]
801                 [DefaultValueAttribute (0)]
802                 public virtual int PageIndex {
803                         get {
804                                 return pageIndex;
805                         }
806                         set {
807                                 if (value == pageIndex)
808                                         return;
809                                 pageIndex = value;
810                                 RequireBinding ();
811                         }
812                 }
813         
814                 [DefaultValueAttribute (10)]
815                 [WebCategoryAttribute ("Paging")]
816                 public virtual int PageSize {
817                         get {
818                                 object ob = ViewState ["PageSize"];
819                                 if (ob != null) return (int) ob;
820                                 return 10;
821                         }
822                         set {
823                                 if (value == PageSize)
824                                         return;
825                                 ViewState ["PageSize"] = value;
826                                 RequireBinding ();
827                         }
828                 }
829         
830                 [WebCategoryAttribute ("Paging")]
831                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
832                 [NotifyParentPropertyAttribute (true)]
833                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
834                 public virtual PagerSettings PagerSettings {
835                         get {
836                                 if (pagerSettings == null) {
837                                         pagerSettings = new PagerSettings (this);
838                                         if (IsTrackingViewState)
839                                                 ((IStateManager)pagerSettings).TrackViewState ();
840                                 }
841                                 return pagerSettings;
842                         }
843                 }
844         
845                 [WebCategoryAttribute ("Styles")]
846                 [PersistenceMode (PersistenceMode.InnerProperty)]
847                 [NotifyParentProperty (true)]
848                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
849                 public TableItemStyle PagerStyle {
850                         get {
851                                 if (pagerStyle == null) {
852                                         pagerStyle = new TableItemStyle ();
853                                         if (IsTrackingViewState)
854                                                 pagerStyle.TrackViewState();
855                                 }
856                                 return pagerStyle;
857                         }
858                 }
859                 
860                 
861                 [DefaultValue (null)]
862                 /* DataControlPagerCell isnt specified in the docs */
863                 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
864                 [PersistenceMode (PersistenceMode.InnerProperty)]
865                 [Browsable (false)]
866                 public virtual ITemplate PagerTemplate {
867                         get { return pagerTemplate; }
868                         set { pagerTemplate = value; }
869                 }
870                 
871                 [DefaultValueAttribute ("")]
872                 [WebCategoryAttribute ("Accessibility")]
873                 //              [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
874                 public virtual string RowHeaderColumn {
875                         get {
876                                 object ob = ViewState ["RowHeaderColumn"];
877                                 if (ob != null) return (string) ob;
878                                 return string.Empty;
879                         }
880                         set {
881                                 if (value == RowHeaderColumn)
882                                         return;
883                                 ViewState ["RowHeaderColumn"] = value;
884                                 RequireBinding ();
885                         }
886                 }
887                 
888                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
889                 [BrowsableAttribute (false)]
890                 public virtual GridViewRowCollection Rows {
891                         get {
892                                 EnsureChildControls ();
893                                 if (rows == null)
894                                         rows = new GridViewRowCollection (new ArrayList ());
895                                 return rows;
896                         }
897                 }
898                 
899                 [WebCategoryAttribute ("Styles")]
900                 [PersistenceMode (PersistenceMode.InnerProperty)]
901                 [NotifyParentProperty (true)]
902                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
903                 public TableItemStyle RowStyle {
904                         get {
905                                 if (rowStyle == null) {
906                                         rowStyle = new TableItemStyle ();
907                                         if (IsTrackingViewState)
908                                                 rowStyle.TrackViewState();
909                                 }
910                                 return rowStyle;
911                         }
912                 }
913                 
914                 [BrowsableAttribute (false)]
915                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
916                 public virtual DataKey SelectedDataKey {
917                         get {
918                                 if (DataKeyNames.Length == 0)
919                                         throw new InvalidOperationException (String.Format ("Data keys must be specified on GridView '{0}' before the selected data keys can be retrieved.  Use the DataKeyNames property to specify data keys.", ID));
920
921                                 if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
922                                         return DataKeys [selectedIndex];
923                                 } else
924                                         return null;
925                         }
926                 }
927
928                 [MonoTODO]
929                 [BrowsableAttribute(false)]
930                 public virtual DataKey SelectedPersistedDataKey {
931                         get; set;
932                 }
933
934                 [MonoTODO]
935                 DataKey IPersistedSelector.DataKey {
936                         get { return SelectedPersistedDataKey; }
937                         set { SelectedPersistedDataKey = value; }
938                 }
939                 
940                 [BindableAttribute (true)]
941                 [DefaultValueAttribute (-1)]
942                 public virtual int SelectedIndex {
943                         get {
944                                 return selectedIndex;
945                         }
946                         set {
947                                 if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
948                                         int oldIndex = selectedIndex;
949                                         selectedIndex = -1;
950                                         Rows [oldIndex].RowState = GetRowState (oldIndex);
951                                 }
952                                 selectedIndex = value;
953                                 if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
954                                         Rows [selectedIndex].RowState = GetRowState (selectedIndex);
955                                 }
956                         }
957                 }
958         
959                 [BrowsableAttribute (false)]
960                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
961                 public virtual GridViewRow SelectedRow {
962                         get {
963                                 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
964                                         return Rows [selectedIndex];
965                                 } else
966                                         return null;
967                         }
968                 }
969                 
970                 [WebCategoryAttribute ("Styles")]
971                 [PersistenceMode (PersistenceMode.InnerProperty)]
972                 [NotifyParentProperty (true)]
973                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
974                 public TableItemStyle SelectedRowStyle {
975                         get {
976                                 if (selectedRowStyle == null) {
977                                         selectedRowStyle = new TableItemStyle ();
978                                         if (IsTrackingViewState)
979                                                 selectedRowStyle.TrackViewState();
980                                 }
981                                 return selectedRowStyle;
982                         }
983                 }
984                 
985                 [BrowsableAttribute (false)]
986                 public object SelectedValue {
987                         get {
988                                 if (SelectedDataKey != null)
989                                         return SelectedDataKey.Value;
990                                 else
991                                         return null;
992                         }
993                 }
994                 
995                 [WebCategoryAttribute ("Appearance")]
996                 [DefaultValueAttribute (false)]
997                 public virtual bool ShowFooter {
998                         get {
999                                 object ob = ViewState ["ShowFooter"];
1000                                 if (ob != null) return (bool) ob;
1001                                 return false;
1002                         }
1003                         set {
1004                                 if (value == ShowFooter)
1005                                         return;
1006                                 ViewState ["ShowFooter"] = value;
1007                                 RequireBinding ();
1008                         }
1009                 }
1010         
1011                 [WebCategoryAttribute ("Appearance")]
1012                 [DefaultValueAttribute (true)]
1013                 public virtual bool ShowHeader {
1014                         get {
1015                                 object ob = ViewState ["ShowHeader"];
1016                                 if (ob != null) return (bool) ob;
1017                                 return true;
1018                         }
1019                         set {
1020                                 if (value == ShowHeader)
1021                                         return;
1022                                 ViewState ["ShowHeader"] = value;
1023                                 RequireBinding ();
1024                         }
1025                 }
1026                 
1027                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
1028                 [BrowsableAttribute (false)]
1029                 [DefaultValueAttribute (SortDirection.Ascending)]
1030                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
1031                 public virtual SortDirection SortDirection {
1032                         get { return sortDirection; }
1033                         private set {
1034                                 if (sortDirection == value)
1035                                         return;
1036                                 sortDirection = value;
1037                                 RequireBinding ();
1038                         }
1039                 }
1040                 
1041                 [BrowsableAttribute (false)]
1042                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
1043                 public virtual string SortExpression {
1044                         get {
1045                                 if (sortExpression == null)
1046                                         return String.Empty;
1047                                 return sortExpression;
1048                         }
1049                         private set {
1050                                 if (sortExpression == value)
1051                                         return;
1052                                 sortExpression = value;
1053                                 RequireBinding ();
1054                         }
1055                 }
1056                 
1057                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
1058                 [BrowsableAttribute (false)]
1059                 public virtual GridViewRow TopPagerRow {
1060                         get {
1061                                 EnsureDataBound ();
1062                                 return topPagerRow;
1063                         }
1064                 }
1065         
1066                 [WebCategoryAttribute ("Accessibility")]
1067                 [DefaultValueAttribute (true)]
1068                 public virtual bool UseAccessibleHeader {
1069                         get {
1070                                 object ob = ViewState ["UseAccessibleHeader"];
1071                                 if (ob != null) return (bool) ob;
1072                                 return true;
1073                         }
1074                         set {
1075                                 if (value == UseAccessibleHeader)
1076                                         return;
1077                                 ViewState ["UseAccessibleHeader"] = value;
1078                                 RequireBinding ();
1079                         }
1080                 }
1081         
1082                 public virtual bool IsBindableType (Type type)
1083                 {
1084                         return type.IsPrimitive || type == typeof (string) || type == typeof (decimal) || type == typeof (DateTime) || type == typeof (Guid);
1085                 }
1086                 
1087                 // MSDN: The CreateDataSourceSelectArguments method is a helper method called by 
1088                 // the GridView control to create the DataSourceSelectArguments object that 
1089                 // contains the arguments passed to the data source. In this implementation, 
1090                 // the DataSourceSelectArguments object contains the arguments for paging operations.
1091                 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
1092                 {
1093                         DataSourceSelectArguments arg = DataSourceSelectArguments.Empty;
1094                         DataSourceView view= GetData();
1095                         if (AllowPaging && view.CanPage) {
1096                                 arg.StartRowIndex = PageIndex * PageSize;
1097                                 if (view.CanRetrieveTotalRowCount) {
1098                                         arg.RetrieveTotalRowCount = true;
1099                                         arg.MaximumRows = PageSize;
1100                                 }
1101                                 else {
1102                                         arg.MaximumRows = -1;
1103                                 }
1104                         }
1105
1106                         if (IsBoundUsingDataSourceID && !String.IsNullOrEmpty (sortExpression)) {
1107                                 if (sortDirection == SortDirection.Ascending)
1108                                         arg.SortExpression = sortExpression;
1109                                 else
1110                                         arg.SortExpression = sortExpression + " DESC";
1111                         }
1112                         
1113                         return arg;
1114                 }
1115                 
1116                 protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
1117                 {
1118                         bool autoGenerate = AutoGenerateColumns;
1119
1120                         if (autoGenerate) {
1121                                 IAutoFieldGenerator fieldGenerator = ColumnsGenerator;
1122                                 if (fieldGenerator != null)
1123                                         return fieldGenerator.GenerateFields (this);
1124                         }
1125                         
1126                         ArrayList fields = new ArrayList ();
1127                         
1128                         if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
1129                                 CommandField field = new CommandField ();
1130                                 field.ShowEditButton = AutoGenerateEditButton;
1131                                 field.ShowDeleteButton = AutoGenerateDeleteButton;
1132                                 field.ShowSelectButton = AutoGenerateSelectButton;
1133                                 fields.Add (field);
1134                         }
1135
1136                         fields.AddRange (Columns);
1137                         
1138                         if (autoGenerate) {
1139                                 if (useDataSource)
1140                                         autoFieldProperties = CreateAutoFieldProperties (dataSource);
1141         
1142                                 if (autoFieldProperties != null) {
1143                                         foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
1144                                                 fields.Add (CreateAutoGeneratedColumn (props));
1145                                 }
1146                         }
1147                         
1148                         return fields;
1149                 }
1150                 
1151                 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
1152                 {
1153                         return new AutoGeneratedField (fieldProperties);
1154                 }
1155                 
1156                 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
1157                 {
1158                         if(source == null) return null;
1159                         
1160                         PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
1161                         Type prop_type;
1162                         
1163                         ArrayList retVal = new ArrayList();
1164                         
1165                         if (props == null)
1166                         {
1167                                 object fitem = null;
1168                                 prop_type = null;
1169                                 PropertyInfo prop_item =  source.DataSource.GetType().GetProperty("Item",
1170                                                                                                   BindingFlags.Instance | BindingFlags.Static |
1171                                                                                                   BindingFlags.Public, null, null,
1172                                                                                                   new Type[] { typeof(int) }, null);
1173                                 
1174                                 if (prop_item != null) {
1175                                         prop_type = prop_item.PropertyType;
1176                                 }
1177                                 
1178                                 if (prop_type == null || prop_type == typeof(object)) {
1179                                         IEnumerator en = source.GetEnumerator();
1180                                         if (en != null && en.MoveNext ()) {
1181                                                 fitem = en.Current;
1182                                                 _dataEnumerator = en;
1183                                         }
1184                                         if (fitem != null)
1185                                                 prop_type = fitem.GetType();
1186                                 }
1187                                 
1188                                 if (fitem != null && fitem is ICustomTypeDescriptor) {
1189                                         props = TypeDescriptor.GetProperties(fitem);
1190                                 } else if (prop_type != null) {
1191                                         if (IsBindableType (prop_type)) {
1192                                                 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1193                                                 ((IStateManager)field).TrackViewState();
1194                                                 field.Name = "Item";
1195                                                 field.DataField = BoundField.ThisExpression;
1196                                                 field.Type = prop_type;
1197                                                 retVal.Add (field);
1198                                         } else {
1199                                                 props = TypeDescriptor.GetProperties (prop_type);
1200                                         }
1201                                 }
1202                         }
1203                         
1204                         if (props != null && props.Count > 0)
1205                         {
1206                                 foreach (PropertyDescriptor current in props) {
1207                                         if (IsBindableType (current.PropertyType)) {
1208                                                 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1209                                                 ((IStateManager)field).TrackViewState();
1210                                                 field.Name = current.Name;
1211                                                 field.DataField = current.Name;
1212                                                 for (int i = 0; i < DataKeyNames.Length; i++) {
1213                                                         if (string.Compare (DataKeyNames [i], current.Name, StringComparison.InvariantCultureIgnoreCase) == 0) {
1214                                                                 field.IsReadOnly = true;
1215                                                                 break;
1216                                                         }
1217                                                 }
1218                                                 field.Type = current.PropertyType;
1219                                                 retVal.Add (field);
1220                                         }
1221                                 }
1222                         }
1223
1224                         if (retVal.Count > 0)
1225                                 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1226                         else
1227                                 return new AutoGeneratedFieldProperties [0];
1228                 }
1229                 
1230                 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1231                 {
1232                         GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1233                         return row;
1234                 }
1235                 
1236                 void RequireBinding ()
1237                 {
1238                         if (Initialized) {
1239                                 RequiresDataBinding = true;
1240                         }
1241                 }
1242                 
1243                 protected virtual Table CreateChildTable ()
1244                 {
1245                         return new ContainedTable (this);
1246                 }
1247         
1248                 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1249                 {
1250                         // clear GridView
1251                         Controls.Clear ();
1252                         table = null;
1253                         rows = null;
1254
1255                         if (data == null) {
1256                                 return 0;
1257                         }
1258
1259                         PagedDataSource dataSource;
1260
1261                         if (dataBinding) {
1262                                 DataSourceView view = GetData ();
1263                                 dataSource = new PagedDataSource ();
1264                                 dataSource.DataSource = data;
1265                                 
1266                                 if (AllowPaging) {
1267                                         dataSource.AllowPaging = true;
1268                                         dataSource.PageSize = PageSize;
1269                                         if (view.CanPage) {
1270                                                 dataSource.AllowServerPaging = true;
1271                                                 if (SelectArguments.RetrieveTotalRowCount)
1272                                                         dataSource.VirtualCount = SelectArguments.TotalRowCount;
1273                                         }
1274                                         if (PageIndex >= dataSource.PageCount)
1275                                                 pageIndex = dataSource.PageCount - 1;
1276                                         dataSource.CurrentPageIndex = PageIndex;
1277                                 }
1278                                 
1279                                 PageCount = dataSource.PageCount;
1280                         }
1281                         else
1282                         {
1283                                 dataSource = new PagedDataSource ();
1284                                 dataSource.DataSource = data;
1285                                 if (AllowPaging) {
1286                                         dataSource.AllowPaging = true;
1287                                         dataSource.PageSize = PageSize;
1288                                         dataSource.CurrentPageIndex = PageIndex;
1289                                 }
1290                         }
1291
1292                         bool createPager = AllowPaging && (PageCount >= 1) && PagerSettings.Visible;
1293
1294                         ArrayList list = new ArrayList ();
1295                         
1296                         // Creates the set of fields to show
1297
1298                         _dataEnumerator = null;
1299                         ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1300                         int fieldCount = fieldCollection.Count;
1301                         DataControlField dcf;
1302                         DataControlField[] fields = new DataControlField [fieldCount];
1303                         fieldCollection.CopyTo (fields, 0);
1304                         
1305                         for (int i = 0; i < fieldCount; i++) {
1306                                 dcf = fields [i];
1307                                 dcf.Initialize (AllowSorting, this);
1308                                 if (EnableSortingAndPagingCallbacks)
1309                                         dcf.ValidateSupportsCallback ();
1310                         }
1311
1312                         bool skip_first = false;
1313                         IEnumerator enumerator;
1314                         if (_dataEnumerator != null) {
1315                                 // replaced when creating bound columns
1316                                 enumerator = _dataEnumerator;
1317                                 skip_first = true;
1318                         }
1319                         else {
1320                                 enumerator = dataSource.GetEnumerator ();
1321                         }
1322
1323                         // Main table creation
1324                         Table mainTable = ContainedTable;
1325                         while (skip_first || enumerator.MoveNext ()) {
1326                                 skip_first = false;
1327                                 object obj = enumerator.Current;
1328                                 
1329                                 if (list.Count == 0) {
1330                                         if (createPager && (PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom)) {
1331                                                 topPagerRow = CreatePagerRow (fieldCount, dataSource);
1332                                                 OnRowCreated (new GridViewRowEventArgs (topPagerRow));
1333                                                 mainTable.Rows.Add (topPagerRow);
1334                                                 if (dataBinding) {
1335                                                         topPagerRow.DataBind ();
1336                                                         OnRowDataBound (new GridViewRowEventArgs (topPagerRow));
1337                                                 }
1338                                                 if (PageCount == 1)
1339                                                         topPagerRow.Visible = false;
1340                                         }
1341
1342                                         GridViewRow headerRow = CreateRow (-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
1343                                         InitializeRow (headerRow, fields);
1344                                         OnRowCreated (new GridViewRowEventArgs (headerRow));
1345                                         mainTable.Rows.Add (headerRow);
1346                                         if (dataBinding) {
1347                                                 headerRow.DataBind ();
1348                                                 OnRowDataBound (new GridViewRowEventArgs (headerRow));
1349                                         }
1350                                 }
1351                                 
1352                                 DataControlRowState rstate = GetRowState (list.Count);
1353                                 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1354                                 row.DataItem = obj;
1355                                 list.Add (row);
1356                                 InitializeRow (row, fields);
1357                                 OnRowCreated (new GridViewRowEventArgs (row));
1358                                 mainTable.Rows.Add (row);
1359                                 if (dataBinding) {
1360                                         row.DataBind ();                                        
1361                                         if (EditIndex == row.RowIndex)
1362                                                 oldEditValues = new DataKey (GetRowValues (row, true, true));
1363                                         DataKeyArrayList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
1364                                         OnRowDataBound (new GridViewRowEventArgs (row));
1365                                 } 
1366                         }
1367
1368                         if (list.Count == 0) {
1369                                 GridViewRow emptyRow = CreateEmptyrRow (fieldCount);
1370                                 if (emptyRow != null) {
1371                                         OnRowCreated (new GridViewRowEventArgs (emptyRow));
1372                                         mainTable.Rows.Add (emptyRow);
1373                                         if (dataBinding) {
1374                                                 emptyRow.DataBind ();
1375                                                 OnRowDataBound (new GridViewRowEventArgs (emptyRow));
1376                                         }
1377                                 }
1378                                 return 0;
1379                         }
1380                         else {
1381                                 GridViewRow footerRow = CreateRow (-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
1382                                 InitializeRow (footerRow, fields);
1383                                 OnRowCreated (new GridViewRowEventArgs (footerRow));
1384                                 mainTable.Rows.Add (footerRow);
1385                                 if (dataBinding) {
1386                                         footerRow.DataBind ();
1387                                         OnRowDataBound (new GridViewRowEventArgs (footerRow));
1388                                 }
1389
1390                                 if (createPager && (PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom)) {
1391                                         bottomPagerRow = CreatePagerRow (fieldCount, dataSource);
1392                                         OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
1393                                         mainTable.Rows.Add (bottomPagerRow);
1394                                         if (dataBinding) {
1395                                                 bottomPagerRow.DataBind ();
1396                                                 OnRowDataBound (new GridViewRowEventArgs (bottomPagerRow));
1397                                         }
1398                                         if (PageCount == 1)
1399                                                 bottomPagerRow.Visible = false;
1400                                 }
1401                         }
1402
1403                         rows = new GridViewRowCollection (list);
1404
1405                         if (!dataBinding)
1406                                 return -1;
1407
1408                         if (AllowPaging)
1409                                 return dataSource.DataSourceCount;
1410                         else
1411                                 return list.Count;
1412                 }
1413
1414                 Table ContainedTable  {
1415                         get {
1416                                 if (table == null) {
1417                                         table = CreateChildTable ();
1418                                         Controls.Add (table);
1419                                 }
1420                                 return table;
1421                         }
1422                 }
1423
1424                 protected override Style CreateControlStyle ()
1425                 {
1426                         TableStyle style = new TableStyle (ViewState);
1427                         style.GridLines = GridLines.Both;
1428                         style.CellSpacing = 0;
1429                         return style;
1430                 }
1431                 
1432                 DataControlRowState GetRowState (int index)
1433                 {
1434                         DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1435                         if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
1436                         if (index == EditIndex) rstate |= DataControlRowState.Edit;
1437                         return rstate;
1438                 }
1439                 
1440                 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1441                 {
1442                         GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1443                         InitializePager (row, fieldCount, dataSource);
1444                         return row;
1445                 }
1446                 
1447                 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1448                 {
1449                         TableCell cell = new TableCell ();
1450                         if (columnSpan > 1)
1451                                 cell.ColumnSpan = columnSpan;
1452                         
1453                         if (pagerTemplate != null)
1454                                 pagerTemplate.InstantiateIn (cell);
1455                         else
1456                                 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
1457                         
1458                         row.Cells.Add (cell);
1459                 }
1460                 
1461                 GridViewRow CreateEmptyrRow (int fieldCount)
1462                 {
1463                         if (emptyDataTemplate == null && String.IsNullOrEmpty (EmptyDataText))
1464                                 return null;
1465
1466                         GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1467                         TableCell cell = new TableCell ();
1468                         cell.ColumnSpan = fieldCount;
1469                         
1470                         if (emptyDataTemplate != null)
1471                                 emptyDataTemplate.InstantiateIn (cell);
1472                         else
1473                                 cell.Text = EmptyDataText;
1474                         
1475                         row.Cells.Add (cell);
1476                         return row;
1477                 }
1478                 
1479                 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1480                 {
1481                         DataControlCellType ctype;
1482                         bool accessibleHeader = false;
1483
1484                         switch (row.RowType) {
1485                         case DataControlRowType.Header:
1486                                 ctype = DataControlCellType.Header; 
1487                                 accessibleHeader = UseAccessibleHeader;
1488                                 break;
1489                         case DataControlRowType.Footer:
1490                                 ctype = DataControlCellType.Footer;
1491                                 break;
1492                         default:
1493                                 ctype = DataControlCellType.DataCell;
1494                                 break;
1495                         }
1496                         
1497                         for (int n=0; n<fields.Length; n++) {
1498                                 DataControlField field = fields [n];
1499                                 
1500                                 DataControlFieldCell cell;
1501                                 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1502                                         cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1503                                 else
1504                                         cell = new DataControlFieldCell (field);
1505                                 row.Cells.Add (cell);
1506                                 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1507                         }
1508                 }
1509                 
1510                 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1511                 {
1512                         if (cachedKeyProperties == null) {
1513                                 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
1514                                 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1515                                 for (int n=0; n<DataKeyNames.Length; n++) { 
1516                                         PropertyDescriptor p = props.Find (DataKeyNames [n], true);
1517                                         if (p == null)
1518                                                 throw new InvalidOperationException ("Property '" + DataKeyNames [n] + "' not found in object of type " + row.DataItem.GetType ());
1519                                         cachedKeyProperties [n] = p;
1520                                 }
1521                         }
1522                         
1523                         OrderedDictionary dic = new OrderedDictionary ();
1524                         foreach (PropertyDescriptor p in cachedKeyProperties)
1525                                 dic [p.Name] = p.GetValue (row.DataItem);
1526                         return dic;
1527                 }
1528                 
1529                 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1530                 {
1531                         OrderedDictionary dic = new OrderedDictionary ();
1532                         ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1533                         return dic;
1534                 }
1535                 
1536                 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1537                 {
1538                         DataControlField field;
1539                         foreach (TableCell cell in row.Cells) {
1540                                 DataControlFieldCell c = cell as DataControlFieldCell;
1541                                 if (c == null)
1542                                         continue;
1543                                 
1544                                 field = c.ContainingField;
1545                                 if (field != null && !field.Visible)
1546                                         continue;
1547                                 
1548                                 c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1549                         }
1550                         if (!includePrimaryKey && DataKeyNames != null)
1551                                 foreach (string key in DataKeyNames)
1552                                         fieldValues.Remove (key);
1553                 }
1554                 
1555                 protected override HtmlTextWriterTag TagKey {
1556                         get {
1557                                 if (EnableSortingAndPagingCallbacks)
1558                                         return HtmlTextWriterTag.Div;
1559                                 else
1560                                         return HtmlTextWriterTag.Table;
1561                         }
1562                 }
1563                 
1564                 public sealed override void DataBind ()
1565                 {
1566                         DataKeyArrayList.Clear ();
1567                         cachedKeyProperties = null;
1568                         base.DataBind ();
1569
1570                         keys = new DataKeyArray (DataKeyArrayList);
1571                         
1572                         GridViewRow row = HeaderRow;
1573                         if (row != null)
1574                                 row.Visible = ShowHeader;
1575
1576                         row = FooterRow;
1577                         if (row != null)
1578                                 row.Visible = ShowFooter;
1579                 }
1580                 
1581                 protected internal override void PerformDataBinding (IEnumerable data)
1582                 {
1583                         base.PerformDataBinding (data);
1584                 }
1585
1586                 protected internal virtual void PrepareControlHierarchy ()
1587                 {
1588                         if (table == null)
1589                                 return;
1590
1591                         table.Caption = Caption;
1592                         table.CaptionAlign = CaptionAlign;
1593                         table.CopyBaseAttributes (this);
1594                         
1595                         foreach (GridViewRow row in table.Rows) {
1596                                 switch (row.RowType) {
1597                                 case DataControlRowType.Header:
1598                                         if (headerStyle != null && !headerStyle.IsEmpty)
1599                                                 row.ControlStyle.MergeWith(headerStyle);
1600                                         row.Visible = ShowHeader;
1601                                         break;
1602                                 case DataControlRowType.Footer:
1603                                         if (footerStyle != null && !footerStyle.IsEmpty)
1604                                                 row.ControlStyle.MergeWith (footerStyle);
1605                                         row.Visible = ShowFooter;
1606                                         break;
1607                                 case DataControlRowType.Pager:
1608                                         if (pagerStyle != null && !pagerStyle.IsEmpty)
1609                                                 row.ControlStyle.MergeWith (pagerStyle);
1610                                         break;
1611                                 case DataControlRowType.EmptyDataRow:
1612                                         if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
1613                                                 row.ControlStyle.MergeWith (emptyDataRowStyle);
1614                                         break;
1615                                 case DataControlRowType.DataRow:
1616                                         if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
1617                                                 row.ControlStyle.MergeWith (editRowStyle);
1618                                         if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null && !selectedRowStyle.IsEmpty)
1619                                                 row.ControlStyle.MergeWith (selectedRowStyle);
1620                                         if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
1621                                                 row.ControlStyle.MergeWith (alternatingRowStyle);
1622                                         if (rowStyle != null && !rowStyle.IsEmpty)
1623                                                 row.ControlStyle.MergeWith (rowStyle);
1624                                         break;
1625                                 default:
1626                                         break;
1627                                 }
1628
1629                                 foreach (TableCell cell in row.Cells) {
1630                                         DataControlFieldCell fcell = cell as DataControlFieldCell;
1631                                         if (fcell != null) {
1632                                                 DataControlField field = fcell.ContainingField;
1633                                                 if (field == null)
1634                                                         continue;
1635                                                 if (!field.Visible) {
1636                                                         cell.Visible = false;
1637                                                         continue;
1638                                                 }
1639                                                 
1640                                                 switch (row.RowType) {
1641                                                 case DataControlRowType.Header:
1642                                                         if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
1643                                                                 cell.ControlStyle.MergeWith (field.HeaderStyle);
1644                                                         break;
1645                                                 case DataControlRowType.Footer:
1646                                                         if (field.FooterStyleCreated && !field.FooterStyle.IsEmpty)
1647                                                                 cell.ControlStyle.MergeWith (field.FooterStyle);
1648                                                         break;
1649                                                 default:
1650                                                         if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty)
1651                                                                 foreach (Control c in cell.Controls) {
1652                                                                         WebControl wc = c as WebControl;
1653                                                                         if (wc != null)
1654                                                                                 wc.ControlStyle.MergeWith (field.ControlStyle);
1655                                                                 }
1656                                                         if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
1657                                                                 cell.ControlStyle.MergeWith (field.ItemStyle);
1658                                                         break;
1659                                                 }
1660                                         }
1661                                 }
1662                         }
1663                 }
1664                 
1665                 protected internal override void OnInit (EventArgs e)
1666                 {
1667                         Page.RegisterRequiresControlState (this);
1668                         base.OnInit (e);
1669                 }
1670                 
1671                 void OnFieldsChanged (object sender, EventArgs args)
1672                 {
1673                         RequireBinding ();
1674                 }
1675                 
1676                 protected override void OnDataPropertyChanged ()
1677                 {
1678                         base.OnDataPropertyChanged ();
1679                         RequireBinding ();
1680                 }
1681                 
1682                 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1683                 {
1684                         base.OnDataSourceViewChanged (sender, e);
1685                         RequireBinding ();
1686                 }
1687                 
1688                 protected override bool OnBubbleEvent (object source, EventArgs e)
1689                 {
1690                         GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1691                         if (args != null) {
1692                                 bool causesValidation = false;
1693                                 IButtonControl button = args.CommandSource as IButtonControl;
1694                                 if (button != null && button.CausesValidation) {
1695                                         Page.Validate (button.ValidationGroup);
1696                                         causesValidation = true;
1697                                 }
1698                                 OnRowCommand (args);
1699                                 string param = args.CommandArgument as string;
1700                                 if (param == null || param.Length == 0) {
1701                                         GridViewRow row = args.Row;
1702                                         if (row != null)
1703                                                 param = row.RowIndex.ToString();
1704                                 }
1705                                 ProcessEvent (args.CommandName, param, causesValidation);
1706                                 return true;
1707                         }
1708                         return base.OnBubbleEvent (source, e);
1709                 }
1710                 
1711                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
1712                 {
1713                         ValidateEvent (UniqueID, eventArgument);
1714                         RaisePostBackEvent (eventArgument);
1715                 }
1716
1717                 // This is prolly obsolete
1718                 protected virtual void RaisePostBackEvent (string eventArgument)
1719                 {
1720                         GridViewCommandEventArgs args;
1721                         int i = eventArgument.IndexOf ('$');
1722                         if (i != -1)
1723                                 args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument.Substring (0, i), eventArgument.Substring (i + 1)));
1724                         else
1725                                 args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument, null));
1726
1727                         OnRowCommand (args);
1728                         ProcessEvent (args.CommandName, (string) args.CommandArgument, false);
1729                 }
1730
1731                 void ProcessEvent (string eventName, string param, bool causesValidation)
1732                 {
1733                         switch (eventName)
1734                         {
1735                         case DataControlCommands.PageCommandName:
1736                                 int newIndex = -1;
1737                                 switch (param) {
1738                                 case DataControlCommands.FirstPageCommandArgument:
1739                                         newIndex = 0;
1740                                         break;
1741                                 case DataControlCommands.LastPageCommandArgument:
1742                                         newIndex = PageCount - 1;
1743                                         break;
1744                                 case DataControlCommands.NextPageCommandArgument:
1745                                         newIndex = PageIndex + 1;
1746                                         break;
1747                                 case DataControlCommands.PreviousPageCommandArgument:
1748                                         newIndex = PageIndex - 1;
1749                                         break;
1750                                 default:
1751                                         int paramIndex = 0;
1752                                         int.TryParse (param, out paramIndex);
1753                                         newIndex = paramIndex - 1;
1754                                         break;
1755                                 }
1756                                 ShowPage (newIndex);
1757                                 break;
1758                                         
1759                         case DataControlCommands.FirstPageCommandArgument:
1760                                 ShowPage (0);
1761                                 break;
1762
1763                         case DataControlCommands.LastPageCommandArgument:
1764                                 ShowPage (PageCount - 1);
1765                                 break;
1766                                         
1767                         case DataControlCommands.NextPageCommandArgument:
1768                                 if (PageIndex < PageCount - 1)
1769                                         ShowPage (PageIndex + 1);
1770                                 break;
1771
1772                         case DataControlCommands.PreviousPageCommandArgument:
1773                                 if (PageIndex > 0)
1774                                         ShowPage (PageIndex - 1);
1775                                 break;
1776                                         
1777                         case DataControlCommands.SelectCommandName:
1778                                 SelectRow (int.Parse (param));
1779                                 break;
1780                                         
1781                         case DataControlCommands.EditCommandName:
1782                                 EditRow (int.Parse (param));
1783                                 break;
1784                                         
1785                         case DataControlCommands.UpdateCommandName:
1786                                 int editIndex = int.Parse (param);
1787                                 UpdateRow (Rows [editIndex], editIndex, causesValidation);
1788                                 break;
1789                                         
1790                         case DataControlCommands.CancelCommandName:
1791                                 CancelEdit ();
1792                                 break;
1793                                         
1794                         case DataControlCommands.DeleteCommandName:
1795                                 DeleteRow (int.Parse (param));
1796                                 break;
1797                                         
1798                         case DataControlCommands.SortCommandName:
1799                                 Sort (param);
1800                                 break;
1801                         }
1802                 }
1803                 
1804                 void Sort (string newSortExpression)
1805                 {
1806                         SortDirection newDirection = SortDirection.Ascending;
1807                         if (sortExpression == newSortExpression && sortDirection == SortDirection.Ascending)
1808                                 newDirection = SortDirection.Descending;
1809
1810                         Sort (newSortExpression, newDirection);
1811                 }
1812                 
1813                 public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
1814                 {
1815                         GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
1816                         OnSorting (args);
1817                         if (args.Cancel) return;
1818                         
1819                         if (IsBoundUsingDataSourceID) {
1820                                 EditIndex = -1;
1821                                 PageIndex = 0;
1822                                 SortExpression = args.SortExpression;
1823                                 SortDirection = args.SortDirection;
1824                         }
1825                         
1826                         OnSorted (EventArgs.Empty);
1827                 }
1828                 
1829                 void SelectRow (int index)
1830                 {
1831                         GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
1832                         OnSelectedIndexChanging (args);
1833                         if (!args.Cancel) {
1834                                 RequireBinding ();
1835                                 SelectedIndex = args.NewSelectedIndex;
1836                                 OnSelectedIndexChanged (EventArgs.Empty);
1837                         }
1838                 }
1839                 
1840                 void ShowPage (int newIndex)
1841                 {
1842                         GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
1843                         OnPageIndexChanging (args);
1844                         
1845                         if (args.Cancel || !IsBoundUsingDataSourceID)
1846                                 return;
1847                         
1848                         EndRowEdit ();
1849                         PageIndex = args.NewPageIndex;
1850                         OnPageIndexChanged (EventArgs.Empty);
1851                 }
1852                 
1853                 void EditRow (int index)
1854                 {
1855                         GridViewEditEventArgs args = new GridViewEditEventArgs (index);
1856                         OnRowEditing (args);
1857                         
1858                         if (args.Cancel || !IsBoundUsingDataSourceID)
1859                                 return;
1860                         
1861                         EditIndex = args.NewEditIndex;
1862                 }
1863                 
1864                 void CancelEdit ()
1865                 {
1866                         GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
1867                         OnRowCancelingEdit (args);
1868                         
1869                         if (args.Cancel || !IsBoundUsingDataSourceID)
1870                                 return;
1871
1872                         EndRowEdit ();
1873                 }
1874
1875                 [MonoTODO ("Support two-way binding expressions")]
1876                 public virtual void UpdateRow (int rowIndex, bool causesValidation)
1877                 {
1878                         if (rowIndex != EditIndex) throw new NotSupportedException ();
1879                         
1880                         GridViewRow row = Rows [rowIndex];
1881                         UpdateRow (row, rowIndex, causesValidation);
1882                 }
1883
1884                 void UpdateRow (GridViewRow row, int rowIndex, bool causesValidation)
1885                 {
1886                         if (causesValidation && Page != null && !Page.IsValid)
1887                                 return;
1888
1889                         currentEditOldValues = CopyOrderedDictionary (OldEditValues.Values);
1890                         currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
1891                         currentEditNewValues = GetRowValues (row, false, false);
1892                         
1893                         GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (rowIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1894                         OnRowUpdating (args);
1895                         
1896                         if (args.Cancel || !IsBoundUsingDataSourceID)
1897                                 return;
1898                         
1899                         DataSourceView view = GetData ();
1900                         if (view == null)
1901                                 throw new HttpException ("The DataSourceView associated to data bound control was null");
1902                         view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1903                 }
1904
1905                 static IOrderedDictionary CopyOrderedDictionary (IOrderedDictionary sourceDic) {
1906                         OrderedDictionary copyDic = new OrderedDictionary ();
1907                         foreach (object key in sourceDic.Keys) {
1908                                 copyDic.Add (key, sourceDic [key]);
1909                         }
1910                         return copyDic;
1911                 }
1912
1913                 bool UpdateCallback (int recordsAffected, Exception exception)
1914                 {
1915                         GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1916                         OnRowUpdated (dargs);
1917
1918                         if (!dargs.KeepInEditMode)                              
1919                                 EndRowEdit ();
1920
1921                         return dargs.ExceptionHandled;
1922                 }
1923                 
1924                 public virtual void DeleteRow (int rowIndex)
1925                 {
1926                         GridViewRow row = Rows [rowIndex];
1927                         currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
1928                         currentEditNewValues = GetRowValues (row, true, true);
1929                         
1930                         GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
1931                         OnRowDeleting (args);
1932
1933                         if (args.Cancel || !IsBoundUsingDataSourceID)
1934                                 return;
1935                         
1936                         RequireBinding ();
1937                         DataSourceView view = GetData ();
1938                         if (view != null)
1939                                 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1940                         else {
1941                                 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1942                                 OnRowDeleted (dargs);
1943                         }
1944                 }
1945
1946                 bool DeleteCallback (int recordsAffected, Exception exception)
1947                 {
1948                         GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1949                         OnRowDeleted (dargs);
1950                         return dargs.ExceptionHandled;
1951                 }
1952                 
1953                 void EndRowEdit ()
1954                 {
1955                         EditIndex = -1;
1956                         oldEditValues = new DataKey (new OrderedDictionary ());
1957                         currentEditRowKeys = null;
1958                         currentEditOldValues = null;
1959                         currentEditNewValues = null;
1960                 }
1961
1962                 protected internal override void LoadControlState (object ob)
1963                 {
1964                         if (ob == null) return;
1965                         object[] state = (object[]) ob;
1966                         base.LoadControlState (state[0]);
1967                         pageIndex = (int) state[1];
1968                         selectedIndex = (int) state[2];
1969                         editIndex = (int) state[3];
1970                         sortExpression = (string) state[4];
1971                         sortDirection = (SortDirection) state[5];
1972                         DataKeyNames = (string []) state [6];
1973                         if (state [7] != null)
1974                                 LoadDataKeyArrayState ((object []) state [7]);
1975                         if (state [8] != null)
1976                                 ((IStateManager) OldEditValues).LoadViewState (state [8]);
1977                 }
1978                 
1979                 protected internal override object SaveControlState ()
1980                 {
1981                         if (EnableSortingAndPagingCallbacks) {
1982                                 Page.ClientScript.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
1983                                 Page.ClientScript.RegisterHiddenField (ClientID + "_SortExpression", SortExpression);
1984                                 Page.ClientScript.RegisterHiddenField (ClientID + "_SortDirection", ((int)SortDirection).ToString());
1985                         }
1986
1987                         object bstate = base.SaveControlState ();
1988                         return new object [] {
1989                                 bstate, 
1990                                 pageIndex, 
1991                                 selectedIndex, 
1992                                 editIndex, 
1993                                 sortExpression, 
1994                                 sortDirection, 
1995                                 DataKeyNames,
1996                                 SaveDataKeyArrayState (),
1997                                 (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState ())
1998                                         };
1999                 }
2000
2001                 object [] SaveDataKeyArrayState ()
2002                 {
2003                         if (keys == null)
2004                                 return null;
2005
2006                         object [] state = new object [keys.Count];
2007                         for (int i = 0; i < keys.Count; i++) {
2008                                 state [i] = ((IStateManager) keys [i]).SaveViewState ();
2009                         }
2010                         return state;
2011                 }
2012
2013                 void LoadDataKeyArrayState (object [] state)
2014                 {
2015                         for (int i = 0; i < state.Length; i++) {
2016                                 DataKey dataKey = new DataKey (new OrderedDictionary (DataKeyNames.Length), DataKeyNames);
2017                                 ((IStateManager) dataKey).LoadViewState (state [i]);
2018                                 DataKeyArrayList.Add (dataKey);
2019                         }
2020                         keys = new DataKeyArray (DataKeyArrayList);
2021                 }
2022
2023                 protected override void TrackViewState()
2024                 {
2025                         base.TrackViewState();
2026                         if (columns != null) ((IStateManager)columns).TrackViewState();
2027                         if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
2028                         if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
2029                         if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
2030                         if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
2031                         if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
2032                         if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
2033                         if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
2034                         if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
2035                         if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
2036                         if (keys != null) ((IStateManager)keys).TrackViewState();
2037                         if (autoFieldProperties != null) {
2038                                 foreach (IStateManager sm in autoFieldProperties)
2039                                         sm.TrackViewState ();
2040                         }
2041                 }
2042
2043                 protected override object SaveViewState()
2044                 {
2045                         object[] states = new object [12];
2046                         states[0] = base.SaveViewState();
2047                         states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
2048                         states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
2049                         states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
2050                         states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
2051                         states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
2052                         states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
2053                         states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
2054                         states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
2055                         states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
2056                         states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
2057                         
2058                         if (autoFieldProperties != null) {
2059                                 object[] data = new object [autoFieldProperties.Length];
2060                                 bool allNull = true;
2061                                 for (int n=0; n<data.Length; n++) {
2062                                         data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
2063                                         if (data [n] != null) allNull = false;
2064                                 }
2065                                 if (!allNull) states [11] = data;
2066                         }
2067
2068                         for (int i = states.Length - 1; i >= 0; i--) {
2069                                 if (states [i] != null)
2070                                         return states;
2071                         }
2072
2073                         return null;
2074                 }
2075
2076                 protected override void LoadViewState (object savedState)
2077                 {
2078                         if (savedState == null) {
2079                                 base.LoadViewState (null);
2080                                 return;
2081                         }
2082
2083                         object [] states = (object []) savedState;
2084                         
2085                         if (states[11] != null) {
2086                                 object[] data = (object[]) states [11];
2087                                 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
2088                                 for (int n=0; n<data.Length; n++) {
2089                                         IStateManager p = new AutoGeneratedFieldProperties ();
2090                                         p.TrackViewState ();
2091                                         p.LoadViewState (data [n]);
2092                                         autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
2093                                 }
2094                         }
2095
2096                         base.LoadViewState (states[0]);
2097                         
2098                         if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
2099                         if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
2100                         if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
2101                         if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
2102                         if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
2103                         if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
2104                         if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
2105                         if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
2106                         if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
2107                         if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
2108                 }
2109                 
2110                 void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
2111                 {
2112                         RaiseCallbackEvent (eventArgs);
2113                 }
2114                 
2115                 protected virtual void RaiseCallbackEvent (string eventArgs)
2116                 {
2117                         string[] clientData = eventArgs.Split ('|');
2118                         PageIndex = int.Parse (clientData[0]);
2119                         SortExpression = HttpUtility.UrlDecode (clientData [1]);
2120                         SortDirection = (SortDirection) int.Parse (clientData [2]);
2121                         
2122                         RaisePostBackEvent (clientData[3]);
2123                         DataBind ();
2124                 }
2125                 
2126                 string ICallbackEventHandler.GetCallbackResult ()
2127                 {
2128                         return GetCallbackResult ();
2129                 }
2130
2131                 protected virtual string GetCallbackResult ()
2132                 {
2133                         PrepareControlHierarchy ();
2134                         
2135                         StringWriter sw = new StringWriter ();
2136                         sw.Write (PageIndex.ToString () + '|' + SortExpression + '|' + (int) SortDirection + '|');
2137
2138                         HtmlTextWriter writer = new HtmlTextWriter (sw);
2139                         RenderGrid (writer);
2140                         return sw.ToString ();
2141                 }
2142
2143                 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
2144                 {
2145                         return GetCallbackScript (control, argument);
2146                 }
2147                 
2148                 protected virtual string GetCallbackScript (IButtonControl control, string argument)
2149                 {
2150                         if (EnableSortingAndPagingCallbacks) {
2151                                 Page page = Page;
2152                                 if (page != null)
2153                                         page.ClientScript.RegisterForEventValidation (UniqueID, argument);
2154                                 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
2155                         } else
2156                                 return null;
2157                 }
2158                 
2159                 protected override void OnPagePreLoad (object sender, EventArgs e)
2160                 {
2161                         base.OnPagePreLoad (sender, e);
2162
2163                         if (Page.IsPostBack && EnableSortingAndPagingCallbacks) {
2164                                 int page;
2165                                 if (int.TryParse (Page.Request.Form [ClientID + "_Page"], out page))
2166                                         PageIndex = page;
2167                                 int dir;
2168                                 if (int.TryParse (Page.Request.Form [ClientID + "_SortDirection"], out dir))
2169                                         SortDirection = (SortDirection) dir;
2170                                 SortExpression = Page.Request.Form [ClientID + "_SortExpression"];
2171                         }
2172                 }
2173
2174
2175                 const string onPreRenderScript = @"var {0} = new Object ();
2176 {0}.pageIndex = {1};
2177 {0}.sortExp = {2};
2178 {0}.sortDir = {3};
2179 {0}.uid = {4};
2180 {0}.form = {5};
2181 ";
2182                 protected internal override void OnPreRender (EventArgs e)
2183                 {
2184                         base.OnPreRender (e);
2185
2186                         if (EnableSortingAndPagingCallbacks) {
2187                                 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
2188                                         string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
2189                                         Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
2190                                 }
2191                                 
2192                                 string cgrid = ClientID + "_data";
2193                                 string script = String.Format (onPreRenderScript,
2194                                                         cgrid,
2195                                                         ClientScriptManager.GetScriptLiteral (PageIndex),
2196                                                         ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression),
2197                                                         ClientScriptManager.GetScriptLiteral ((int) SortDirection),
2198                                                         ClientScriptManager.GetScriptLiteral (UniqueID),
2199                                                         Page.theForm);
2200                                 
2201                                 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
2202                                 
2203                                 // Make sure the basic script infrastructure is rendered
2204                                 Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
2205                                 Page.ClientScript.GetPostBackClientHyperlink (this, "");
2206                         }
2207                 }
2208                 
2209                 protected internal override void Render (HtmlTextWriter writer)
2210                 {
2211                         PrepareControlHierarchy ();
2212                         if (EnableSortingAndPagingCallbacks)
2213                                 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_div");
2214                         writer.RenderBeginTag (HtmlTextWriterTag.Div);
2215
2216                         RenderGrid (writer);
2217
2218                         writer.RenderEndTag ();
2219                 }
2220                 
2221                 void RenderGrid (HtmlTextWriter writer)
2222                 {
2223                         if (table == null)
2224                                 return;
2225
2226                         table.Render (writer);
2227                 }
2228
2229                 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
2230                 {
2231                         if (control == null)
2232                                 throw new ArgumentNullException ("control");
2233                         
2234                         if (control.CausesValidation)
2235                                 throw new InvalidOperationException ("A button that causes validation in GridView '" + 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.");
2236                         
2237                         PostBackOptions options = new PostBackOptions (this);
2238                         options.Argument = control.CommandName + "$" + control.CommandArgument;
2239                         options.RequiresJavaScriptProtocol = true;
2240
2241                         return options;
2242                 }
2243         }
2244 }
2245
2246 #endif