2 // System.Web.UI.WebControls.GridView.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.ComponentModel;
36 using System.Security.Permissions;
39 using System.Reflection;
41 namespace System.Web.UI.WebControls
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
52 GridViewRowCollection rows;
53 GridViewRow bottomPagerRow;
54 GridViewRow topPagerRow;
56 IOrderedDictionary currentEditRowKeys;
57 IOrderedDictionary currentEditNewValues;
58 IOrderedDictionary currentEditOldValues;
60 ITemplate pagerTemplate;
61 ITemplate emptyDataTemplate;
63 PropertyDescriptor[] cachedKeyProperties;
66 DataControlFieldCollection columns;
67 PagerSettings pagerSettings;
69 TableItemStyle alternatingRowStyle;
70 TableItemStyle editRowStyle;
71 TableItemStyle emptyDataRowStyle;
72 TableItemStyle footerStyle;
73 TableItemStyle headerStyle;
74 TableItemStyle pagerStyle;
75 TableItemStyle rowStyle;
76 TableItemStyle selectedRowStyle;
77 ArrayList _dataKeyArrayList;
79 DataKey oldEditValues;
80 AutoGeneratedFieldProperties[] autoFieldProperties;
81 string [] dataKeyNames = null;
82 readonly string[] emptyKeys = new string[0];
83 IEnumerator _dataEnumerator;
85 private static readonly object PageIndexChangedEvent = new object();
86 private static readonly object PageIndexChangingEvent = new object();
87 private static readonly object RowCancelingEditEvent = new object();
88 private static readonly object RowCommandEvent = new object();
89 private static readonly object RowCreatedEvent = new object();
90 private static readonly object RowDataBoundEvent = new object();
91 private static readonly object RowDeletedEvent = new object();
92 private static readonly object RowDeletingEvent = new object();
93 private static readonly object RowEditingEvent = new object();
94 private static readonly object RowUpdatedEvent = new object();
95 private static readonly object RowUpdatingEvent = new object();
96 private static readonly object SelectedIndexChangedEvent = new object();
97 private static readonly object SelectedIndexChangingEvent = new object();
98 private static readonly object SortedEvent = new object();
99 private static readonly object SortingEvent = new object();
103 int selectedIndex = -1;
105 SortDirection sortDirection = SortDirection.Ascending;
106 string sortExpression;
112 public event EventHandler PageIndexChanged {
113 add { Events.AddHandler (PageIndexChangedEvent, value); }
114 remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
117 public event GridViewPageEventHandler PageIndexChanging {
118 add { Events.AddHandler (PageIndexChangingEvent, value); }
119 remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
122 public event GridViewCancelEditEventHandler RowCancelingEdit {
123 add { Events.AddHandler (RowCancelingEditEvent, value); }
124 remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
127 public event GridViewCommandEventHandler RowCommand {
128 add { Events.AddHandler (RowCommandEvent, value); }
129 remove { Events.RemoveHandler (RowCommandEvent, value); }
132 public event GridViewRowEventHandler RowCreated {
133 add { Events.AddHandler (RowCreatedEvent, value); }
134 remove { Events.RemoveHandler (RowCreatedEvent, value); }
137 public event GridViewRowEventHandler RowDataBound {
138 add { Events.AddHandler (RowDataBoundEvent, value); }
139 remove { Events.RemoveHandler (RowDataBoundEvent, value); }
142 public event GridViewDeletedEventHandler RowDeleted {
143 add { Events.AddHandler (RowDeletedEvent, value); }
144 remove { Events.RemoveHandler (RowDeletedEvent, value); }
147 public event GridViewDeleteEventHandler RowDeleting {
148 add { Events.AddHandler (RowDeletingEvent, value); }
149 remove { Events.RemoveHandler (RowDeletingEvent, value); }
152 public event GridViewEditEventHandler RowEditing {
153 add { Events.AddHandler (RowEditingEvent, value); }
154 remove { Events.RemoveHandler (RowEditingEvent, value); }
157 public event GridViewUpdatedEventHandler RowUpdated {
158 add { Events.AddHandler (RowUpdatedEvent, value); }
159 remove { Events.RemoveHandler (RowUpdatedEvent, value); }
162 public event GridViewUpdateEventHandler RowUpdating {
163 add { Events.AddHandler (RowUpdatingEvent, value); }
164 remove { Events.RemoveHandler (RowUpdatingEvent, value); }
167 public event EventHandler SelectedIndexChanged {
168 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
169 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
172 public event GridViewSelectEventHandler SelectedIndexChanging {
173 add { Events.AddHandler (SelectedIndexChangingEvent, value); }
174 remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
177 public event EventHandler Sorted {
178 add { Events.AddHandler (SortedEvent, value); }
179 remove { Events.RemoveHandler (SortedEvent, value); }
182 public event GridViewSortEventHandler Sorting {
183 add { Events.AddHandler (SortingEvent, value); }
184 remove { Events.RemoveHandler (SortingEvent, value); }
187 protected virtual void OnPageIndexChanged (EventArgs e)
189 if (Events != null) {
190 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
191 if (eh != null) eh (this, e);
195 protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
197 if (Events != null) {
198 GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
204 if (!IsBoundUsingDataSourceID)
205 throw new HttpException (String.Format ("The GridView '{0}' fired event PageIndexChanging which wasn't handled.", ID));
208 protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
210 if (Events != null) {
211 GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
217 if (!IsBoundUsingDataSourceID)
218 throw new HttpException (String.Format ("The GridView '{0}' fired event RowCancelingEdit which wasn't handled.", ID));
221 protected virtual void OnRowCommand (GridViewCommandEventArgs e)
223 if (Events != null) {
224 GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
225 if (eh != null) eh (this, e);
229 protected virtual void OnRowCreated (GridViewRowEventArgs e)
231 if (Events != null) {
232 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
233 if (eh != null) eh (this, e);
237 protected virtual void OnRowDataBound (GridViewRowEventArgs e)
239 if (Events != null) {
240 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
241 if (eh != null) eh (this, e);
245 protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
247 if (Events != null) {
248 GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
249 if (eh != null) eh (this, e);
253 protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
255 if (Events != null) {
256 GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
262 if (!IsBoundUsingDataSourceID)
263 throw new HttpException (String.Format ("The GridView '{0}' fired event RowDeleting which wasn't handled.", ID));
266 protected virtual void OnRowEditing (GridViewEditEventArgs e)
268 if (Events != null) {
269 GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
275 if (!IsBoundUsingDataSourceID)
276 throw new HttpException (String.Format ("The GridView '{0}' fired event RowEditing which wasn't handled.", ID));
279 protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
281 if (Events != null) {
282 GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
283 if (eh != null) eh (this, e);
287 protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
289 if (Events != null) {
290 GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
296 if (!IsBoundUsingDataSourceID)
297 throw new HttpException (String.Format ("The GridView '{0}' fired event RowUpdating which wasn't handled.", ID));
300 protected virtual void OnSelectedIndexChanged (EventArgs e)
302 if (Events != null) {
303 EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
304 if (eh != null) eh (this, e);
308 protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
310 if (Events != null) {
311 GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
312 if (eh != null) eh (this, e);
316 protected virtual void OnSorted (EventArgs e)
318 if (Events != null) {
319 EventHandler eh = (EventHandler) Events [SortedEvent];
320 if (eh != null) eh (this, e);
324 protected virtual void OnSorting (GridViewSortEventArgs e)
326 if (Events != null) {
327 GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
333 if (!IsBoundUsingDataSourceID)
334 throw new HttpException (String.Format ("The GridView '{0}' fired event Sorting which wasn't handled.", ID));
338 [WebCategoryAttribute ("Paging")]
339 [DefaultValueAttribute (false)]
340 public virtual bool AllowPaging {
342 object ob = ViewState ["AllowPaging"];
343 if (ob != null) return (bool) ob;
347 if (value == AllowPaging)
349 ViewState ["AllowPaging"] = value;
354 [WebCategoryAttribute ("Behavior")]
355 [DefaultValueAttribute (false)]
356 public virtual bool AllowSorting {
358 object ob = ViewState ["AllowSorting"];
359 if (ob != null) return (bool) ob;
363 if (value == AllowSorting)
365 ViewState ["AllowSorting"] = value;
370 [WebCategoryAttribute ("Styles")]
371 [PersistenceMode (PersistenceMode.InnerProperty)]
372 [NotifyParentProperty (true)]
373 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
374 public TableItemStyle AlternatingRowStyle {
376 if (alternatingRowStyle == null) {
377 alternatingRowStyle = new TableItemStyle ();
378 if (IsTrackingViewState)
379 alternatingRowStyle.TrackViewState();
381 return alternatingRowStyle;
385 [WebCategoryAttribute ("Behavior")]
386 [DefaultValueAttribute (false)]
387 public virtual bool AutoGenerateEditButton {
389 object ob = ViewState ["AutoGenerateEditButton"];
390 if (ob != null) return (bool) ob;
394 if (value == AutoGenerateEditButton)
396 ViewState ["AutoGenerateEditButton"] = value;
401 [WebCategoryAttribute ("Behavior")]
402 [DefaultValueAttribute (false)]
403 public virtual bool AutoGenerateDeleteButton {
405 object ob = ViewState ["AutoGenerateDeleteButton"];
406 if (ob != null) return (bool) ob;
410 if (value == AutoGenerateDeleteButton)
412 ViewState ["AutoGenerateDeleteButton"] = value;
417 [WebCategoryAttribute ("Behavior")]
418 [DefaultValueAttribute (false)]
419 public virtual bool AutoGenerateSelectButton {
421 object ob = ViewState ["AutoGenerateSelectButton"];
422 if (ob != null) return (bool) ob;
426 if (value == AutoGenerateSelectButton)
428 ViewState ["AutoGenerateSelectButton"] = value;
433 [WebCategoryAttribute ("Behavior")]
434 [DefaultValueAttribute (true)]
435 public virtual bool AutoGenerateColumns {
437 object ob = ViewState ["AutoGenerateColumns"];
438 if (ob != null) return (bool) ob;
442 if (value == AutoGenerateColumns)
444 ViewState ["AutoGenerateColumns"] = value;
449 [UrlPropertyAttribute]
450 [WebCategoryAttribute ("Appearance")]
451 [DefaultValueAttribute ("")]
452 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
453 public virtual string BackImageUrl {
455 if (ControlStyleCreated)
456 return ((TableStyle) ControlStyle).BackImageUrl;
460 ((TableStyle) ControlStyle).BackImageUrl = value;
464 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
465 [BrowsableAttribute (false)]
466 public virtual GridViewRow BottomPagerRow {
469 return bottomPagerRow;
473 [WebCategoryAttribute ("Accessibility")]
474 [DefaultValueAttribute ("")]
475 [LocalizableAttribute (true)]
476 public virtual string Caption {
478 object ob = ViewState ["Caption"];
479 if (ob != null) return (string) ob;
483 ViewState ["Caption"] = value;
487 [WebCategoryAttribute ("Accessibility")]
488 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
489 public virtual TableCaptionAlign CaptionAlign
492 object o = ViewState ["CaptionAlign"];
493 if(o != null) return (TableCaptionAlign) o;
494 return TableCaptionAlign.NotSet;
497 ViewState ["CaptionAlign"] = value;
501 [WebCategoryAttribute ("Layout")]
502 [DefaultValueAttribute (-1)]
503 public virtual int CellPadding
506 if (ControlStyleCreated)
507 return ((TableStyle) ControlStyle).CellPadding;
511 ((TableStyle) ControlStyle).CellPadding = value;
515 [WebCategoryAttribute ("Layout")]
516 [DefaultValueAttribute (0)]
517 public virtual int CellSpacing
520 if (ControlStyleCreated)
521 return ((TableStyle) ControlStyle).CellSpacing;
525 ((TableStyle) ControlStyle).CellSpacing = value;
529 [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
530 [MergablePropertyAttribute (false)]
531 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
532 [DefaultValueAttribute (null)]
533 [WebCategoryAttribute ("Misc")]
534 public virtual DataControlFieldCollection Columns {
536 if (columns == null) {
537 columns = new DataControlFieldCollection ();
538 columns.FieldsChanged += new EventHandler (OnFieldsChanged);
539 if (IsTrackingViewState)
540 ((IStateManager)columns).TrackViewState ();
546 [DefaultValueAttribute (null)]
547 [WebCategoryAttribute ("Data")]
548 [TypeConverter (typeof(StringArrayConverter))]
549 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
550 public virtual string[] DataKeyNames
553 if (dataKeyNames != null)
558 dataKeyNames = value;
563 ArrayList DataKeyArrayList {
565 if (_dataKeyArrayList == null) {
566 _dataKeyArrayList = new ArrayList ();
568 return _dataKeyArrayList;
572 [BrowsableAttribute (false)]
573 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
574 public virtual DataKeyArray DataKeys {
577 keys = new DataKeyArray (DataKeyArrayList);
578 if (IsTrackingViewState)
579 ((IStateManager) keys).TrackViewState ();
585 DataKey OldEditValues {
587 if (oldEditValues == null) {
588 oldEditValues = new DataKey (new OrderedDictionary ());
590 return oldEditValues;
594 [WebCategoryAttribute ("Misc")]
595 [DefaultValueAttribute (-1)]
596 public virtual int EditIndex {
601 if (value == editIndex)
608 [WebCategoryAttribute ("Styles")]
609 [PersistenceMode (PersistenceMode.InnerProperty)]
610 [NotifyParentProperty (true)]
611 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
612 public TableItemStyle EditRowStyle {
614 if (editRowStyle == null) {
615 editRowStyle = new TableItemStyle ();
616 if (IsTrackingViewState)
617 editRowStyle.TrackViewState();
623 [WebCategoryAttribute ("Styles")]
624 [PersistenceMode (PersistenceMode.InnerProperty)]
625 [NotifyParentProperty (true)]
626 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
627 public TableItemStyle EmptyDataRowStyle {
629 if (emptyDataRowStyle == null) {
630 emptyDataRowStyle = new TableItemStyle ();
631 if (IsTrackingViewState)
632 emptyDataRowStyle.TrackViewState();
634 return emptyDataRowStyle;
638 [DefaultValue (null)]
639 [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
640 [PersistenceMode (PersistenceMode.InnerProperty)]
642 public virtual ITemplate EmptyDataTemplate {
643 get { return emptyDataTemplate; }
644 set { emptyDataTemplate = value; RequireBinding (); }
647 [LocalizableAttribute (true)]
648 [WebCategoryAttribute ("Appearance")]
649 [DefaultValueAttribute ("")]
650 public virtual string EmptyDataText {
652 object ob = ViewState ["EmptyDataText"];
653 if (ob != null) return (string) ob;
657 if (value == EmptyDataText)
659 ViewState ["EmptyDataText"] = value;
664 [WebCategoryAttribute ("Behavior")]
665 [DefaultValueAttribute (false)]
666 public virtual bool EnableSortingAndPagingCallbacks {
668 object ob = ViewState ["EnableSortingAndPagingCallbacks"];
669 if (ob != null) return (bool) ob;
673 if (value == EnableSortingAndPagingCallbacks)
675 ViewState ["EnableSortingAndPagingCallbacks"] = value;
680 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
681 [BrowsableAttribute (false)]
682 public virtual GridViewRow FooterRow {
685 for (int index = table.Rows.Count - 1; index >= 0; index--) {
686 GridViewRow row = (GridViewRow) table.Rows [index];
687 switch (row.RowType) {
688 case DataControlRowType.Separator:
689 case DataControlRowType.Pager:
691 case DataControlRowType.Footer:
702 [WebCategoryAttribute ("Styles")]
703 [PersistenceMode (PersistenceMode.InnerProperty)]
704 [NotifyParentProperty (true)]
705 [DefaultValue (null)]
706 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
707 public TableItemStyle FooterStyle {
709 if (footerStyle == null) {
710 footerStyle = new TableItemStyle ();
711 if (IsTrackingViewState)
712 footerStyle.TrackViewState();
718 [WebCategoryAttribute ("Appearance")]
719 [DefaultValueAttribute (GridLines.Both)]
720 public virtual GridLines GridLines {
722 if (ControlStyleCreated)
723 return ((TableStyle) ControlStyle).GridLines;
724 return GridLines.Both;
727 ((TableStyle) ControlStyle).GridLines = value;
731 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
732 [BrowsableAttribute (false)]
733 public virtual GridViewRow HeaderRow {
736 for (int index = 0, total = table.Rows.Count; index < total; index++) {
737 GridViewRow row = (GridViewRow) table.Rows [index];
738 switch (row.RowType) {
739 case DataControlRowType.Separator:
740 case DataControlRowType.Pager:
742 case DataControlRowType.Header:
753 [WebCategoryAttribute ("Styles")]
754 [PersistenceMode (PersistenceMode.InnerProperty)]
755 [NotifyParentProperty (true)]
756 [DefaultValue (null)]
757 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
758 public TableItemStyle HeaderStyle {
760 if (headerStyle == null) {
761 headerStyle = new TableItemStyle ();
762 if (IsTrackingViewState)
763 headerStyle.TrackViewState();
769 [Category ("Layout")]
770 [DefaultValueAttribute (HorizontalAlign.NotSet)]
771 public virtual HorizontalAlign HorizontalAlign {
773 if (ControlStyleCreated)
774 return ((TableStyle) ControlStyle).HorizontalAlign;
775 return HorizontalAlign.NotSet;
778 ((TableStyle) ControlStyle).HorizontalAlign = value;
782 [BrowsableAttribute (false)]
783 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
784 public virtual int PageCount {
786 return ViewState.GetInt ("PageCount", 0);
789 ViewState ["PageCount"] = value;
793 [WebCategoryAttribute ("Paging")]
794 [BrowsableAttribute (true)]
795 [DefaultValueAttribute (0)]
796 public virtual int PageIndex {
801 if (value == pageIndex)
808 [DefaultValueAttribute (10)]
809 [WebCategoryAttribute ("Paging")]
810 public virtual int PageSize {
812 object ob = ViewState ["PageSize"];
813 if (ob != null) return (int) ob;
817 if (value == PageSize)
819 ViewState ["PageSize"] = value;
824 [WebCategoryAttribute ("Paging")]
825 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
826 [NotifyParentPropertyAttribute (true)]
827 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
828 public virtual PagerSettings PagerSettings {
830 if (pagerSettings == null) {
831 pagerSettings = new PagerSettings (this);
832 if (IsTrackingViewState)
833 ((IStateManager)pagerSettings).TrackViewState ();
835 return pagerSettings;
839 [WebCategoryAttribute ("Styles")]
840 [PersistenceMode (PersistenceMode.InnerProperty)]
841 [NotifyParentProperty (true)]
842 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
843 public TableItemStyle PagerStyle {
845 if (pagerStyle == null) {
846 pagerStyle = new TableItemStyle ();
847 if (IsTrackingViewState)
848 pagerStyle.TrackViewState();
855 [DefaultValue (null)]
856 /* DataControlPagerCell isnt specified in the docs */
857 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
858 [PersistenceMode (PersistenceMode.InnerProperty)]
860 public virtual ITemplate PagerTemplate {
861 get { return pagerTemplate; }
862 set { pagerTemplate = value; RequireBinding (); }
865 [DefaultValueAttribute ("")]
866 [WebCategoryAttribute ("Accessibility")]
867 // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
868 public virtual string RowHeaderColumn {
870 object ob = ViewState ["RowHeaderColumn"];
871 if (ob != null) return (string) ob;
875 if (value == RowHeaderColumn)
877 ViewState ["RowHeaderColumn"] = value;
882 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
883 [BrowsableAttribute (false)]
884 public virtual GridViewRowCollection Rows {
886 EnsureChildControls ();
888 rows = new GridViewRowCollection (new ArrayList ());
893 [WebCategoryAttribute ("Styles")]
894 [PersistenceMode (PersistenceMode.InnerProperty)]
895 [NotifyParentProperty (true)]
896 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
897 public TableItemStyle RowStyle {
899 if (rowStyle == null) {
900 rowStyle = new TableItemStyle ();
901 if (IsTrackingViewState)
902 rowStyle.TrackViewState();
908 [BrowsableAttribute (false)]
909 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
910 public virtual DataKey SelectedDataKey {
912 if (DataKeyNames.Length == 0)
913 throw new InvalidOperationException (String.Format ("Data keys must be specified on GridView '{0}' before the selected data keys can be retrieved. Use the DataKeyNames property to specify data keys.", ID));
915 if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
916 return DataKeys [selectedIndex];
922 [BindableAttribute (true)]
923 [DefaultValueAttribute (-1)]
924 public virtual int SelectedIndex {
926 return selectedIndex;
929 if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
930 int oldIndex = selectedIndex;
932 Rows [oldIndex].RowState = GetRowState (oldIndex);
934 selectedIndex = value;
935 if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
936 Rows [selectedIndex].RowState = GetRowState (selectedIndex);
941 [BrowsableAttribute (false)]
942 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
943 public virtual GridViewRow SelectedRow {
945 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
946 return Rows [selectedIndex];
952 [WebCategoryAttribute ("Styles")]
953 [PersistenceMode (PersistenceMode.InnerProperty)]
954 [NotifyParentProperty (true)]
955 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
956 public TableItemStyle SelectedRowStyle {
958 if (selectedRowStyle == null) {
959 selectedRowStyle = new TableItemStyle ();
960 if (IsTrackingViewState)
961 selectedRowStyle.TrackViewState();
963 return selectedRowStyle;
967 [BrowsableAttribute (false)]
968 public object SelectedValue {
970 if (SelectedDataKey != null)
971 return SelectedDataKey.Value;
977 [WebCategoryAttribute ("Appearance")]
978 [DefaultValueAttribute (false)]
979 public virtual bool ShowFooter {
981 object ob = ViewState ["ShowFooter"];
982 if (ob != null) return (bool) ob;
986 if (value == ShowFooter)
988 ViewState ["ShowFooter"] = value;
993 [WebCategoryAttribute ("Appearance")]
994 [DefaultValueAttribute (true)]
995 public virtual bool ShowHeader {
997 object ob = ViewState ["ShowHeader"];
998 if (ob != null) return (bool) ob;
1002 if (value == ShowHeader)
1004 ViewState ["ShowHeader"] = value;
1009 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
1010 [BrowsableAttribute (false)]
1011 [DefaultValueAttribute (SortDirection.Ascending)]
1012 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
1013 public virtual SortDirection SortDirection {
1014 get { return sortDirection; }
1016 if (sortDirection == value)
1018 sortDirection = value;
1023 [BrowsableAttribute (false)]
1024 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
1025 public virtual string SortExpression {
1027 if (sortExpression == null)
1028 return String.Empty;
1029 return sortExpression;
1032 if (sortExpression == value)
1034 sortExpression = value;
1039 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
1040 [BrowsableAttribute (false)]
1041 public virtual GridViewRow TopPagerRow {
1048 [WebCategoryAttribute ("Accessibility")]
1049 [DefaultValueAttribute (true)]
1050 public virtual bool UseAccessibleHeader {
1052 object ob = ViewState ["UseAccessibleHeader"];
1053 if (ob != null) return (bool) ob;
1057 if (value == UseAccessibleHeader)
1059 ViewState ["UseAccessibleHeader"] = value;
1064 public virtual bool IsBindableType (Type type)
1066 return type.IsPrimitive || type == typeof (string) || type == typeof (decimal) || type == typeof (DateTime) || type == typeof (Guid);
1069 // MSDN: The CreateDataSourceSelectArguments method is a helper method called by
1070 // the GridView control to create the DataSourceSelectArguments object that
1071 // contains the arguments passed to the data source. In this implementation,
1072 // the DataSourceSelectArguments object contains the arguments for paging operations.
1073 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
1075 DataSourceSelectArguments arg = DataSourceSelectArguments.Empty;
1076 DataSourceView view= GetData();
1077 if (AllowPaging && view.CanPage) {
1078 arg.StartRowIndex = PageIndex * PageSize;
1079 if (view.CanRetrieveTotalRowCount) {
1080 arg.RetrieveTotalRowCount = true;
1081 arg.MaximumRows = PageSize;
1084 arg.MaximumRows = -1;
1088 if (IsBoundUsingDataSourceID && !String.IsNullOrEmpty (sortExpression)) {
1089 if (sortDirection == SortDirection.Ascending)
1090 arg.SortExpression = sortExpression;
1092 arg.SortExpression = sortExpression + " DESC";
1098 protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
1100 ArrayList fields = new ArrayList ();
1102 if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
1103 CommandField field = new CommandField ();
1104 field.ShowEditButton = AutoGenerateEditButton;
1105 field.ShowDeleteButton = AutoGenerateDeleteButton;
1106 field.ShowSelectButton = AutoGenerateSelectButton;
1110 fields.AddRange (Columns);
1112 if (AutoGenerateColumns) {
1114 autoFieldProperties = CreateAutoFieldProperties (dataSource);
1116 if (autoFieldProperties != null) {
1117 foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
1118 fields.Add (CreateAutoGeneratedColumn (props));
1125 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
1127 return new AutoGeneratedField (fieldProperties);
1130 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
1132 if(source == null) return null;
1134 PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
1137 ArrayList retVal = new ArrayList();
1141 object fitem = null;
1143 PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
1144 BindingFlags.Instance | BindingFlags.Static |
1145 BindingFlags.Public, null, null,
1146 new Type[] { typeof(int) }, null);
1148 if (prop_item != null) {
1149 prop_type = prop_item.PropertyType;
1152 if (prop_type == null || prop_type == typeof(object)) {
1153 IEnumerator en = source.GetEnumerator();
1154 if (en != null && en.MoveNext ()) {
1156 _dataEnumerator = en;
1159 prop_type = fitem.GetType();
1162 if (fitem != null && fitem is ICustomTypeDescriptor) {
1163 props = TypeDescriptor.GetProperties(fitem);
1164 } else if (prop_type != null) {
1165 if (IsBindableType (prop_type)) {
1166 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1167 ((IStateManager)field).TrackViewState();
1168 field.Name = "Item";
1169 field.DataField = BoundField.ThisExpression;
1170 field.Type = prop_type;
1173 props = TypeDescriptor.GetProperties (prop_type);
1178 if (props != null && props.Count > 0)
1180 foreach (PropertyDescriptor current in props) {
1181 if (IsBindableType (current.PropertyType)) {
1182 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1183 ((IStateManager)field).TrackViewState();
1184 field.Name = current.Name;
1185 field.DataField = current.Name;
1186 for (int i = 0; i < DataKeyNames.Length; i++) {
1187 if (string.Compare (DataKeyNames [i], current.Name, StringComparison.InvariantCultureIgnoreCase) == 0) {
1188 field.IsReadOnly = true;
1192 field.Type = current.PropertyType;
1198 if (retVal.Count > 0)
1199 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1201 return new AutoGeneratedFieldProperties [0];
1204 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1206 GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1210 void RequireBinding ()
1213 RequiresDataBinding = true;
1217 protected virtual Table CreateChildTable ()
1219 return new ContainedTable (this);
1222 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1233 PagedDataSource dataSource;
1236 DataSourceView view = GetData ();
1237 dataSource = new PagedDataSource ();
1238 dataSource.DataSource = data;
1241 dataSource.AllowPaging = true;
1242 dataSource.PageSize = PageSize;
1244 dataSource.AllowServerPaging = true;
1245 if (SelectArguments.RetrieveTotalRowCount)
1246 dataSource.VirtualCount = SelectArguments.TotalRowCount;
1248 if (PageIndex >= dataSource.PageCount)
1249 pageIndex = dataSource.PageCount - 1;
1250 dataSource.CurrentPageIndex = PageIndex;
1253 PageCount = dataSource.PageCount;
1257 dataSource = new PagedDataSource ();
1258 dataSource.DataSource = data;
1260 dataSource.AllowPaging = true;
1261 dataSource.PageSize = PageSize;
1262 dataSource.CurrentPageIndex = PageIndex;
1266 bool showPager = AllowPaging && (PageCount > 1);
1268 ArrayList list = new ArrayList ();
1270 // Creates the set of fields to show
1272 _dataEnumerator = null;
1273 ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1274 DataControlField[] fields = new DataControlField [fieldCollection.Count];
1275 fieldCollection.CopyTo (fields, 0);
1277 foreach (DataControlField field in fields) {
1278 field.Initialize (AllowSorting, this);
1279 if (EnableSortingAndPagingCallbacks)
1280 field.ValidateSupportsCallback ();
1283 bool skip_first = false;
1284 IEnumerator enumerator;
1285 if (_dataEnumerator != null) {
1286 // replaced when creating bound columns
1287 enumerator = _dataEnumerator;
1291 enumerator = dataSource.GetEnumerator ();
1294 // Main table creation
1295 while (skip_first || enumerator.MoveNext ()) {
1297 object obj = enumerator.Current;
1299 if (list.Count == 0) {
1300 if (showPager && (PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom)) {
1301 topPagerRow = CreatePagerRow (fields.Length, dataSource);
1302 OnRowCreated (new GridViewRowEventArgs (topPagerRow));
1303 ContainedTable.Rows.Add (topPagerRow);
1305 topPagerRow.DataBind ();
1306 OnRowDataBound (new GridViewRowEventArgs (topPagerRow));
1310 GridViewRow headerRow = CreateRow (-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
1311 InitializeRow (headerRow, fields);
1312 OnRowCreated (new GridViewRowEventArgs (headerRow));
1313 ContainedTable.Rows.Add (headerRow);
1315 headerRow.DataBind ();
1316 OnRowDataBound (new GridViewRowEventArgs (headerRow));
1320 DataControlRowState rstate = GetRowState (list.Count);
1321 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1324 InitializeRow (row, fields);
1325 OnRowCreated (new GridViewRowEventArgs (row));
1326 ContainedTable.Rows.Add (row);
1329 OnRowDataBound (new GridViewRowEventArgs (row));
1330 if (EditIndex == row.RowIndex)
1331 oldEditValues = new DataKey (GetRowValues (row, true, true));
1332 DataKeyArrayList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
1336 if (list.Count == 0) {
1337 GridViewRow emptyRow = CreateEmptyrRow (fields.Length);
1338 if (emptyRow != null) {
1339 OnRowCreated (new GridViewRowEventArgs (emptyRow));
1340 ContainedTable.Rows.Add (emptyRow);
1342 emptyRow.DataBind ();
1343 OnRowDataBound (new GridViewRowEventArgs (emptyRow));
1349 GridViewRow footerRow = CreateRow (-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
1350 InitializeRow (footerRow, fields);
1351 OnRowCreated (new GridViewRowEventArgs (footerRow));
1352 ContainedTable.Rows.Add (footerRow);
1354 footerRow.DataBind ();
1355 OnRowDataBound (new GridViewRowEventArgs (footerRow));
1358 if (showPager && (PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom)) {
1359 bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
1360 OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
1361 ContainedTable.Rows.Add (bottomPagerRow);
1363 bottomPagerRow.DataBind ();
1364 OnRowDataBound (new GridViewRowEventArgs (bottomPagerRow));
1369 rows = new GridViewRowCollection (list);
1375 return dataSource.DataSourceCount;
1380 Table ContainedTable {
1382 if (table == null) {
1383 table = CreateChildTable ();
1384 Controls.Add (table);
1390 protected override Style CreateControlStyle ()
1392 TableStyle style = new TableStyle (ViewState);
1393 style.GridLines = GridLines.Both;
1394 style.CellSpacing = 0;
1398 DataControlRowState GetRowState (int index)
1400 DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1401 if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
1402 if (index == EditIndex) rstate |= DataControlRowState.Edit;
1406 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1408 GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1409 InitializePager (row, fieldCount, dataSource);
1413 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1415 TableCell cell = new TableCell ();
1417 cell.ColumnSpan = columnSpan;
1419 if (pagerTemplate != null)
1420 pagerTemplate.InstantiateIn (cell);
1422 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
1424 row.Cells.Add (cell);
1427 GridViewRow CreateEmptyrRow (int fieldCount)
1429 if (emptyDataTemplate == null && String.IsNullOrEmpty (EmptyDataText))
1432 GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1433 TableCell cell = new TableCell ();
1434 cell.ColumnSpan = fieldCount;
1436 if (emptyDataTemplate != null)
1437 emptyDataTemplate.InstantiateIn (cell);
1439 cell.Text = EmptyDataText;
1441 row.Cells.Add (cell);
1445 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1447 DataControlCellType ctype;
1448 bool accessibleHeader = false;
1450 switch (row.RowType) {
1451 case DataControlRowType.Header:
1452 ctype = DataControlCellType.Header;
1453 accessibleHeader = UseAccessibleHeader;
1455 case DataControlRowType.Footer:
1456 ctype = DataControlCellType.Footer;
1459 ctype = DataControlCellType.DataCell;
1463 for (int n=0; n<fields.Length; n++) {
1464 DataControlField field = fields [n];
1468 DataControlFieldCell cell;
1469 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1470 cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1472 cell = new DataControlFieldCell (field);
1473 row.Cells.Add (cell);
1474 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1478 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1480 if (cachedKeyProperties == null) {
1481 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
1482 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1483 for (int n=0; n<DataKeyNames.Length; n++) {
1484 PropertyDescriptor p = props.Find (DataKeyNames [n], true);
1486 throw new InvalidOperationException ("Property '" + DataKeyNames [n] + "' not found in object of type " + row.DataItem.GetType ());
1487 cachedKeyProperties [n] = p;
1491 OrderedDictionary dic = new OrderedDictionary ();
1492 foreach (PropertyDescriptor p in cachedKeyProperties)
1493 dic [p.Name] = p.GetValue (row.DataItem);
1497 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1499 OrderedDictionary dic = new OrderedDictionary ();
1500 ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1504 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1506 foreach (TableCell cell in row.Cells) {
1507 DataControlFieldCell c = cell as DataControlFieldCell;
1509 c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1511 if (!includePrimaryKey && DataKeyNames != null)
1512 foreach (string key in DataKeyNames)
1513 fieldValues.Remove (key);
1516 protected override HtmlTextWriterTag TagKey {
1518 if (EnableSortingAndPagingCallbacks)
1519 return HtmlTextWriterTag.Div;
1521 return HtmlTextWriterTag.Table;
1525 public sealed override void DataBind ()
1527 DataKeyArrayList.Clear ();
1528 cachedKeyProperties = null;
1531 keys = new DataKeyArray (DataKeyArrayList);
1534 protected internal override void PerformDataBinding (IEnumerable data)
1536 base.PerformDataBinding (data);
1539 protected internal virtual void PrepareControlHierarchy ()
1544 table.Caption = Caption;
1545 table.CaptionAlign = CaptionAlign;
1547 foreach (GridViewRow row in table.Rows) {
1548 switch (row.RowType) {
1549 case DataControlRowType.Header:
1550 if (headerStyle != null && !headerStyle.IsEmpty)
1551 row.ControlStyle.MergeWith(headerStyle);
1552 row.Visible = ShowHeader;
1554 case DataControlRowType.Footer:
1555 if (footerStyle != null && !footerStyle.IsEmpty)
1556 row.ControlStyle.MergeWith (footerStyle);
1557 row.Visible = ShowFooter;
1559 case DataControlRowType.Pager:
1560 if (pagerStyle != null && !pagerStyle.IsEmpty)
1561 row.ControlStyle.MergeWith (pagerStyle);
1563 case DataControlRowType.EmptyDataRow:
1564 if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
1565 row.ControlStyle.MergeWith (emptyDataRowStyle);
1567 case DataControlRowType.DataRow:
1568 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
1569 row.ControlStyle.MergeWith (editRowStyle);
1570 if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null && !selectedRowStyle.IsEmpty)
1571 row.ControlStyle.MergeWith (selectedRowStyle);
1572 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
1573 row.ControlStyle.MergeWith (alternatingRowStyle);
1574 if (rowStyle != null && !rowStyle.IsEmpty)
1575 row.ControlStyle.MergeWith (rowStyle);
1581 foreach (TableCell cell in row.Cells) {
1582 DataControlFieldCell fcell = cell as DataControlFieldCell;
1583 if (fcell != null) {
1584 DataControlField field = fcell.ContainingField;
1585 switch (row.RowType) {
1586 case DataControlRowType.Header:
1587 if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
1588 cell.ControlStyle.MergeWith (field.HeaderStyle);
1590 case DataControlRowType.Footer:
1591 if (field.FooterStyleCreated && !field.FooterStyle.IsEmpty)
1592 cell.ControlStyle.MergeWith (field.FooterStyle);
1595 if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty)
1596 foreach (Control c in cell.Controls) {
1597 WebControl wc = c as WebControl;
1599 wc.ControlStyle.MergeWith (field.ControlStyle);
1601 if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
1602 cell.ControlStyle.MergeWith (field.ItemStyle);
1610 protected internal override void OnInit (EventArgs e)
1612 Page.RegisterRequiresControlState (this);
1616 void OnFieldsChanged (object sender, EventArgs args)
1621 protected override void OnDataPropertyChanged ()
1623 base.OnDataPropertyChanged ();
1627 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1629 base.OnDataSourceViewChanged (sender, e);
1633 protected override bool OnBubbleEvent (object source, EventArgs e)
1635 GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1637 bool causesValidation = false;
1638 IButtonControl button = args.CommandSource as IButtonControl;
1639 if (button != null && button.CausesValidation) {
1640 Page.Validate (button.ValidationGroup);
1641 causesValidation = true;
1643 OnRowCommand (args);
1644 string param = args.CommandArgument as string;
1645 if (param == null || param.Length == 0) {
1646 GridViewRow row = args.Row;
1648 param = row.RowIndex.ToString();
1650 ProcessEvent (args.CommandName, param, causesValidation);
1653 return base.OnBubbleEvent (source, e);
1656 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
1658 RaisePostBackEvent (eventArgument);
1661 // This is prolly obsolete
1662 protected virtual void RaisePostBackEvent (string eventArgument)
1664 GridViewCommandEventArgs args;
1665 int i = eventArgument.IndexOf ('$');
1667 args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument.Substring (0, i), eventArgument.Substring (i + 1)));
1669 args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument, null));
1671 OnRowCommand (args);
1672 ProcessEvent (args.CommandName, (string) args.CommandArgument, false);
1675 void ProcessEvent (string eventName, string param, bool causesValidation)
1679 case DataControlCommands.PageCommandName:
1682 case DataControlCommands.FirstPageCommandArgument:
1685 case DataControlCommands.LastPageCommandArgument:
1686 newIndex = PageCount - 1;
1688 case DataControlCommands.NextPageCommandArgument:
1689 newIndex = PageIndex + 1;
1691 case DataControlCommands.PreviousPageCommandArgument:
1692 newIndex = PageIndex - 1;
1696 int.TryParse (param, out paramIndex);
1697 newIndex = paramIndex - 1;
1700 ShowPage (newIndex);
1703 case DataControlCommands.FirstPageCommandArgument:
1707 case DataControlCommands.LastPageCommandArgument:
1708 ShowPage (PageCount - 1);
1711 case DataControlCommands.NextPageCommandArgument:
1712 if (PageIndex < PageCount - 1)
1713 ShowPage (PageIndex + 1);
1716 case DataControlCommands.PreviousPageCommandArgument:
1718 ShowPage (PageIndex - 1);
1721 case DataControlCommands.SelectCommandName:
1722 SelectRow (int.Parse (param));
1725 case DataControlCommands.EditCommandName:
1726 EditRow (int.Parse (param));
1729 case DataControlCommands.UpdateCommandName:
1730 int editIndex = int.Parse (param);
1731 UpdateRow (Rows [editIndex], editIndex, causesValidation);
1734 case DataControlCommands.CancelCommandName:
1738 case DataControlCommands.DeleteCommandName:
1739 DeleteRow (int.Parse (param));
1742 case DataControlCommands.SortCommandName:
1748 void Sort (string newSortExpression)
1750 SortDirection newDirection = SortDirection.Ascending;
1751 if (sortExpression == newSortExpression && sortDirection == SortDirection.Ascending)
1752 newDirection = SortDirection.Descending;
1754 Sort (newSortExpression, newDirection);
1757 public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
1759 GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
1761 if (args.Cancel) return;
1763 if (IsBoundUsingDataSourceID) {
1766 SortExpression = args.SortExpression;
1767 SortDirection = args.SortDirection;
1770 OnSorted (EventArgs.Empty);
1773 void SelectRow (int index)
1775 GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
1776 OnSelectedIndexChanging (args);
1779 SelectedIndex = args.NewSelectedIndex;
1780 OnSelectedIndexChanged (EventArgs.Empty);
1784 void ShowPage (int newIndex)
1786 GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
1787 OnPageIndexChanging (args);
1789 if (args.Cancel || !IsBoundUsingDataSourceID)
1793 PageIndex = args.NewPageIndex;
1794 OnPageIndexChanged (EventArgs.Empty);
1797 void EditRow (int index)
1799 GridViewEditEventArgs args = new GridViewEditEventArgs (index);
1800 OnRowEditing (args);
1802 if (args.Cancel || !IsBoundUsingDataSourceID)
1805 EditIndex = args.NewEditIndex;
1810 GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
1811 OnRowCancelingEdit (args);
1813 if (args.Cancel || !IsBoundUsingDataSourceID)
1819 [MonoTODO ("Support two-way binding expressions")]
1820 public virtual void UpdateRow (int rowIndex, bool causesValidation)
1822 if (rowIndex != EditIndex) throw new NotSupportedException ();
1824 GridViewRow row = Rows [rowIndex];
1825 UpdateRow (row, rowIndex, causesValidation);
1828 void UpdateRow (GridViewRow row, int rowIndex, bool causesValidation)
1830 if (causesValidation && Page != null && !Page.IsValid)
1833 currentEditOldValues = OldEditValues.Values;
1835 currentEditRowKeys = DataKeys [rowIndex].Values;
1836 currentEditNewValues = GetRowValues (row, false, false);
1838 GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (rowIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1839 OnRowUpdating (args);
1841 if (args.Cancel || !IsBoundUsingDataSourceID)
1844 DataSourceView view = GetData ();
1846 throw new HttpException ("The DataSourceView associated to data bound control was null");
1847 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1850 bool UpdateCallback (int recordsAffected, Exception exception)
1852 GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1853 OnRowUpdated (dargs);
1855 if (!dargs.KeepInEditMode)
1858 return dargs.ExceptionHandled;
1861 public virtual void DeleteRow (int rowIndex)
1863 GridViewRow row = Rows [rowIndex];
1864 currentEditRowKeys = DataKeys [rowIndex].Values;
1865 currentEditNewValues = GetRowValues (row, true, true);
1867 GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
1868 OnRowDeleting (args);
1870 if (args.Cancel || !IsBoundUsingDataSourceID)
1874 DataSourceView view = GetData ();
1876 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1878 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1879 OnRowDeleted (dargs);
1883 bool DeleteCallback (int recordsAffected, Exception exception)
1885 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1886 OnRowDeleted (dargs);
1887 return dargs.ExceptionHandled;
1893 oldEditValues = new DataKey (new OrderedDictionary ());
1894 currentEditRowKeys = null;
1895 currentEditOldValues = null;
1896 currentEditNewValues = null;
1899 protected internal override void LoadControlState (object ob)
1901 if (ob == null) return;
1902 object[] state = (object[]) ob;
1903 base.LoadControlState (state[0]);
1904 pageIndex = (int) state[1];
1905 selectedIndex = (int) state[2];
1906 editIndex = (int) state[3];
1907 sortExpression = (string) state[4];
1908 sortDirection = (SortDirection) state[5];
1909 DataKeyNames = (string []) state [6];
1910 if (state [7] != null)
1911 LoadDataKeyArrayState ((object []) state [7]);
1912 if (state [8] != null)
1913 ((IStateManager) OldEditValues).LoadViewState (state [8]);
1916 protected internal override object SaveControlState ()
1918 if (EnableSortingAndPagingCallbacks) {
1919 Page.ClientScript.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
1920 Page.ClientScript.RegisterHiddenField (ClientID + "_SortExpression", SortExpression);
1921 Page.ClientScript.RegisterHiddenField (ClientID + "_SortDirection", ((int)SortDirection).ToString());
1924 object bstate = base.SaveControlState ();
1925 return new object [] {
1933 SaveDataKeyArrayState (),
1934 (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState ())
1938 object [] SaveDataKeyArrayState ()
1943 object [] state = new object [keys.Count];
1944 for (int i = 0; i < keys.Count; i++) {
1945 state [i] = ((IStateManager) keys [i]).SaveViewState ();
1950 void LoadDataKeyArrayState (object [] state)
1952 for (int i = 0; i < state.Length; i++) {
1953 DataKey dataKey = new DataKey (new OrderedDictionary (DataKeyNames.Length), DataKeyNames);
1954 ((IStateManager) dataKey).LoadViewState (state [i]);
1955 DataKeyArrayList.Add (dataKey);
1957 keys = new DataKeyArray (DataKeyArrayList);
1960 protected override void TrackViewState()
1962 base.TrackViewState();
1963 if (columns != null) ((IStateManager)columns).TrackViewState();
1964 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1965 if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
1966 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1967 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1968 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1969 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1970 if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
1971 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1972 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1973 if (keys != null) ((IStateManager)keys).TrackViewState();
1974 if (autoFieldProperties != null) {
1975 foreach (IStateManager sm in autoFieldProperties)
1976 sm.TrackViewState ();
1980 protected override object SaveViewState()
1982 object[] states = new object [12];
1983 states[0] = base.SaveViewState();
1984 states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
1985 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1986 states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
1987 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1988 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1989 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1990 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1991 states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
1992 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1993 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1995 if (autoFieldProperties != null) {
1996 object[] data = new object [autoFieldProperties.Length];
1997 bool allNull = true;
1998 for (int n=0; n<data.Length; n++) {
1999 data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
2000 if (data [n] != null) allNull = false;
2002 if (!allNull) states [11] = data;
2005 for (int i = states.Length - 1; i >= 0; i--) {
2006 if (states [i] != null)
2013 protected override void LoadViewState (object savedState)
2015 if (savedState == null) {
2016 base.LoadViewState (null);
2020 object [] states = (object []) savedState;
2022 if (states[11] != null) {
2023 object[] data = (object[]) states [11];
2024 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
2025 for (int n=0; n<data.Length; n++) {
2026 IStateManager p = new AutoGeneratedFieldProperties ();
2027 p.TrackViewState ();
2028 p.LoadViewState (data [n]);
2029 autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
2033 base.LoadViewState (states[0]);
2035 if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
2036 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
2037 if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
2038 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
2039 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
2040 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
2041 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
2042 if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
2043 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
2044 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
2047 void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
2049 RaiseCallbackEvent (eventArgs);
2052 protected virtual void RaiseCallbackEvent (string eventArgs)
2054 string[] clientData = eventArgs.Split ('|');
2055 PageIndex = int.Parse (clientData[0]);
2056 SortExpression = HttpUtility.UrlDecode (clientData [1]);
2057 SortDirection = (SortDirection) int.Parse (clientData [2]);
2059 RaisePostBackEvent (clientData[3]);
2063 string ICallbackEventHandler.GetCallbackResult ()
2065 return GetCallbackResult ();
2068 protected virtual string GetCallbackResult ()
2070 PrepareControlHierarchy ();
2072 StringWriter sw = new StringWriter ();
2073 sw.Write (PageIndex.ToString () + '|' + SortExpression + '|' + (int) SortDirection + '|');
2075 HtmlTextWriter writer = new HtmlTextWriter (sw);
2076 RenderGrid (writer);
2077 return sw.ToString ();
2080 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
2082 return GetCallbackScript (control, argument);
2085 protected virtual string GetCallbackScript (IButtonControl control, string argument)
2087 if (EnableSortingAndPagingCallbacks)
2088 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
2093 protected override void OnPagePreLoad (object sender, EventArgs e)
2095 base.OnPagePreLoad (sender, e);
2097 if (Page.IsPostBack && EnableSortingAndPagingCallbacks) {
2099 if (int.TryParse (Page.Request.Form [ClientID + "_Page"], out page))
2102 if (int.TryParse (Page.Request.Form [ClientID + "_SortDirection"], out dir))
2103 SortDirection = (SortDirection) dir;
2104 SortExpression = Page.Request.Form [ClientID + "_SortExpression"];
2108 protected internal override void OnPreRender (EventArgs e)
2110 base.OnPreRender (e);
2112 if (EnableSortingAndPagingCallbacks)
2114 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
2115 string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
2116 Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
2119 string cgrid = ClientID + "_data";
2120 string script = string.Format ("var {0} = new Object ();\n", cgrid);
2121 script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
2122 script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
2123 script += string.Format ("{0}.sortDir = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral ((int) SortDirection));
2124 script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
2125 script += string.Format ("{0}.form = {1};\n", cgrid, Page.theForm);
2126 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
2128 // Make sure the basic script infrastructure is rendered
2129 Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
2130 Page.ClientScript.GetPostBackClientHyperlink (this, "");
2134 protected internal override void Render (HtmlTextWriter writer)
2136 PrepareControlHierarchy ();
2138 if (EnableSortingAndPagingCallbacks)
2139 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_div");
2140 writer.RenderBeginTag (HtmlTextWriterTag.Div);
2142 RenderGrid (writer);
2144 writer.RenderEndTag ();
2147 void RenderGrid (HtmlTextWriter writer)
2152 table.Render (writer);
2155 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
2157 if (control == null)
2158 throw new ArgumentNullException ("control");
2160 if (control.CausesValidation)
2161 throw new InvalidOperationException ("A button that causes validation in GridView '" + ID + "' is attempting to use the container GridView as the post back target. The button should either turn off validation or use itself as the post back container.");
2163 PostBackOptions options = new PostBackOptions (this);
2164 options.Argument = control.CommandName + "$" + control.CommandArgument;
2165 options.RequiresJavaScriptProtocol = true;