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