New test.
[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 sortDirection; }
933                 }
934                 
935                 [BrowsableAttribute (false)]
936                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
937                 public virtual string SortExpression {
938                         get { return sortExpression; }
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                 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
970                 {
971                         return base.CreateDataSourceSelectArguments ();
972                 }
973                 
974                 protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
975                 {
976                         ArrayList fields = new ArrayList ();
977                         fields.AddRange (Columns);
978                         
979                         if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
980                                 CommandField field = new CommandField ();
981                                 field.ShowEditButton = AutoGenerateEditButton;
982                                 field.ShowDeleteButton = AutoGenerateDeleteButton;
983                                 field.ShowSelectButton = AutoGenerateSelectButton;
984                                 fields.Add (field);
985                         }
986                         
987                         if (AutoGenerateColumns) {
988                                 if (useDataSource)
989                                         autoFieldProperties = CreateAutoFieldProperties (dataSource);
990         
991                                 if (autoFieldProperties != null) {
992                                         foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
993                                                 fields.Add (CreateAutoGeneratedColumn (props));
994                                 }
995                         }
996                         
997                         return fields;
998                 }
999                 
1000                 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
1001                 {
1002                         return new AutoGeneratedField (fieldProperties);
1003                 }
1004                 
1005                 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
1006                 {
1007                         if(source == null) return null;
1008                         
1009                         PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
1010                         Type prop_type;
1011                         
1012                         ArrayList retVal = new ArrayList();
1013                         
1014                         if (props == null)
1015                         {
1016                                 object fitem = null;
1017                                 prop_type = null;
1018                                 PropertyInfo prop_item =  source.DataSource.GetType().GetProperty("Item",
1019                                                                                                   BindingFlags.Instance | BindingFlags.Static |
1020                                                                                                   BindingFlags.Public, null, null,
1021                                                                                                   new Type[] { typeof(int) }, null);
1022                                 
1023                                 if (prop_item != null) {
1024                                         prop_type = prop_item.PropertyType;
1025                                 }
1026                                 
1027                                 if (prop_type == null || prop_type == typeof(object)) {
1028                                         IEnumerator en = source.GetEnumerator();
1029                                         if (en.MoveNext())
1030                                                 fitem = en.Current;
1031                                         if (fitem != null)
1032                                                 prop_type = fitem.GetType();
1033                                 }
1034                                 
1035                                 if (fitem != null && fitem is ICustomTypeDescriptor) {
1036                                         props = TypeDescriptor.GetProperties(fitem);
1037                                 } else if (prop_type != null) {
1038                                         if (IsBindableType (prop_type)) {
1039                                                 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1040                                                 ((IStateManager)field).TrackViewState();
1041                                                 field.Name = "Item";
1042                                                 field.DataField = BoundField.ThisExpression;
1043                                                 field.Type = prop_type;
1044                                                 retVal.Add (field);
1045                                         } else {
1046                                                 props = TypeDescriptor.GetProperties (prop_type);
1047                                         }
1048                                 }
1049                         }
1050                         
1051                         if (props != null && props.Count > 0)
1052                         {
1053                                 foreach (PropertyDescriptor current in props) {
1054                                         if (IsBindableType (current.PropertyType)) {
1055                                                 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1056                                                 ((IStateManager)field).TrackViewState();
1057                                                 field.Name = current.Name;
1058                                                 field.DataField = current.Name;
1059                                                 field.IsReadOnly = current.IsReadOnly;
1060                                                 field.Type = current.PropertyType;
1061                                                 retVal.Add (field);
1062                                         }
1063                                 }
1064                         }
1065
1066                         if (retVal.Count > 0)
1067                                 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1068                         else
1069                                 return new AutoGeneratedFieldProperties [0];
1070                 }
1071                 
1072                 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1073                 {
1074                         GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1075                         OnRowCreated (new GridViewRowEventArgs (row));
1076                         return row;
1077                 }
1078                 
1079                 void RequireBinding ()
1080                 {
1081                         if (Initialized) {
1082                                 RequiresDataBinding = true;
1083                                 pageCount = -1;
1084                         }
1085                 }
1086                 
1087                 protected virtual Table CreateChildTable ()
1088                 {
1089                         return new ContainedTable (this);
1090                 }
1091         
1092                 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1093                 {
1094                         PagedDataSource dataSource;
1095
1096                         if (dataBinding) {
1097                                 DataSourceView view = GetData ();
1098                                 dataSource = new PagedDataSource ();
1099                                 dataSource.DataSource = data;
1100                                 
1101                                 if (AllowPaging) {
1102                                         dataSource.AllowPaging = true;
1103                                         dataSource.PageSize = PageSize;
1104                                         dataSource.CurrentPageIndex = PageIndex;
1105                                         if (view.CanPage) {
1106                                                 dataSource.AllowServerPaging = true;
1107                                                 if (view.CanRetrieveTotalRowCount)
1108                                                         dataSource.VirtualCount = SelectArguments.TotalRowCount;
1109                                         }
1110                                 }
1111                                 
1112                                 pageCount = dataSource.PageCount;
1113                         }
1114                         else
1115                         {
1116                                 dataSource = new PagedDataSource ();
1117                                 dataSource.DataSource = data;
1118                                 if (AllowPaging) {
1119                                         dataSource.AllowPaging = true;
1120                                         dataSource.PageSize = PageSize;
1121                                         dataSource.CurrentPageIndex = PageIndex;
1122                                 }
1123                         }
1124
1125                         bool showPager = AllowPaging && (PageCount > 1);
1126                         
1127                         Controls.Clear ();
1128                         table = CreateChildTable ();
1129                         Controls.Add (table);
1130                                 
1131                         ArrayList list = new ArrayList ();
1132                         ArrayList keyList = new ArrayList ();
1133                         
1134                         // Creates the set of fields to show
1135                         
1136                         ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1137                         DataControlField[] fields = new DataControlField [fieldCollection.Count];
1138                         fieldCollection.CopyTo (fields, 0);
1139
1140                         foreach (DataControlField field in fields) {
1141                                 field.Initialize (AllowSorting, this);
1142                                 if (EnableSortingAndPagingCallbacks)
1143                                         field.ValidateSupportsCallback ();
1144                         }
1145
1146                         // Main table creation
1147                         
1148                         if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
1149                                 topPagerRow = CreatePagerRow (fields.Length, dataSource);
1150                                 table.Rows.Add (topPagerRow);
1151                         }
1152
1153                         GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
1154                         table.Rows.Add (headerRow);
1155                         InitializeRow (headerRow, fields);
1156                         
1157                         foreach (object obj in dataSource) {
1158                                 DataControlRowState rstate = GetRowState (list.Count);
1159                                 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1160                                 row.DataItem = obj;
1161                                 list.Add (row);
1162                                 table.Rows.Add (row);
1163                                 InitializeRow (row, fields);
1164                                 if (dataBinding) {
1165                                         row.DataBind ();
1166                                         OnRowDataBound (new GridViewRowEventArgs (row));
1167                                         if (EditIndex == row.RowIndex)
1168                                                 oldEditValues = new DataKey (GetRowValues (row, false, true));
1169                                         keyList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
1170                                 } else {
1171                                         if (EditIndex == row.RowIndex)
1172                                                 oldEditValues = new DataKey (new OrderedDictionary ());
1173                                         keyList.Add (new DataKey (new OrderedDictionary (), DataKeyNames));
1174                                 }
1175
1176                                 if (list.Count >= PageSize)
1177                                         break;
1178                         }
1179                         
1180                         if (list.Count == 0)
1181                                 table.Rows.Add (CreateEmptyrRow (fields.Length));
1182
1183                         GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
1184                         table.Rows.Add (footerRow);
1185                         InitializeRow (footerRow, fields);
1186
1187                         if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
1188                                 bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
1189                                 table.Rows.Add (bottomPagerRow);
1190                         }
1191
1192                         rows = new GridViewRowCollection (list);
1193                         keys = new DataKeyArray (keyList);
1194
1195                         return dataSource.DataSourceCount;
1196                 }
1197
1198                 protected override Style CreateControlStyle ()
1199                 {
1200                         TableStyle style = new TableStyle (ViewState);
1201                         style.GridLines = GridLines.Both;
1202                         style.CellSpacing = 0;
1203                         return style;
1204                 }
1205                 
1206                 DataControlRowState GetRowState (int index)
1207                 {
1208                         DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1209                         if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
1210                         if (index == EditIndex) rstate |= DataControlRowState.Edit;
1211                         return rstate;
1212                 }
1213                 
1214                 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1215                 {
1216                         GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1217                         InitializePager (row, fieldCount, dataSource);
1218                         return row;
1219                 }
1220                 
1221                 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1222                 {
1223                         TableCell cell = new TableCell ();
1224                         if (columnSpan > 1)
1225                                 cell.ColumnSpan = columnSpan;
1226                         
1227                         if (pagerTemplate != null)
1228                                 pagerTemplate.InstantiateIn (cell);
1229                         else
1230                                 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount, pagerStyle));
1231                         
1232                         row.Cells.Add (cell);
1233                 }
1234                 
1235                 GridViewRow CreateEmptyrRow (int fieldCount)
1236                 {
1237                         GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1238                         TableCell cell = new TableCell ();
1239                         cell.ColumnSpan = fieldCount;
1240                         
1241                         if (emptyDataTemplate != null)
1242                                 emptyDataTemplate.InstantiateIn (cell);
1243                         else
1244                                 cell.Text = EmptyDataText;
1245                         
1246                         row.Cells.Add (cell);
1247                         return row;
1248                 }
1249                 
1250                 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1251                 {
1252                         DataControlCellType ctype;
1253                         bool accessibleHeader = false;
1254
1255                         switch (row.RowType) {
1256                         case DataControlRowType.Header:
1257                                 ctype = DataControlCellType.Header; 
1258                                 accessibleHeader = UseAccessibleHeader;
1259                                 break;
1260                         case DataControlRowType.Footer:
1261                                 ctype = DataControlCellType.Footer;
1262                                 break;
1263                         default:
1264                                 ctype = DataControlCellType.DataCell;
1265                                 break;
1266                         }
1267                         
1268                         for (int n=0; n<fields.Length; n++) {
1269                                 DataControlField field = fields [n];
1270                                 DataControlFieldCell cell;
1271                                 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1272                                         cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1273                                 else
1274                                         cell = new DataControlFieldCell (field);
1275                                 row.Cells.Add (cell);
1276                                 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1277                         }
1278                 }
1279                 
1280                 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1281                 {
1282                         if (cachedKeyProperties == null) {
1283                                 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
1284                                 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1285                                 for (int n=0; n<DataKeyNames.Length; n++) { 
1286                                         PropertyDescriptor p = props [DataKeyNames[n]];
1287                                         if (p == null)
1288                                                 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + row.DataItem.GetType());
1289                                         cachedKeyProperties [n] = p;
1290                                 }
1291                         }
1292                         
1293                         OrderedDictionary dic = new OrderedDictionary ();
1294                         foreach (PropertyDescriptor p in cachedKeyProperties)
1295                                 dic [p.Name] = p.GetValue (row.DataItem);
1296                         return dic;
1297                 }
1298                 
1299                 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1300                 {
1301                         OrderedDictionary dic = new OrderedDictionary ();
1302                         ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1303                         return dic;
1304                 }
1305                 
1306                 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1307                 {
1308                         foreach (TableCell cell in row.Cells) {
1309                                 DataControlFieldCell c = cell as DataControlFieldCell;
1310                                 if (c != null)
1311                                         c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1312                         }
1313                         if (!includePrimaryKey && DataKeyNames != null)
1314                                 foreach (string key in DataKeyNames)
1315                                         fieldValues.Remove (key);
1316                 }
1317                 
1318                 protected override HtmlTextWriterTag TagKey {
1319                         get {
1320                                 if (EnableSortingAndPagingCallbacks)
1321                                         return HtmlTextWriterTag.Div;
1322                                 else
1323                                         return HtmlTextWriterTag.Table;
1324                         }
1325                 }
1326                 
1327                 public sealed override void DataBind ()
1328                 {
1329                         DataSourceView view = GetData ();
1330                         if (AllowPaging && view.CanPage) {
1331                                 SelectArguments.StartRowIndex = PageIndex * PageSize;
1332                                 SelectArguments.MaximumRows = PageSize;
1333                                 if (view.CanRetrieveTotalRowCount)
1334                                         SelectArguments.RetrieveTotalRowCount = true;
1335                         }
1336
1337                         if (sortExpression != "") {
1338                                 if (sortDirection == SortDirection.Ascending)
1339                                         SelectArguments.SortExpression = sortExpression;
1340                                 else
1341                                         SelectArguments.SortExpression = sortExpression + " DESC";
1342                         }
1343                         
1344                         cachedKeyProperties = null;
1345                         base.DataBind ();
1346                 }
1347                 
1348                 protected internal override void PerformDataBinding (IEnumerable data)
1349                 {
1350                         base.PerformDataBinding (data);
1351                 }
1352
1353                 [MonoTODO]
1354                 protected internal virtual void PrepareControlHierarchy ()
1355                 {
1356                         throw new NotImplementedException ();
1357                 }
1358                 
1359                 protected internal override void OnInit (EventArgs e)
1360                 {
1361                         Page.RegisterRequiresControlState (this);
1362                         base.OnInit (e);
1363                 }
1364                 
1365                 void OnFieldsChanged (object sender, EventArgs args)
1366                 {
1367                         RequireBinding ();
1368                 }
1369                 
1370                 protected override void OnDataPropertyChanged ()
1371                 {
1372                         base.OnDataPropertyChanged ();
1373                         RequireBinding ();
1374                 }
1375                 
1376                 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1377                 {
1378                         base.OnDataSourceViewChanged (sender, e);
1379                         RequireBinding ();
1380                 }
1381                 
1382                 protected override bool OnBubbleEvent (object source, EventArgs e)
1383                 {
1384                         GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1385                         if (args != null) {
1386                                 OnRowCommand (args);
1387                                 string param = args.CommandArgument as string;
1388                                 if (param == null || param.Length == 0) {
1389                                         GridViewRow row = args.Row;
1390                                         if (row != null)
1391                                                 param = row.RowIndex.ToString();
1392                                 }
1393                                 ProcessEvent (args.CommandName, param);
1394                         }
1395                         return base.OnBubbleEvent (source, e);
1396                 }
1397                 
1398                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
1399                 {
1400                         RaisePostBackEvent (eventArgument);
1401                 }
1402
1403                 // This is prolly obsolete
1404                 protected virtual void RaisePostBackEvent (string eventArgument)
1405                 {
1406                         int i = eventArgument.IndexOf ('$');
1407                         if (i != -1)
1408                                 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1409                         else
1410                                 ProcessEvent (eventArgument, null);
1411                 }
1412                 
1413                 void ProcessEvent (string eventName, string param)
1414                 {
1415                         switch (eventName)
1416                         {
1417                         case DataControlCommands.PageCommandName:
1418                                 int newIndex = -1;
1419                                 switch (param) {
1420                                 case DataControlCommands.FirstPageCommandArgument:
1421                                         newIndex = 0;
1422                                         break;
1423                                 case DataControlCommands.LastPageCommandArgument:
1424                                         newIndex = PageCount - 1;
1425                                         break;
1426                                 case DataControlCommands.NextPageCommandArgument:
1427                                         if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1428                                         break;
1429                                 case DataControlCommands.PreviousPageCommandArgument:
1430                                         if (PageIndex > 0) newIndex = PageIndex - 1;
1431                                         break;
1432                                 default:
1433                                         newIndex = int.Parse (param) - 1;
1434                                         break;
1435                                 }
1436                                 ShowPage (newIndex);
1437                                 break;
1438                                         
1439                         case DataControlCommands.FirstPageCommandArgument:
1440                                 ShowPage (0);
1441                                 break;
1442
1443                         case DataControlCommands.LastPageCommandArgument:
1444                                 ShowPage (PageCount - 1);
1445                                 break;
1446                                         
1447                         case DataControlCommands.NextPageCommandArgument:
1448                                 if (PageIndex < PageCount - 1)
1449                                         ShowPage (PageIndex + 1);
1450                                 break;
1451
1452                         case DataControlCommands.PreviousPageCommandArgument:
1453                                 if (PageIndex > 0)
1454                                         ShowPage (PageIndex - 1);
1455                                 break;
1456                                         
1457                         case DataControlCommands.SelectCommandName:
1458                                 SelectRow (int.Parse (param));
1459                                 break;
1460                                         
1461                         case DataControlCommands.EditCommandName:
1462                                 EditRow (int.Parse (param));
1463                                 break;
1464                                         
1465                         case DataControlCommands.UpdateCommandName:
1466                                 UpdateRow (EditIndex, true);
1467                                 break;
1468                                         
1469                         case DataControlCommands.CancelCommandName:
1470                                 CancelEdit ();
1471                                 break;
1472                                         
1473                         case DataControlCommands.DeleteCommandName:
1474                                 DeleteRow (int.Parse (param));
1475                                 break;
1476                                         
1477                         case DataControlCommands.SortCommandName:
1478                                 Sort (param);
1479                                 break;
1480                         }
1481                 }
1482                 
1483                 void Sort (string newSortExpression)
1484                 {
1485                         SortDirection newDirection;
1486                         if (sortExpression == newSortExpression) {
1487                                 if (sortDirection == SortDirection.Ascending)
1488                                         newDirection = SortDirection.Descending;
1489                                 else
1490                                         newDirection = SortDirection.Ascending;
1491                         } else
1492                                 newDirection = sortDirection;
1493                         
1494                         Sort (newSortExpression, newDirection);
1495                 }
1496                 
1497                 public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
1498                 {
1499                         GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
1500                         OnSorting (args);
1501                         if (args.Cancel) return;
1502                         
1503                         sortExpression = args.SortExpression;
1504                         sortDirection = args.SortDirection;
1505                         RequireBinding ();
1506                         
1507                         OnSorted (EventArgs.Empty);
1508                 }
1509                 
1510                 void SelectRow (int index)
1511                 {
1512                         GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
1513                         OnSelectedIndexChanging (args);
1514                         if (!args.Cancel) {
1515                                 SelectedIndex = args.NewSelectedIndex;
1516                                 OnSelectedIndexChanged (EventArgs.Empty);
1517                         }
1518                 }
1519                 
1520                 void ShowPage (int newIndex)
1521                 {
1522                         GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
1523                         OnPageIndexChanging (args);
1524                         if (!args.Cancel) {
1525                                 EndRowEdit ();
1526                                 PageIndex = args.NewPageIndex;
1527                                 OnPageIndexChanged (EventArgs.Empty);
1528                         }
1529                 }
1530                 
1531                 void EditRow (int index)
1532                 {
1533                         GridViewEditEventArgs args = new GridViewEditEventArgs (index);
1534                         OnRowEditing (args);
1535                         if (!args.Cancel) {
1536                                 EditIndex = args.NewEditIndex;
1537                         }
1538                 }
1539                 
1540                 void CancelEdit ()
1541                 {
1542                         GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
1543                         OnRowCancelingEdit (args);
1544                         if (!args.Cancel) {
1545                                 EndRowEdit ();
1546                         }
1547                 }
1548
1549                 [MonoTODO ("Support two-way binding expressions")]
1550                 public virtual void UpdateRow (int rowIndex, bool causesValidation)
1551                 {
1552                         if (causesValidation)
1553                                 Page.Validate ();
1554                         
1555                         if (rowIndex != EditIndex) throw new NotSupportedException ();
1556                         
1557                         currentEditOldValues = oldEditValues.Values;
1558
1559                         GridViewRow row = Rows [rowIndex];
1560                         currentEditRowKeys = DataKeys [rowIndex].Values;
1561                         currentEditNewValues = GetRowValues (row, false, false);
1562                         
1563                         GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (EditIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1564                         OnRowUpdating (args);
1565                         if (!args.Cancel) {
1566                                 DataSourceView view = GetData ();
1567                                 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1568                                 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1569                         } else
1570                                 EndRowEdit ();
1571                 }
1572
1573                 bool UpdateCallback (int recordsAffected, Exception exception)
1574                 {
1575                         GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1576                         OnRowUpdated (dargs);
1577
1578                         if (!dargs.KeepInEditMode)                              
1579                                 EndRowEdit ();
1580
1581                         return dargs.ExceptionHandled;
1582                 }
1583                 
1584                 public virtual void DeleteRow (int rowIndex)
1585                 {
1586                         GridViewRow row = Rows [rowIndex];
1587                         currentEditRowKeys = DataKeys [rowIndex].Values;
1588                         currentEditNewValues = GetRowValues (row, true, true);
1589                         
1590                         GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
1591                         OnRowDeleting (args);
1592
1593                         if (!args.Cancel) {
1594                                 RequireBinding ();
1595                                 DataSourceView view = GetData ();
1596                                 if (view != null)
1597                                         view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1598                                 else {
1599                                         GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1600                                         OnRowDeleted (dargs);
1601                                 }
1602                         }
1603                 }
1604
1605                 bool DeleteCallback (int recordsAffected, Exception exception)
1606                 {
1607                         GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1608                         OnRowDeleted (dargs);
1609                         return dargs.ExceptionHandled;
1610                 }
1611                 
1612                 void EndRowEdit ()
1613                 {
1614                         EditIndex = -1;
1615                         oldEditValues = new DataKey (new OrderedDictionary ());
1616                         currentEditRowKeys = null;
1617                         currentEditOldValues = null;
1618                         currentEditNewValues = null;
1619                 }
1620
1621                 protected internal override void LoadControlState (object ob)
1622                 {
1623                         if (ob == null) return;
1624                         object[] state = (object[]) ob;
1625                         base.LoadControlState (state[0]);
1626                         pageIndex = (int) state[1];
1627                         pageCount = (int) state[2];
1628                         selectedIndex = (int) state[3];
1629                         editIndex = (int) state[4];
1630                         sortExpression = (string) state[5];
1631                         sortDirection = (SortDirection) state[6];
1632                         DataKeyNames = (string []) state [7];
1633                 }
1634                 
1635                 protected internal override object SaveControlState ()
1636                 {
1637                         object bstate = base.SaveControlState ();
1638                         return new object[] {
1639                                 bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection, DataKeyNames
1640                                         };
1641                 }
1642                 
1643                 protected override void TrackViewState()
1644                 {
1645                         base.TrackViewState();
1646                         if (columns != null) ((IStateManager)columns).TrackViewState();
1647                         if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1648                         if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
1649                         if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1650                         if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1651                         if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1652                         if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1653                         if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
1654                         if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1655                         if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1656                         if (keys != null) ((IStateManager)keys).TrackViewState();
1657                         if (autoFieldProperties != null) {
1658                                 foreach (IStateManager sm in autoFieldProperties)
1659                                         sm.TrackViewState ();
1660                         }
1661                 }
1662
1663                 protected override object SaveViewState()
1664                 {
1665                         object[] states = new object [14];
1666                         states[0] = base.SaveViewState();
1667                         states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
1668                         states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1669                         states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
1670                         states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1671                         states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1672                         states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1673                         states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1674                         states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
1675                         states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1676                         states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1677                         states[11] = (keys == null ? null : ((IStateManager)keys).SaveViewState());
1678                         states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1679                         
1680                         if (autoFieldProperties != null) {
1681                                 object[] data = new object [autoFieldProperties.Length];
1682                                 bool allNull = true;
1683                                 for (int n=0; n<data.Length; n++) {
1684                                         data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
1685                                         if (data [n] != null) allNull = false;
1686                                 }
1687                                 if (!allNull) states [13] = data;
1688                         }
1689
1690                         for (int i = states.Length - 1; i >= 0; i--) {
1691                                 if (states [i] != null)
1692                                         return states;
1693                         }
1694
1695                         return null;
1696                 }
1697
1698                 protected override void LoadViewState (object savedState)
1699                 {
1700                         if (savedState == null) {
1701                                 base.LoadViewState (null);
1702                                 return;
1703                         }
1704
1705                         object [] states = (object []) savedState;
1706                         
1707                         if (states[13] != null) {
1708                                 object[] data = (object[]) states [13];
1709                                 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
1710                                 for (int n=0; n<data.Length; n++) {
1711                                         IStateManager p = new AutoGeneratedFieldProperties ();
1712                                         p.TrackViewState ();
1713                                         p.LoadViewState (data [n]);
1714                                         autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
1715                                 }
1716                         }
1717
1718                         base.LoadViewState (states[0]);
1719                         EnsureChildControls ();
1720                         
1721                         if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
1722                         if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1723                         if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
1724                         if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1725                         if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1726                         if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1727                         if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1728                         if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
1729                         if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1730                         if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1731                         if (states[11] != null) ((IStateManager)DataKeys).LoadViewState (states[11]);
1732                         if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1733                 }
1734                 
1735                 void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
1736                 {
1737                         RaiseCallbackEvent (eventArgs);
1738                 }
1739                 
1740                 string callbackResult;
1741                 protected virtual void RaiseCallbackEvent (string eventArgs)
1742                 {
1743                         string[] clientData = eventArgs.Split ('|');
1744                         pageIndex = int.Parse (clientData[0]);
1745                         sortExpression = HttpUtility.UrlDecode (clientData[1]);
1746                         if (sortExpression == "") sortExpression = null;
1747                         RequireBinding ();
1748                         
1749                         RaisePostBackEvent (clientData[2]);
1750                         EnsureDataBound ();
1751                         
1752                         StringWriter sw = new StringWriter ();
1753                         sw.Write (PageIndex.ToString() + '|' + SortExpression + '|');
1754
1755                         HtmlTextWriter writer = new HtmlTextWriter (sw);
1756                         RenderGrid (writer);
1757                         callbackResult = sw.ToString ();
1758                 }
1759                 
1760                 string ICallbackEventHandler.GetCallbackResult ()
1761                 {
1762                         return GetCallbackResult ();
1763                 }
1764
1765                 protected virtual string GetCallbackResult ()
1766                 {
1767                         return callbackResult;
1768                 }
1769
1770                 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
1771                 {
1772                         return GetCallbackScript (control, argument);
1773                 }
1774                 
1775                 protected virtual string GetCallbackScript (IButtonControl control, string argument)
1776                 {
1777                         if (EnableSortingAndPagingCallbacks)
1778                                 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
1779                         else
1780                                 return null;
1781                 }
1782                 
1783                 [MonoTODO]
1784                 protected override void OnPagePreLoad (object sender, EventArgs e)
1785                 {
1786                         base.OnPagePreLoad (sender, e);
1787                 }
1788                 
1789                 protected internal override void OnPreRender (EventArgs e)
1790                 {
1791                         base.OnPreRender (e);
1792                         
1793                         if (EnableSortingAndPagingCallbacks)
1794                         {
1795                                 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
1796                                         string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
1797                                         Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
1798                                 }
1799                                 
1800                                 string cgrid = ClientID + "_data";
1801                                 string script = string.Format ("var {0} = new Object ();\n", cgrid);
1802                                 script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
1803                                 script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
1804                                 script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
1805                                 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
1806                                 
1807                                 // Make sure the basic script infrastructure is rendered
1808                                 Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
1809                                 Page.ClientScript.GetPostBackClientHyperlink (this, "");
1810                         }
1811                 }
1812
1813                 protected internal override void Render (HtmlTextWriter writer)
1814                 {
1815                         if (EnableSortingAndPagingCallbacks)
1816                                 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
1817                         writer.RenderBeginTag (HtmlTextWriterTag.Div);
1818
1819                         RenderGrid (writer);
1820
1821                         writer.RenderEndTag ();
1822                 }
1823                 
1824                 void RenderGrid (HtmlTextWriter writer)
1825                 {
1826                         if (table == null)
1827                                 return;
1828
1829                         table.Caption = Caption;
1830                         table.CaptionAlign = CaptionAlign;
1831                         table.RenderBeginTag (writer);
1832                         
1833                         foreach (GridViewRow row in table.Rows)
1834                         {
1835                                 switch (row.RowType) {
1836                                 case DataControlRowType.Header:
1837                                         if (!ShowHeader) continue;
1838                                         if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1839                                         break;
1840                                 case DataControlRowType.Footer:
1841                                         if (!ShowFooter) continue;
1842                                         if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1843                                         break;
1844                                 case DataControlRowType.Pager:
1845                                         if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1846                                         break;
1847                                 case DataControlRowType.EmptyDataRow:
1848                                         if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1849                                         break;
1850                                 default:
1851                                         break;
1852                                 }
1853
1854                                 if ((row.RowState & DataControlRowState.Normal) != 0 && rowStyle != null)
1855                                         rowStyle.AddAttributesToRender (writer, row);
1856                                 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null)
1857                                         alternatingRowStyle.AddAttributesToRender (writer, row);
1858                                 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1859                                         editRowStyle.AddAttributesToRender (writer, row);
1860                                 if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null)
1861                                         selectedRowStyle.AddAttributesToRender (writer, row);
1862                                 
1863                                 row.RenderBeginTag (writer);
1864                                 
1865                                 foreach (TableCell cell in row.Cells) {
1866                                         DataControlFieldCell fcell = cell as DataControlFieldCell;
1867                                         if (fcell != null) {
1868                                                 Style cellStyle = null;
1869                                                 switch (row.RowType) {
1870                                                 case DataControlRowType.Header: cellStyle = fcell.ContainingField.HeaderStyle; break;
1871                                                 case DataControlRowType.Footer: cellStyle = fcell.ContainingField.FooterStyle; break;
1872                                                 default: cellStyle = fcell.ContainingField.ItemStyle; break;
1873                                                 }
1874                                                 if (cellStyle != null)
1875                                                         cellStyle.AddAttributesToRender (writer, cell);
1876                                         }
1877                                         cell.Render (writer);
1878                                 }
1879                                 row.RenderEndTag (writer);
1880                         }
1881                         table.RenderEndTag (writer);
1882                 }
1883
1884                 [MonoTODO]
1885                 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
1886                 {
1887                         PostBackOptions pbo = new PostBackOptions (this, control.CommandName + "$" + control.CommandArgument);
1888                         return pbo;
1889                 }
1890         }
1891 }
1892
1893 #endif