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