Add portal support
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataGrid.cs
1 //
2 // System.Web.UI.WebControls.DataGrid.cs
3 //
4 // Authors:
5 //      Jackson Harper (jackson@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Web.Util;
31 using System.Collections;
32 using System.Globalization;
33 using System.ComponentModel;
34 using System.Reflection;
35 using System.Security.Permissions;
36
37 namespace System.Web.UI.WebControls {
38
39         // CAS
40         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         // attributes
43         [Editor("System.Web.UI.Design.WebControls.DataGridComponentEditor, " + Consts.AssemblySystem_Design, typeof(System.ComponentModel.ComponentEditor))]
44         [Designer("System.Web.UI.Design.WebControls.DataGridDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45         public class DataGrid : BaseDataList, INamingContainer {
46
47                 public const string CancelCommandName = "Cancel";
48                 public const string DeleteCommandName = "Delete";
49                 public const string EditCommandName = "Edit";
50                 public const string SelectCommandName = "Select";
51                 public const string SortCommandName = "Sort";
52                 public const string UpdateCommandName = "Update";
53
54                 public const string PageCommandName = "Page";
55                 public const string NextPageCommandArgument = "Next";
56                 public const string PrevPageCommandArgument = "Prev";
57
58                 private static readonly object CancelCommandEvent = new object ();
59                 private static readonly object DeleteCommandEvent = new object ();
60                 private static readonly object EditCommandEvent = new object ();
61                 private static readonly object ItemCommandEvent = new object ();
62                 private static readonly object ItemCreatedEvent = new object ();
63                 private static readonly object ItemDataBoundEvent = new object ();
64                 private static readonly object PageIndexChangedEvent = new object ();
65                 private static readonly object SortCommandEvent = new object ();
66                 private static readonly object UpdateCommandEvent = new object ();
67
68                 private TableItemStyle alt_item_style;
69                 private TableItemStyle edit_item_style;
70                 private TableItemStyle footer_style;
71                 private TableItemStyle header_style;
72                 private TableItemStyle item_style;
73                 private TableItemStyle selected_style;
74                 private DataGridPagerStyle pager_style;
75                 
76                 private ArrayList items_list;
77                 private DataGridItemCollection items;
78
79                 private ArrayList columns_list;
80                 private DataGridColumnCollection columns;
81
82                 private ArrayList data_source_columns_list;
83                 private DataGridColumnCollection data_source_columns;
84
85                 private Table render_table;
86                 private DataGridColumn [] render_columns;
87                 private PagedDataSource paged_data_source;
88                 private IEnumerator data_enumerator;
89                 
90                 [DefaultValue(false)]
91                 [WebSysDescription ("")]
92                 [WebCategory ("Paging")]
93                 public virtual bool AllowCustomPaging {
94                         get { return ViewState.GetBool ("AllowCustomPaging", false); }
95                         set { ViewState ["AllowCustomPaging"] = value; }
96                 }
97
98                 [DefaultValue(false)]
99                 [WebSysDescription ("")]
100                 [WebCategory ("Paging")]
101                 public virtual bool AllowPaging {
102                         get { return ViewState.GetBool ("AllowPaging", false); }
103                         set { ViewState ["AllowPaging"] = value; }
104                 }
105
106                 [DefaultValue(false)]
107                 [WebSysDescription ("")]
108                 [WebCategory ("Behavior")]
109                 public virtual bool AllowSorting {
110                         get { return ViewState.GetBool ("AllowSorting", false); }
111                         set { ViewState ["AllowSorting"] = value; }
112                 }
113
114                 [DefaultValue(true)]
115                 [WebSysDescription ("")]
116                 [WebCategory ("Behavior")]
117                 public virtual bool AutoGenerateColumns {
118                         get { return ViewState.GetBool ("AutoGenerateColumns", true); }
119                         set { ViewState ["AutoGenerateColumns"] = value; }
120                 }
121
122 #if NET_2_0
123                 [UrlProperty]
124 #else
125                 [Bindable(true)]
126 #endif
127                 [DefaultValue("")]
128                 [Editor("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
129                 [WebSysDescription ("")]
130                 [WebCategory ("Appearance")]
131                 public virtual string BackImageUrl {
132                         get { return TableStyle.BackImageUrl; }
133                         set { TableStyle.BackImageUrl = value; }
134                 }
135
136 #if ONLY_1_1
137                 [Bindable(true)]
138 #endif
139                 [Browsable(false)]
140                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
141                 [WebSysDescription ("")]
142                 [WebCategory ("Behavior")]
143                 public int CurrentPageIndex {
144                         get { return ViewState.GetInt ("CurrentPageIndex", 0); }
145                         set {
146                                 if (value < 0)
147                                         throw new ArgumentOutOfRangeException ("value");
148                                 ViewState ["CurrentPageIndex"] = value;
149                         }
150                 }
151
152                 [DefaultValue(-1)]
153                 [WebSysDescription ("")]
154                 [WebCategory ("Misc")]
155                 public virtual int EditItemIndex {
156                         get { return ViewState.GetInt ("EditItemIndex", -1); }
157                         set {
158                                 if (value < -1)
159                                         throw new ArgumentOutOfRangeException ("value");
160                                 ViewState ["EditItemIndex"] = value;
161                         }
162                 }
163
164                 [Browsable(false)]
165                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
166                 [WebSysDescription ("")]
167                 [WebCategory ("Style")]
168                 public int PageCount {
169                         get {
170                                 if (paged_data_source != null)
171                                         return paged_data_source.PageCount;
172
173                                 return ViewState.GetInt ("PageCount", 0);
174                         }
175                 }
176
177                 [DefaultValue(10)]
178                 [WebSysDescription ("")]
179                 [WebCategory ("Paging")]
180                 public virtual int PageSize {
181                         get { return ViewState.GetInt ("PageSize", 10); }
182                         set {
183                                 if (value < 1)
184                                         throw new ArgumentOutOfRangeException ("value");
185                                 ViewState ["PageSize"] = value;
186                         }
187                 }
188
189                 void AdjustItemTypes (int prev_select, int new_select)
190                 {
191                         if (items_list == null)
192                                 return; // nothing to select
193
194                         int count = items_list.Count;
195                         if (count == 0)
196                                 return; // nothing to select
197
198                         DataGridItem item;
199                         // Restore item type for the previously selected one.
200                         if (prev_select >= 0 && prev_select < count) {
201                                 item = (DataGridItem) items_list [prev_select];
202                                 
203                                 if (item.ItemType == ListItemType.EditItem) {
204                                         // nothing to do. This has priority.
205                                 } else if ((item.ItemIndex % 2) != 0) {
206                                         item.SetItemType (ListItemType.AlternatingItem);
207                                 } else {
208                                         item.SetItemType (ListItemType.Item);
209                                 }
210                         }
211
212                         if (new_select == -1 || new_select >= count)
213                                 return; // nothing to select
214
215                         item = (DataGridItem) items_list [new_select];
216                         if (item.ItemType != ListItemType.EditItem) // EditItem takes precedence
217                                 item.SetItemType (ListItemType.SelectedItem);
218                 }
219
220                 [Bindable(true)]
221                 [DefaultValue(-1)]
222                 [WebSysDescription ("")]
223                 [WebCategory ("Paging")]
224                 public virtual int SelectedIndex {
225                         get { return ViewState.GetInt ("SelectedIndex", -1); }
226                         set {
227                                 if (value < -1)
228                                         throw new ArgumentOutOfRangeException ("value");
229
230                                 int selected_index = ViewState.GetInt ("SelectedIndex", -1);
231                                 AdjustItemTypes (selected_index, value);
232                                 ViewState ["SelectedIndex"] = value;
233                         }
234                 }
235
236                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
237                 [NotifyParentProperty(true)]
238                 [PersistenceMode(PersistenceMode.InnerProperty)]
239                 [WebSysDescription ("")]
240                 [WebCategory ("Style")]
241                 public virtual TableItemStyle AlternatingItemStyle {
242                         get {
243                                 if (alt_item_style == null) {
244                                         alt_item_style = new TableItemStyle ();
245                                         if (IsTrackingViewState)
246                                                 alt_item_style.TrackViewState ();
247                                 }
248                                 return alt_item_style;
249                         }
250                 }
251
252                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
253                 [NotifyParentProperty(true)]
254                 [PersistenceMode(PersistenceMode.InnerProperty)]
255                 [WebSysDescription ("")]
256                 [WebCategory ("Style")]
257                 public virtual TableItemStyle EditItemStyle {
258                         get {
259                                 if (edit_item_style == null) {
260                                         edit_item_style = new TableItemStyle ();
261                                         if (IsTrackingViewState)
262                                                 edit_item_style.TrackViewState ();
263                                 }
264                                 return edit_item_style;
265                         }
266                 }
267
268                 [DefaultValue(null)]
269                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
270                 [NotifyParentProperty(true)]
271                 [PersistenceMode(PersistenceMode.InnerProperty)]
272                 [WebSysDescription ("")]
273                 [WebCategory ("Style")]
274                 public virtual TableItemStyle FooterStyle {
275                         get {
276                                 if (footer_style == null) {
277                                         footer_style = new TableItemStyle ();
278                                         if (IsTrackingViewState)
279                                                 footer_style.TrackViewState ();
280                                 }
281                                 return footer_style;
282                         }
283                 }
284
285                 [DefaultValue(null)]
286                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
287                 [NotifyParentProperty(true)]
288                 [PersistenceMode(PersistenceMode.InnerProperty)]
289                 [WebSysDescription ("")]
290                 [WebCategory ("Style")]
291                 public virtual TableItemStyle HeaderStyle {
292                         get {
293                                 if (header_style == null) {
294                                         header_style = new TableItemStyle ();
295                                         if (IsTrackingViewState)
296                                                 header_style.TrackViewState ();
297                                 }
298                                 return header_style;
299                         }
300                 }
301
302                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
303                 [NotifyParentProperty(true)]
304                 [PersistenceMode(PersistenceMode.InnerProperty)]
305                 [WebSysDescription ("")]
306                 [WebCategory ("Style")]
307                 public virtual TableItemStyle ItemStyle {
308                         get {
309                                 if (item_style == null) {
310                                         item_style = new TableItemStyle ();
311                                         if (IsTrackingViewState)
312                                                 item_style.TrackViewState ();
313                                 }
314                                 return item_style;
315                         }
316                 }
317
318                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
319                 [NotifyParentProperty(true)]
320                 [PersistenceMode(PersistenceMode.InnerProperty)]
321                 [WebSysDescription ("")]
322                 [WebCategory ("Style")]
323                 public virtual TableItemStyle SelectedItemStyle {
324                         get {
325                                 if (selected_style == null) {
326                                         selected_style = new TableItemStyle ();
327                                         if (IsTrackingViewState)
328                                                 selected_style.TrackViewState ();
329                                 }
330                                 return selected_style;
331                         }
332                 }
333
334                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
335                 [NotifyParentProperty(true)]
336                 [PersistenceMode(PersistenceMode.InnerProperty)]
337                 [WebSysDescription ("")]
338                 [WebCategory ("Style")]
339                 public virtual DataGridPagerStyle PagerStyle {
340                         get {
341                                 if (pager_style == null) {
342                                         pager_style = new DataGridPagerStyle ();
343                                         if (IsTrackingViewState)
344                                                 pager_style.TrackViewState ();
345                                 }
346                                 return pager_style;
347                         }
348                 }
349
350                 [Browsable(false)]
351                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
352                 [WebSysDescription ("")]
353                 [WebCategory ("Style")]
354                 public virtual DataGridItemCollection Items {
355                         get {
356                                 if (items == null) {
357                                         if (items_list == null)
358                                                 items_list = new ArrayList ();
359                                         items = new DataGridItemCollection (items_list);
360                                 }
361                                 return items;
362                         }
363                 }
364
365                 [DefaultValue (null)]
366                 [Editor ("System.Web.UI.Design.WebControls.DataGridColumnCollectionEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
367                 [MergableProperty (false)]
368                 [PersistenceMode (PersistenceMode.InnerProperty)]
369                 [WebSysDescription ("")]
370                 [WebCategory ("Behavior")]
371                 public virtual DataGridColumnCollection Columns {
372                         get {
373                                 if (columns == null) {
374                                         columns_list = new ArrayList ();
375                                         columns = new DataGridColumnCollection (this, columns_list);
376                                         if (IsTrackingViewState) {
377                                                 IStateManager manager = (IStateManager) columns;
378                                                 manager.TrackViewState ();
379                                         }
380                                 }
381                                 return columns;
382                         }
383                 }
384
385                 private DataGridColumnCollection DataSourceColumns {
386                         get {
387                                 if (data_source_columns == null) {
388                                         data_source_columns_list = new ArrayList ();
389                                         data_source_columns = new DataGridColumnCollection (this,
390                                                         data_source_columns_list);
391                                         if (IsTrackingViewState) {
392                                                 IStateManager manager = (IStateManager) data_source_columns;
393                                                 manager.TrackViewState ();
394                                         }
395                                 }
396                                 return data_source_columns;
397                         }
398                 }
399
400                 class TableID : Table {
401                         Control parent;
402
403                         public TableID (Control parent)
404                         {
405                                 this.parent = parent;
406                         }
407
408                         protected override void AddAttributesToRender (HtmlTextWriter writer)
409                         {
410                                 base.AddAttributesToRender (writer);
411                                 if (ID == null)
412                                         writer.AddAttribute ("id", parent.ClientID);
413                         }
414                 }
415
416                 private Table RenderTable {
417                         get {
418                                 if (render_table == null) {
419                                         render_table = new TableID (this);
420                                         render_table.AutoID = false;
421                                 }
422                                 return render_table;
423                         }
424                 }
425
426                 private void CreateRenderColumns (PagedDataSource paged, bool useDataSource)
427                 {
428                         if (useDataSource) {
429                                 ArrayList columns_list = CreateColumnSet (paged, useDataSource);
430                                 render_columns = new DataGridColumn [columns_list.Count];
431
432                                 for (int c = 0; c < render_columns.Length; c++) {
433                                         DataGridColumn col = (DataGridColumn) columns_list [c];
434                                         col.Set_Owner (this);
435                                         col.Initialize ();
436                                         render_columns [c] = col;
437                                 }
438                         } else {
439                                 render_columns = new DataGridColumn [Columns.Count];
440                                 Columns.CopyTo (render_columns, 0);
441                         }
442                 }
443
444                 [Browsable(false)]
445                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
446                 [WebSysDescription ("")]
447                 [WebCategory ("Paging")]
448                 public virtual DataGridItem SelectedItem {
449                         get {
450                                 if (SelectedIndex == -1)
451                                         return null;
452                                 return Items [SelectedIndex];
453                         }
454                 }
455
456 #if ONLY_1_1
457                 [Bindable(true)]
458 #endif
459                 [DefaultValue(false)]
460                 [WebSysDescription ("")]
461                 [WebCategory ("Appearance")]
462                 public virtual bool ShowFooter {
463                         get { return ViewState.GetBool ("ShowFooter", false); }
464                         set { ViewState ["ShowFooter"] = value; }
465                 }
466
467 #if ONLY_1_1
468                 [Bindable(true)]
469 #endif
470                 [DefaultValue(true)]
471                 [WebSysDescription ("")]
472                 [WebCategory ("Appearance")]
473                 public virtual bool ShowHeader {
474                         get { return ViewState.GetBool ("ShowHeader", true); }
475                         set { ViewState ["ShowHeader"] = value; }
476                 }
477
478                 [Browsable(false)]
479                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
480                 [WebSysDescription ("")]
481                 [WebCategory ("Appearance")]
482                 public virtual int VirtualItemCount {
483                         get { return ViewState.GetInt ("VirtualItemCount", 0); }
484                         set {
485                                 if (value < 0)
486                                         throw new ArgumentOutOfRangeException ("value");
487                                 ViewState ["VirtualItemCount"] = value;
488                         }
489                 }
490
491 #if NET_2_0
492                 protected override HtmlTextWriterTag TagKey {
493                         get { return HtmlTextWriterTag.Table; }
494                 }
495 #endif
496
497                 private TableStyle TableStyle {
498                         get { return (TableStyle) ControlStyle; }
499                 }
500
501                 static Type [] item_args = new Type [] {typeof (int) };
502                 void AddColumnsFromSource (PagedDataSource data_source)
503                 {
504                         PropertyDescriptorCollection props = null;
505                         Type ptype = null;
506                         bool no_items = false;
507
508                         // Use plain reflection for the Item property.
509                         // If we use TypeDescriptor, props will hold
510                         // all of the Type properties, which will be listed as columns
511                         Type ds_type = data_source.GetType ();
512                         PropertyInfo pinfo = ds_type.GetProperty ("Item", item_args);
513                         if (pinfo == null) {
514                                 IEnumerator items = (data_source.DataSource != null) ? data_source.GetEnumerator () : null;
515                                 if (items != null && items.MoveNext ()) {
516                                         object data = items.Current;
517                                         if ((data is ICustomTypeDescriptor) || (!IsBindableType(data.GetType())))
518                                                 props = TypeDescriptor.GetProperties (data);
519                                         else if (data != null)
520                                                 ptype = data.GetType ();
521                                         data_enumerator = items;
522                                 } else {
523                                         no_items = true;
524                                 }
525                         } else {
526                                 ptype = pinfo.PropertyType;
527                         }
528
529                         if (ptype != null) {
530                                 // Found the "Item" property
531                                 AddPropertyToColumns ();
532                         } else if (props != null) {
533                                 foreach (PropertyDescriptor pd in props)
534                                         AddPropertyToColumns (pd, false);
535                         } else if (!no_items) {
536                                 // This is not thrown for an empty ArrayList.
537                                 string msg = String.Format ("DataGrid '{0}' cannot autogenerate " +
538                                                         "columns from the given datasource. {1}", ID, ptype);
539                                 throw new HttpException (msg);
540                         }
541                 }
542
543                 protected virtual ArrayList CreateColumnSet (PagedDataSource dataSource, bool useDataSource)
544                 {
545                         ArrayList res = new ArrayList ();
546                         if (columns_list != null)
547                                 res.AddRange (columns_list);
548
549                         if (AutoGenerateColumns) {
550                                 if (useDataSource) {
551                                         data_enumerator = null;
552                                         PropertyDescriptorCollection props = dataSource.GetItemProperties (null);
553                                         DataSourceColumns.Clear ();
554                                         if (props != null) {
555                                                 foreach (PropertyDescriptor d in props)
556                                                         AddPropertyToColumns (d, false);
557                                         } else {
558                                                 AddColumnsFromSource (dataSource);
559                                         }
560                                 }
561
562                                 if (data_source_columns != null && data_source_columns.Count > 0)
563                                         res.AddRange (data_source_columns);
564                         }
565
566                         return res;
567                 }
568
569                 private void AddPropertyToColumns ()
570                 {
571                         BoundColumn b = new BoundColumn ();
572                         if (IsTrackingViewState) {
573                                 IStateManager m = (IStateManager) b;
574                                 m.TrackViewState ();
575                         }
576                         b.Set_Owner (this);
577                         b.HeaderText = "Item";
578                         b.SortExpression = "Item";
579                         b.DataField  = BoundColumn.thisExpr;
580                         DataSourceColumns.Add (b);
581                 }
582
583                 private void AddPropertyToColumns (PropertyDescriptor prop, bool tothis)
584                 {
585                         BoundColumn b = new BoundColumn ();
586                         b.Set_Owner (this);
587                         if (IsTrackingViewState) {
588                                 IStateManager m = (IStateManager) b;
589                                 m.TrackViewState ();
590                         }
591                         b.HeaderText = prop.Name;
592                         b.DataField = (tothis ? BoundColumn.thisExpr : prop.Name);
593                         b.SortExpression = prop.Name;
594                         DataSourceColumns.Add (b);
595                 }
596
597                 protected override void TrackViewState ()
598                 {
599                         base.TrackViewState ();
600
601                         if (pager_style != null)
602                                 pager_style.TrackViewState ();
603                         if (header_style != null)
604                                 header_style.TrackViewState ();
605                         if (footer_style != null)
606                                 footer_style.TrackViewState ();
607                         if (item_style != null)
608                                 item_style.TrackViewState ();
609                         if (alt_item_style != null)
610                                 alt_item_style.TrackViewState ();
611                         if (selected_style != null)
612                                 selected_style.TrackViewState ();
613                         if (edit_item_style != null)
614                                 edit_item_style.TrackViewState ();
615
616                         IStateManager manager = (IStateManager) columns;
617                         if (manager != null)
618                                 manager.TrackViewState ();
619                 }
620
621                 protected override object SaveViewState ()
622                 {
623                         object [] res = new object [10];
624
625                         res [0] = base.SaveViewState ();
626                         if (columns != null) {
627                                 IStateManager cm = (IStateManager) columns;
628                                 res [1] = cm.SaveViewState ();
629                         }
630                         if (pager_style != null)
631                                 res [2] = pager_style.SaveViewState ();
632                         if (header_style != null)
633                                 res [3] = header_style.SaveViewState ();
634                         if (footer_style != null)
635                                 res [4] = footer_style.SaveViewState ();
636                         if (item_style != null)
637                                 res [5] = item_style.SaveViewState ();
638                         if (alt_item_style != null)
639                                 res [6] = alt_item_style.SaveViewState ();
640                         if (selected_style != null)
641                                 res [7] = selected_style.SaveViewState ();
642                         if (edit_item_style != null)
643                                 res [8] = edit_item_style.SaveViewState ();
644
645                         if (data_source_columns != null) {
646                                 IStateManager m = (IStateManager) data_source_columns;
647                                 res [9] = m.SaveViewState ();
648                         }
649                         
650                         return res;
651                 }
652
653                 protected override void LoadViewState (object savedState)
654                 {
655                         object [] pieces = savedState as object [];
656
657                         if (pieces == null)
658                                 return;
659
660                         base.LoadViewState (pieces [0]);
661                         if (columns != null) {
662                                 IStateManager cm = (IStateManager) columns;
663                                 cm.LoadViewState (pieces [1]);
664                         }
665                         if (pieces [2] != null)
666                                 PagerStyle.LoadViewState (pieces [2]);
667                         if (pieces [3] != null)
668                                 HeaderStyle.LoadViewState (pieces [3]);
669                         if (pieces [4] != null)
670                                 FooterStyle.LoadViewState (pieces [4]);
671                         if (pieces [5] != null)
672                                 ItemStyle.LoadViewState (pieces [5]);
673                         if (pieces [6] != null)
674                                 AlternatingItemStyle.LoadViewState (pieces [6]);
675                         if (pieces [7] != null)
676                                 SelectedItemStyle.LoadViewState (pieces [7]);
677                         if (pieces [8] != null)
678                                 EditItemStyle.LoadViewState (pieces [8]);
679
680                         if (pieces [9] != null) {
681                                 // IStateManager manager = (IStateManager) DataSourceColumns;
682                                 // manager.LoadViewState (pieces [9]);
683                                 object [] cols = (object []) pieces [9];
684                                 foreach (object o in cols) {
685                                         BoundColumn c = new BoundColumn ();
686                                         c.Set_Owner (this);
687                                         ((IStateManager) c).LoadViewState (o);
688                                         DataSourceColumns.Add (c);
689                                 }
690                         }
691                 }
692
693                 protected override Style CreateControlStyle ()
694                 {
695                         TableStyle res = new TableStyle (ViewState);
696                         res.GridLines = GridLines.Both;
697                         res.CellSpacing = 0;
698                         return res;
699                 }
700
701                 protected virtual void InitializeItem (DataGridItem item, DataGridColumn [] columns)
702                 {
703                         bool th = UseAccessibleHeader && item.ItemType == ListItemType.Header;
704                         for (int i = 0; i < columns.Length; i++) {
705                                 TableCell cell = null;
706                                 if (th) {
707                                         cell = new TableHeaderCell ();
708                                         cell.Attributes["scope"] = "col";
709                                 }
710                                 else
711                                         cell = new TableCell ();
712                                 columns [i].InitializeCell (cell, i, item.ItemType);
713                                 item.Cells.Add (cell);
714                         }
715                 }
716
717                 protected virtual void InitializePager (DataGridItem item, int columnSpan, PagedDataSource pagedDataSource)
718                 {
719                         TableCell pager_cell;
720                         if (PagerStyle.Mode == PagerMode.NextPrev)
721                                 pager_cell = InitializeNextPrevPager (item, columnSpan, pagedDataSource);
722                         else
723                                 pager_cell = InitializeNumericPager (item, columnSpan, pagedDataSource);
724
725                         item.Controls.Add (pager_cell);
726                 }
727
728                 private TableCell InitializeNumericPager (DataGridItem item, int columnSpan,
729                                 PagedDataSource paged)
730                 {
731                         TableCell res = new TableCell ();
732                         res.ColumnSpan = columnSpan;
733
734                         int button_count = PagerStyle.PageButtonCount;
735                         int current = paged.CurrentPageIndex;
736                         int start = current - (current % button_count);
737                         int end = start + button_count;
738
739                         if (end > paged.PageCount)
740                                 end = paged.PageCount;
741
742                         if (start > 0) {
743                                 LinkButton link = new LinkButton ();
744                                 link.Text = "...";
745                                 link.CommandName = PageCommandName;
746                                 link.CommandArgument = start.ToString (CultureInfo.InvariantCulture);
747                                 link.CausesValidation = false;
748                                 res.Controls.Add (link);
749                                 res.Controls.Add (new LiteralControl ("&nbsp;"));
750                         }
751
752                         for (int i = start; i < end; i++) {
753                                 Control number = null;
754                                 string page = (i + 1).ToString (CultureInfo.InvariantCulture);
755                                 if (i != paged.CurrentPageIndex) {
756                                         LinkButton link = new LinkButton ();
757                                         link.Text = page;
758                                         link.CommandName = PageCommandName;
759                                         link.CommandArgument = page;
760                                         link.CausesValidation = false;
761                                         number = link;
762                                 } else {
763                                         Label pageLabel = new Label();
764                                         pageLabel.Text = page;
765                                         number = pageLabel;
766                                 }
767
768                                 res.Controls.Add (number);
769                                 if (i < end - 1)
770                                         res.Controls.Add (new LiteralControl ("&nbsp;"));
771                         }
772
773                         if (end < paged.PageCount) {
774                                 res.Controls.Add (new LiteralControl ("&nbsp;"));
775                                 LinkButton link = new LinkButton ();
776                                 link.Text = "...";
777                                 link.CommandName = PageCommandName;
778                                 link.CommandArgument = (end + 1).ToString (CultureInfo.InvariantCulture);
779                                 link.CausesValidation = false;
780                                 res.Controls.Add (link);
781                         }
782
783                         return res;
784                 }
785
786                 private TableCell InitializeNextPrevPager (DataGridItem item, int columnSpan, PagedDataSource paged)
787                 {
788                         TableCell res = new TableCell ();
789                         res.ColumnSpan = columnSpan;
790
791                         Control prev;
792                         Control next;
793
794                         if (paged.IsFirstPage) {
795                                 Label l = new Label ();
796                                 l.Text = PagerStyle.PrevPageText;
797                                 prev = l;
798                         } else {
799                                 LinkButton l = new LinkButton ();
800                                 l.Text = PagerStyle.PrevPageText;
801                                 l.CommandName = PageCommandName;
802                                 l.CommandArgument = PrevPageCommandArgument;
803                                 l.CausesValidation = false;
804                                 prev = l;
805                         }
806
807                         if (paged.Count > 0 && !paged.IsLastPage) {
808                                 LinkButton l = new LinkButton ();
809                                 l.Text = PagerStyle.NextPageText;
810                                 l.CommandName = PageCommandName;
811                                 l.CommandArgument = NextPageCommandArgument;
812                                 l.CausesValidation = false;
813                                 next = l;
814                         } else {
815                                 Label l = new Label ();
816                                 l.Text = PagerStyle.NextPageText;
817                                 next = l;
818                         }
819
820                         res.Controls.Add (prev);
821                         res.Controls.Add (new LiteralControl ("&nbsp;"));
822                         res.Controls.Add (next);
823
824                         return res;
825                 }
826                                 
827                 protected virtual DataGridItem CreateItem (int itemIndex, int dataSourceIndex,
828                                 ListItemType itemType)
829                 {
830                         DataGridItem res = new DataGridItem (itemIndex, dataSourceIndex, itemType);
831                         return res;
832                 }
833
834                 private DataGridItem CreateItem (int item_index, int data_source_index,
835                                 ListItemType type, bool data_bind, object data_item,
836                                 PagedDataSource paged)
837                 {
838                         DataGridItem res = CreateItem (item_index, data_source_index, type);
839                         DataGridItemEventArgs args = new DataGridItemEventArgs (res);
840                         bool no_pager = (type != ListItemType.Pager);
841
842                         if (no_pager) {
843                                 InitializeItem (res, render_columns);
844                                 if (data_bind)
845                                         res.DataItem = data_item;
846                                 OnItemCreated (args);
847                         } else {
848                                 InitializePager (res, render_columns.Length, paged);
849                                 if (pager_style != null)
850                                         res.ApplyStyle (pager_style);
851                                 OnItemCreated (args);
852                         }
853
854                         // Add before the column is bound, so that the
855                         // value is saved in the viewstate
856                         RenderTable.Controls.Add (res);
857
858                         if (no_pager && data_bind) {
859                                 res.DataBind ();
860                                 OnItemDataBound (args);
861                                 res.DataItem = null;
862                         }
863                         return res;
864                 }
865
866                 class NCollection : ICollection {
867                         int n;
868
869                         public NCollection (int n)
870                         {
871                                 this.n = n;
872                         }
873
874                         public IEnumerator GetEnumerator ()
875                         {
876                                 for (int i = 0; i < n; i++)
877                                         yield return i;
878                         }
879
880                         public int Count {
881                                 get { return n; }
882                         }
883
884                         public bool IsSynchronized {
885                                 get { return false; }
886                         }
887
888                         public object SyncRoot {
889                                 get { return this; }
890                         }
891
892                         public void CopyTo (Array array, int index)
893                         {
894                                 throw new NotImplementedException ("This should never be called");
895                         }
896                 }
897
898                 protected override void CreateControlHierarchy (bool useDataSource)
899                 {
900                         Controls.Clear ();
901                         RenderTable.Controls.Clear ();
902
903                         IEnumerable data_source;
904                         ArrayList keys = null;
905                         if (useDataSource) {
906                                 data_source = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
907                                 keys = DataKeysArray;
908                                 keys.Clear ();
909                         } else {
910                                 int nitems = ViewState.GetInt ("Items", 0);
911                                 data_source = new NCollection (nitems);
912                         }
913
914                         paged_data_source = new PagedDataSource ();
915                         PagedDataSource pds = paged_data_source;
916                         pds.AllowPaging = AllowPaging;
917                         pds.AllowCustomPaging = AllowCustomPaging;
918                         pds.DataSource = data_source;
919                         pds.CurrentPageIndex = CurrentPageIndex;
920                         pds.PageSize = PageSize;
921                         pds.VirtualCount = VirtualItemCount;
922
923                         if ((pds.IsPagingEnabled) && (pds.PageCount < pds.CurrentPageIndex))
924                                 throw new HttpException ("Invalid DataGrid PageIndex");
925                                 
926                         CreateRenderColumns (paged_data_source, useDataSource);
927                         if (useDataSource) {
928                                 if (DataSource != null)
929                                         Controls.Add (RenderTable);
930                         } else {
931                                 Controls.Add (RenderTable);
932                         }
933
934                         if (pds.IsPagingEnabled)
935                                 CreateItem (-1, -1, ListItemType.Pager, false, null, pds);
936
937                         CreateItem (-1, -1, ListItemType.Header, useDataSource, null, pds);
938
939                         // No indexer on PagedDataSource so we have to do
940                         // this silly foreach and index++
941                         if (items_list == null)
942                                 items_list = new ArrayList ();
943                         else
944                                 items_list.Clear();
945
946                         bool skip_first = false;
947                         IEnumerator enumerator = null;
948                         if (data_enumerator != null) {
949                                 // replaced when creating bound columns
950                                 enumerator = data_enumerator;
951                                 skip_first = true;
952                         } else if (pds.DataSource != null) {
953                                 enumerator = pds.GetEnumerator ();
954                         } else {
955                                 enumerator = null;
956                         }
957
958                         int index = 0;
959                         bool first = true;
960                         string key = null;
961                         int dataset_index = pds.FirstIndexInPage;
962                         int selected_index = SelectedIndex;
963                         int edit_item_index = EditItemIndex;
964                         while (enumerator != null && (skip_first || enumerator.MoveNext ())) {
965                                 // MS does not render <table blah></table> on empty datasource.
966                                 if (first) {
967                                         first = false;
968                                         key = DataKeyField;
969                                         skip_first = false;
970                                 }
971                                 object data = enumerator.Current;
972                                 // This will throw if the DataKeyField is not there. As on MS, this
973                                 // will not be hit on an empty datasource.
974                                 // The values stored here can be used in events so that you can
975                                 // get, for example, the row that was clicked
976                                 // (data.Rows.Find (ItemsGrid.DataKeys [e.Item.ItemIndex]))
977                                 // BaseDataList will keep the array across postbacks.
978                                 if (useDataSource && key != "")
979                                         keys.Add (DataBinder.GetPropertyValue (data, key));
980
981                                 ListItemType type = ListItemType.Item;
982                                 if (index == edit_item_index) 
983                                         type = ListItemType.EditItem;
984                                 else if (index == selected_index) 
985                                         type = ListItemType.SelectedItem;
986                                 else if (index % 2 != 0) 
987                                         type = ListItemType.AlternatingItem;
988
989                                 items_list.Add (CreateItem (index, dataset_index, type, useDataSource, data, pds));
990                                 index++;
991                                 dataset_index++;
992                         }
993
994                         CreateItem (-1, -1, ListItemType.Footer, useDataSource, null, paged_data_source);
995                         if (pds.IsPagingEnabled) {
996                                 CreateItem (-1, -1, ListItemType.Pager, false, null, paged_data_source);
997                                 if (useDataSource)
998                                         ViewState ["Items"] = pds.IsCustomPagingEnabled ? index : pds.DataSourceCount;
999                         } else if (useDataSource) {
1000                                 ViewState ["Items"] = index;
1001                         }
1002                 }
1003
1004                 void ApplyColumnStyle (TableCellCollection cells, ListItemType type)
1005                 {
1006                         int ncells = Math.Min (cells.Count, render_columns.Length);
1007                         if (ncells <= 0)
1008                                 return;
1009
1010                         for (int i = 0; i < ncells; i++) {
1011                                 Style style = null;
1012                                 TableCell cell = cells [i];
1013                                 DataGridColumn column = render_columns [i];
1014                                 if (!column.Visible) {
1015                                         cell.Visible = false;
1016                                         continue;
1017                                 }
1018
1019                                 style = column.GetStyle (type);
1020                                 if (style != null)
1021                                         cell.MergeStyle (style);
1022                         }
1023                 }
1024
1025                 protected override void PrepareControlHierarchy ()
1026                 {
1027                         if (!HasControls () || Controls.Count == 0)
1028                                 return; // No one called CreateControlHierarchy() with DataSource != null
1029
1030                         Table rt = render_table;
1031                         rt.CopyBaseAttributes (this);
1032                         rt.ApplyStyle (ControlStyle);
1033
1034                         rt.Caption = Caption;
1035                         rt.CaptionAlign = CaptionAlign;
1036                         rt.Enabled = Enabled;
1037
1038                         bool top_pager = true;
1039                         Style alt = null;
1040                         foreach (DataGridItem item in rt.Rows) {
1041                                 
1042                                 switch (item.ItemType) {
1043                                 case ListItemType.Item:
1044                                         item.MergeStyle (item_style);
1045                                         ApplyColumnStyle (item.Cells, ListItemType.Item);
1046                                         break;
1047                                 case ListItemType.AlternatingItem:
1048                                         if (alt == null) {
1049                                                 if (alt_item_style != null) {
1050                                                         alt = new TableItemStyle ();
1051                                                         alt.CopyFrom (item_style);
1052                                                         alt.CopyFrom (alt_item_style);
1053                                                 } else {
1054                                                         alt = item_style;
1055                                                 }
1056                                         }
1057
1058                                         item.MergeStyle (alt);
1059                                         ApplyColumnStyle (item.Cells, ListItemType.AlternatingItem);
1060                                         break;
1061                                 case ListItemType.EditItem:
1062                                         item.MergeStyle (edit_item_style);
1063                                         ApplyColumnStyle (item.Cells, ListItemType.EditItem);
1064                                         break;
1065                                 case ListItemType.Footer:
1066                                         if (!ShowFooter) {
1067                                                 item.Visible = false;
1068                                                 break;
1069                                         }
1070                                         if (footer_style != null)
1071                                                 item.MergeStyle (footer_style);
1072                                         ApplyColumnStyle (item.Cells, ListItemType.Footer);
1073                                         break;
1074                                 case ListItemType.Header:
1075                                         if (!ShowHeader) {
1076                                                 item.Visible = false;
1077                                                 break;
1078                                         }
1079                                         if (header_style != null)
1080                                                 item.MergeStyle (header_style);
1081                                         ApplyColumnStyle (item.Cells, ListItemType.Header);
1082                                         break;
1083                                 case ListItemType.SelectedItem:
1084                                         if (selected_style != null)
1085                                                 item.MergeStyle (selected_style);
1086                                         else
1087                                                 item.MergeStyle (item_style);
1088                                         ApplyColumnStyle (item.Cells, ListItemType.SelectedItem);
1089                                         break;
1090                                 case ListItemType.Separator:
1091                                         ApplyColumnStyle (item.Cells, ListItemType.Separator);
1092                                         break;
1093                                 case ListItemType.Pager:
1094                                         DataGridPagerStyle ps = PagerStyle;
1095                                         if (ps.Visible == false || !paged_data_source.IsPagingEnabled) {
1096                                                 item.Visible = false;
1097                                         } else {
1098                                                 if (top_pager)
1099                                                         item.Visible = (ps.Position != PagerPosition.Bottom);
1100                                                 else
1101                                                         item.Visible = (ps.Position != PagerPosition.Top);
1102                                                 top_pager = false;
1103                                         }
1104
1105                                         if (item.Visible)
1106                                                 item.MergeStyle (pager_style);
1107                                         break;
1108                                 }
1109                         }
1110                 }
1111
1112                 protected override bool OnBubbleEvent (object source, EventArgs e)
1113                 {
1114                         DataGridCommandEventArgs de = e as DataGridCommandEventArgs;
1115
1116                         if (de == null)
1117                                 return false;
1118
1119                         string cn = de.CommandName;
1120                         CultureInfo inv = CultureInfo.InvariantCulture;
1121
1122                         OnItemCommand (de);
1123                         if (String.Compare (cn, CancelCommandName, true, inv) == 0) {
1124                                 OnCancelCommand (de);
1125                         } else if (String.Compare (cn, DeleteCommandName, true, inv) == 0) {
1126                                 OnDeleteCommand (de);
1127                         } else if (String.Compare (cn, EditCommandName, true, inv) == 0) {
1128                                 OnEditCommand (de);
1129                         } else if (String.Compare (cn, SelectCommandName, true, inv) == 0) {
1130                                 SelectedIndex = de.Item.ItemIndex;
1131                                 OnSelectedIndexChanged (de);
1132                         } else if (String.Compare (cn, SortCommandName, true, inv) == 0) {
1133                                 DataGridSortCommandEventArgs se = new DataGridSortCommandEventArgs (de.CommandSource, de);
1134                                 OnSortCommand (se);
1135                         } else if (String.Compare (cn, UpdateCommandName, true, inv) == 0) {
1136                                 OnUpdateCommand (de);
1137                         } else if (String.Compare (cn, PageCommandName, true, inv) == 0) {
1138                                 int new_index;
1139                                 if (String.Compare ((string) de.CommandArgument,
1140                                                     NextPageCommandArgument, true, inv) == 0) {
1141                                         new_index = CurrentPageIndex + 1;
1142                                 } else if (String.Compare ((string) de.CommandArgument,
1143                                                            PrevPageCommandArgument, true, inv) == 0) {
1144                                         new_index = CurrentPageIndex - 1;
1145                                 } else {
1146                                         // It seems to just assume it's an int and parses, no
1147                                         // checks to make sure its valid or anything.
1148                                         //  also it's always one less then specified, not sure
1149                                         // why that is.
1150                                         new_index = Int32.Parse ((string) de.CommandArgument, inv) - 1;
1151                                 }
1152                                 DataGridPageChangedEventArgs pc = new DataGridPageChangedEventArgs (
1153                                         de.CommandSource, new_index);
1154                                 OnPageIndexChanged (pc);
1155                         }
1156
1157                         return true;
1158                 }
1159
1160                 protected virtual void OnCancelCommand (DataGridCommandEventArgs e)
1161                 {
1162                         DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [CancelCommandEvent];
1163                         if (handler != null)
1164                                 handler (this, e);
1165                 }
1166
1167                 protected virtual void OnDeleteCommand (DataGridCommandEventArgs e)
1168                 {
1169                         DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [DeleteCommandEvent];
1170                         if (handler != null)
1171                                 handler (this, e);
1172                 }
1173
1174                 protected virtual void OnEditCommand (DataGridCommandEventArgs e)
1175                 {
1176                         DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [EditCommandEvent];
1177                         if (handler != null)
1178                                 handler (this, e);
1179                 }
1180
1181                 protected virtual void OnItemCommand (DataGridCommandEventArgs e)
1182                 {
1183                         DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [ItemCommandEvent];
1184                         if (handler != null)
1185                                 handler (this, e);
1186                 }
1187
1188                 protected virtual void OnItemCreated (DataGridItemEventArgs e)
1189                 {
1190                         DataGridItemEventHandler handler = (DataGridItemEventHandler) Events [ItemCreatedEvent];
1191                         if (handler != null)
1192                                 handler (this, e);
1193                 }
1194
1195                 protected virtual void OnItemDataBound (DataGridItemEventArgs e)
1196                 {
1197                         DataGridItemEventHandler handler = (DataGridItemEventHandler) Events [ItemDataBoundEvent];
1198                         if (handler != null)
1199                                 handler (this, e);
1200                 }
1201
1202                 protected virtual void OnPageIndexChanged (DataGridPageChangedEventArgs e)
1203                 {
1204                         DataGridPageChangedEventHandler handler = (DataGridPageChangedEventHandler) Events [PageIndexChangedEvent];
1205                         if (handler != null)
1206                                 handler (this, e);
1207                 }
1208
1209                 protected virtual void OnSortCommand (DataGridSortCommandEventArgs e)
1210                 {
1211                         DataGridSortCommandEventHandler handler = (DataGridSortCommandEventHandler) Events [SortCommandEvent];
1212                         if (handler != null)
1213                                 handler (this, e);
1214                 }
1215
1216                 protected virtual void OnUpdateCommand (DataGridCommandEventArgs e)
1217                 {
1218                         DataGridCommandEventHandler handler = (DataGridCommandEventHandler) Events [UpdateCommandEvent];
1219                         if (handler != null)
1220                                 handler (this, e);
1221                 }
1222
1223                 [WebSysDescription ("")]
1224                 [WebCategory ("Action")]
1225                 public event DataGridCommandEventHandler CancelCommand {
1226                         add { Events.AddHandler (CancelCommandEvent, value); }
1227                         remove { Events.RemoveHandler (CancelCommandEvent, value); }
1228                 }
1229
1230                 [WebSysDescription ("")]
1231                 [WebCategory ("Action")]
1232                 public event DataGridCommandEventHandler DeleteCommand {
1233                         add { Events.AddHandler (DeleteCommandEvent, value); }
1234                         remove { Events.RemoveHandler (DeleteCommandEvent, value); }
1235                 }
1236
1237                 [WebSysDescription ("")]
1238                 [WebCategory ("Action")]
1239                 public event DataGridCommandEventHandler EditCommand {
1240                         add { Events.AddHandler (EditCommandEvent, value); }
1241                         remove { Events.RemoveHandler (EditCommandEvent, value); }
1242                 }
1243
1244                 [WebSysDescription ("")]
1245                 [WebCategory ("Action")]
1246                 public event DataGridCommandEventHandler ItemCommand {
1247                         add { Events.AddHandler (ItemCommandEvent, value); }
1248                         remove { Events.RemoveHandler (ItemCommandEvent, value); }
1249                         
1250                 }
1251
1252                 [WebSysDescription ("")]
1253                 [WebCategory ("Action")]
1254                 public event DataGridItemEventHandler ItemCreated {
1255                         add { Events.AddHandler (ItemCreatedEvent, value); }
1256                         remove { Events.RemoveHandler (ItemCreatedEvent, value); }
1257                 }
1258
1259                 [WebSysDescription ("")]
1260                 [WebCategory ("Action")]
1261                 public event DataGridItemEventHandler ItemDataBound {
1262                         add { Events.AddHandler (ItemDataBoundEvent, value); }
1263                         remove { Events.RemoveHandler (ItemDataBoundEvent, value); }
1264                 }
1265
1266                 [WebSysDescription ("")]
1267                 [WebCategory ("Action")]
1268                 public event DataGridPageChangedEventHandler PageIndexChanged {
1269                         add { Events.AddHandler (PageIndexChangedEvent, value); }
1270                         remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
1271                 }
1272
1273                 [WebSysDescription ("")]
1274                 [WebCategory ("Action")]
1275                 public event DataGridSortCommandEventHandler SortCommand {
1276                         add { Events.AddHandler (SortCommandEvent, value); }
1277                         remove { Events.RemoveHandler (SortCommandEvent, value); }
1278                 }
1279
1280                 [WebSysDescription ("")]
1281                 [WebCategory ("Action")]
1282                 public event DataGridCommandEventHandler UpdateCommand {
1283                         add { Events.AddHandler (UpdateCommandEvent, value); }
1284                         remove { Events.AddHandler (UpdateCommandEvent, value); }
1285                 }
1286         }
1287 }
1288