2 // System.Web.UI.WebControls.GridView.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // Copyright (C) 2005-2010 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.
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Specialized;
33 using System.ComponentModel;
35 using System.Security.Permissions;
38 using System.Reflection;
40 namespace System.Web.UI.WebControls
42 [SupportsEventValidation]
43 [DesignerAttribute ("System.Web.UI.Design.WebControls.GridViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
44 [ControlValuePropertyAttribute ("SelectedValue")]
45 [DefaultEventAttribute ("SelectedIndexChanged")]
47 [DataKeyProperty ("DataKey")]
49 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
50 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
51 public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IPostBackEventHandler, IPostBackContainer, IPersistedSelector
53 , IDataKeysControl, IDataBoundListControl, IDataBoundControl, IFieldControl
57 GridViewRowCollection rows;
58 GridViewRow bottomPagerRow;
59 GridViewRow topPagerRow;
61 IOrderedDictionary currentEditRowKeys;
62 IOrderedDictionary currentEditNewValues;
63 IOrderedDictionary currentEditOldValues;
65 ITemplate pagerTemplate;
66 ITemplate emptyDataTemplate;
68 PropertyDescriptor[] cachedKeyProperties;
70 PropertyDescriptor[] cachedSuffixKeyProperties;
73 DataControlFieldCollection columns;
74 PagerSettings pagerSettings;
76 TableItemStyle alternatingRowStyle;
77 TableItemStyle editRowStyle;
78 TableItemStyle emptyDataRowStyle;
79 TableItemStyle footerStyle;
80 TableItemStyle headerStyle;
81 TableItemStyle pagerStyle;
82 TableItemStyle rowStyle;
83 TableItemStyle selectedRowStyle;
85 TableItemStyle sortedAscendingCellStyle;
86 TableItemStyle sortedAscendingHeaderStyle;
87 TableItemStyle sortedDescendingCellStyle;
88 TableItemStyle sortedDescendingHeaderStyle;
90 List <DataKey> _dataKeySuffixList;
91 DataKeyArray rowSuffixKeys;
93 List <DataKey> _dataKeyList;
95 DataKey oldEditValues;
96 AutoGeneratedFieldProperties[] autoFieldProperties;
97 string [] dataKeyNames = null;
98 readonly string[] emptyKeys = new string[0];
99 IEnumerator _dataEnumerator;
101 static readonly object PageIndexChangedEvent = new object();
102 static readonly object PageIndexChangingEvent = new object();
103 static readonly object RowCancelingEditEvent = new object();
104 static readonly object RowCommandEvent = new object();
105 static readonly object RowCreatedEvent = new object();
106 static readonly object RowDataBoundEvent = new object();
107 static readonly object RowDeletedEvent = new object();
108 static readonly object RowDeletingEvent = new object();
109 static readonly object RowEditingEvent = new object();
110 static readonly object RowUpdatedEvent = new object();
111 static readonly object RowUpdatingEvent = new object();
112 static readonly object SelectedIndexChangedEvent = new object();
113 static readonly object SelectedIndexChangingEvent = new object();
114 static readonly object SortedEvent = new object();
115 static readonly object SortingEvent = new object();
119 int selectedIndex = -1;
122 SortDirection sortDirection = SortDirection.Ascending;
123 string sortExpression;
127 EnableModelValidation = true;
130 public event EventHandler PageIndexChanged {
131 add { Events.AddHandler (PageIndexChangedEvent, value); }
132 remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
135 public event GridViewPageEventHandler PageIndexChanging {
136 add { Events.AddHandler (PageIndexChangingEvent, value); }
137 remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
140 public event GridViewCancelEditEventHandler RowCancelingEdit {
141 add { Events.AddHandler (RowCancelingEditEvent, value); }
142 remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
145 public event GridViewCommandEventHandler RowCommand {
146 add { Events.AddHandler (RowCommandEvent, value); }
147 remove { Events.RemoveHandler (RowCommandEvent, value); }
150 public event GridViewRowEventHandler RowCreated {
151 add { Events.AddHandler (RowCreatedEvent, value); }
152 remove { Events.RemoveHandler (RowCreatedEvent, value); }
155 public event GridViewRowEventHandler RowDataBound {
156 add { Events.AddHandler (RowDataBoundEvent, value); }
157 remove { Events.RemoveHandler (RowDataBoundEvent, value); }
160 public event GridViewDeletedEventHandler RowDeleted {
161 add { Events.AddHandler (RowDeletedEvent, value); }
162 remove { Events.RemoveHandler (RowDeletedEvent, value); }
165 public event GridViewDeleteEventHandler RowDeleting {
166 add { Events.AddHandler (RowDeletingEvent, value); }
167 remove { Events.RemoveHandler (RowDeletingEvent, value); }
170 public event GridViewEditEventHandler RowEditing {
171 add { Events.AddHandler (RowEditingEvent, value); }
172 remove { Events.RemoveHandler (RowEditingEvent, value); }
175 public event GridViewUpdatedEventHandler RowUpdated {
176 add { Events.AddHandler (RowUpdatedEvent, value); }
177 remove { Events.RemoveHandler (RowUpdatedEvent, value); }
180 public event GridViewUpdateEventHandler RowUpdating {
181 add { Events.AddHandler (RowUpdatingEvent, value); }
182 remove { Events.RemoveHandler (RowUpdatingEvent, value); }
185 public event EventHandler SelectedIndexChanged {
186 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
187 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
190 public event GridViewSelectEventHandler SelectedIndexChanging {
191 add { Events.AddHandler (SelectedIndexChangingEvent, value); }
192 remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
195 public event EventHandler Sorted {
196 add { Events.AddHandler (SortedEvent, value); }
197 remove { Events.RemoveHandler (SortedEvent, value); }
200 public event GridViewSortEventHandler Sorting {
201 add { Events.AddHandler (SortingEvent, value); }
202 remove { Events.RemoveHandler (SortingEvent, value); }
205 protected virtual void OnPageIndexChanged (EventArgs e)
207 if (Events != null) {
208 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
209 if (eh != null) eh (this, e);
213 protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
215 if (Events != null) {
216 GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
222 if (!IsBoundUsingDataSourceID)
223 throw new HttpException (String.Format ("The GridView '{0}' fired event PageIndexChanging which wasn't handled.", ID));
226 protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
228 if (Events != null) {
229 GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
235 if (!IsBoundUsingDataSourceID)
236 throw new HttpException (String.Format ("The GridView '{0}' fired event RowCancelingEdit which wasn't handled.", ID));
239 protected virtual void OnRowCommand (GridViewCommandEventArgs e)
241 if (Events != null) {
242 GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
243 if (eh != null) eh (this, e);
247 protected virtual void OnRowCreated (GridViewRowEventArgs e)
249 if (Events != null) {
250 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
251 if (eh != null) eh (this, e);
255 protected virtual void OnRowDataBound (GridViewRowEventArgs e)
257 if (Events != null) {
258 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
259 if (eh != null) eh (this, e);
263 protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
265 if (Events != null) {
266 GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
267 if (eh != null) eh (this, e);
271 protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
273 if (Events != null) {
274 GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
280 if (!IsBoundUsingDataSourceID)
281 throw new HttpException (String.Format ("The GridView '{0}' fired event RowDeleting which wasn't handled.", ID));
284 protected virtual void OnRowEditing (GridViewEditEventArgs e)
286 if (Events != null) {
287 GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
293 if (!IsBoundUsingDataSourceID)
294 throw new HttpException (String.Format ("The GridView '{0}' fired event RowEditing which wasn't handled.", ID));
297 protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
299 if (Events != null) {
300 GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
301 if (eh != null) eh (this, e);
305 protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
307 if (Events != null) {
308 GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
314 if (!IsBoundUsingDataSourceID)
315 throw new HttpException (String.Format ("The GridView '{0}' fired event RowUpdating which wasn't handled.", ID));
318 protected virtual void OnSelectedIndexChanged (EventArgs e)
320 if (Events != null) {
321 EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
322 if (eh != null) eh (this, e);
326 protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
328 if (Events != null) {
329 GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
330 if (eh != null) eh (this, e);
334 protected virtual void OnSorted (EventArgs e)
336 if (Events != null) {
337 EventHandler eh = (EventHandler) Events [SortedEvent];
338 if (eh != null) eh (this, e);
342 protected virtual void OnSorting (GridViewSortEventArgs e)
344 if (Events != null) {
345 GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
351 if (!IsBoundUsingDataSourceID)
352 throw new HttpException (String.Format ("The GridView '{0}' fired event Sorting which wasn't handled.", ID));
356 [WebCategoryAttribute ("Paging")]
357 [DefaultValueAttribute (false)]
358 public virtual bool AllowPaging {
360 object ob = ViewState ["AllowPaging"];
366 if (value == AllowPaging)
368 ViewState ["AllowPaging"] = value;
373 [WebCategoryAttribute ("Behavior")]
374 [DefaultValueAttribute (false)]
375 public virtual bool AllowSorting {
377 object ob = ViewState ["AllowSorting"];
383 if (value == AllowSorting)
385 ViewState ["AllowSorting"] = value;
390 [WebCategoryAttribute ("Styles")]
391 [PersistenceMode (PersistenceMode.InnerProperty)]
392 [NotifyParentProperty (true)]
393 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
394 public TableItemStyle AlternatingRowStyle {
396 if (alternatingRowStyle == null) {
397 alternatingRowStyle = new TableItemStyle ();
398 if (IsTrackingViewState)
399 alternatingRowStyle.TrackViewState();
401 return alternatingRowStyle;
405 [WebCategoryAttribute ("Behavior")]
406 [DefaultValueAttribute (false)]
407 public virtual bool AutoGenerateEditButton {
409 object ob = ViewState ["AutoGenerateEditButton"];
415 if (value == AutoGenerateEditButton)
417 ViewState ["AutoGenerateEditButton"] = value;
422 [WebCategoryAttribute ("Behavior")]
423 [DefaultValueAttribute (false)]
424 public virtual bool AutoGenerateDeleteButton {
426 object ob = ViewState ["AutoGenerateDeleteButton"];
432 if (value == AutoGenerateDeleteButton)
434 ViewState ["AutoGenerateDeleteButton"] = value;
439 [WebCategoryAttribute ("Behavior")]
440 [DefaultValueAttribute (false)]
441 public virtual bool AutoGenerateSelectButton {
443 object ob = ViewState ["AutoGenerateSelectButton"];
449 if (value == AutoGenerateSelectButton)
451 ViewState ["AutoGenerateSelectButton"] = value;
456 [WebCategoryAttribute ("Behavior")]
457 [DefaultValueAttribute (true)]
458 public virtual bool AutoGenerateColumns {
460 object ob = ViewState ["AutoGenerateColumns"];
466 if (value == AutoGenerateColumns)
468 ViewState ["AutoGenerateColumns"] = value;
473 [UrlPropertyAttribute]
474 [WebCategoryAttribute ("Appearance")]
475 [DefaultValueAttribute ("")]
476 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
477 public virtual string BackImageUrl {
479 if (ControlStyleCreated)
480 return ((TableStyle) ControlStyle).BackImageUrl;
483 set { ((TableStyle) ControlStyle).BackImageUrl = value; }
486 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
487 [BrowsableAttribute (false)]
488 public virtual GridViewRow BottomPagerRow {
491 return bottomPagerRow;
495 [WebCategoryAttribute ("Accessibility")]
496 [DefaultValueAttribute ("")]
497 [LocalizableAttribute (true)]
498 public virtual string Caption {
500 object ob = ViewState ["Caption"];
505 set { ViewState ["Caption"] = value; }
508 [WebCategoryAttribute ("Accessibility")]
509 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
510 public virtual TableCaptionAlign CaptionAlign
513 object o = ViewState ["CaptionAlign"];
515 return (TableCaptionAlign) o;
516 return TableCaptionAlign.NotSet;
518 set { ViewState ["CaptionAlign"] = value; }
521 [WebCategoryAttribute ("Layout")]
522 [DefaultValueAttribute (-1)]
523 public virtual int CellPadding
526 if (ControlStyleCreated)
527 return ((TableStyle) ControlStyle).CellPadding;
530 set { ((TableStyle) ControlStyle).CellPadding = value; }
533 [WebCategoryAttribute ("Layout")]
534 [DefaultValueAttribute (0)]
535 public virtual int CellSpacing
538 if (ControlStyleCreated)
539 return ((TableStyle) ControlStyle).CellSpacing;
542 set { ((TableStyle) ControlStyle).CellSpacing = value; }
545 [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
546 [MergablePropertyAttribute (false)]
547 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
548 [DefaultValueAttribute (null)]
549 [WebCategoryAttribute ("Misc")]
550 public virtual DataControlFieldCollection Columns {
552 if (columns == null) {
553 columns = new DataControlFieldCollection ();
554 columns.FieldsChanged += new EventHandler (OnFieldsChanged);
555 if (IsTrackingViewState)
556 ((IStateManager)columns).TrackViewState ();
562 [BrowsableAttribute(false)]
564 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
566 public IAutoFieldGenerator ColumnsGenerator {
571 [DefaultValueAttribute (null)]
572 [WebCategoryAttribute ("Data")]
573 [TypeConverter (typeof(StringArrayConverter))]
574 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
575 public virtual string[] DataKeyNames {
577 if (dataKeyNames != null)
582 dataKeyNames = value;
587 List <DataKey> DataKeyList {
589 if (_dataKeyList == null)
590 _dataKeyList = new List <DataKey> ();
596 List <DataKey> DataKeySuffixList {
598 if (_dataKeySuffixList == null)
599 _dataKeySuffixList = new List <DataKey> ();
601 return _dataKeySuffixList;
605 [BrowsableAttribute (false)]
606 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
607 public virtual DataKeyArray DataKeys {
610 keys = new DataKeyArray (DataKeyList);
611 if (IsTrackingViewState)
612 ((IStateManager) keys).TrackViewState ();
618 DataKey OldEditValues {
620 if (oldEditValues == null) {
621 oldEditValues = new DataKey (new OrderedDictionary ());
623 return oldEditValues;
627 [WebCategoryAttribute ("Misc")]
628 [DefaultValueAttribute (-1)]
629 public virtual int EditIndex {
630 get { return editIndex; }
632 if (value == editIndex)
639 [WebCategoryAttribute ("Styles")]
640 [PersistenceMode (PersistenceMode.InnerProperty)]
641 [NotifyParentProperty (true)]
642 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
643 public TableItemStyle EditRowStyle {
645 if (editRowStyle == null) {
646 editRowStyle = new TableItemStyle ();
647 if (IsTrackingViewState)
648 editRowStyle.TrackViewState();
654 [WebCategoryAttribute ("Styles")]
655 [PersistenceMode (PersistenceMode.InnerProperty)]
656 [NotifyParentProperty (true)]
657 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
658 public TableItemStyle EmptyDataRowStyle {
660 if (emptyDataRowStyle == null) {
661 emptyDataRowStyle = new TableItemStyle ();
662 if (IsTrackingViewState)
663 emptyDataRowStyle.TrackViewState();
665 return emptyDataRowStyle;
669 [DefaultValue (null)]
670 [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
671 [PersistenceMode (PersistenceMode.InnerProperty)]
673 public virtual ITemplate EmptyDataTemplate {
674 get { return emptyDataTemplate; }
675 set { emptyDataTemplate = value; }
678 [LocalizableAttribute (true)]
679 [WebCategoryAttribute ("Appearance")]
680 [DefaultValueAttribute ("")]
681 public virtual string EmptyDataText {
683 object ob = ViewState ["EmptyDataText"];
689 if (value == EmptyDataText)
691 ViewState ["EmptyDataText"] = value;
696 [WebCategoryAttribute ("Behavior")]
697 [DefaultValueAttribute (false)]
698 public virtual bool EnableSortingAndPagingCallbacks {
700 object ob = ViewState ["EnableSortingAndPagingCallbacks"];
706 if (value == EnableSortingAndPagingCallbacks)
708 ViewState ["EnableSortingAndPagingCallbacks"] = value;
713 [MonoTODO ("Make use of it in the code")]
714 [DefaultValue (true)]
715 public virtual bool EnableModelValidation {
720 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
721 [BrowsableAttribute (false)]
722 public virtual GridViewRow FooterRow {
725 for (int index = table.Rows.Count - 1; index >= 0; index--) {
726 GridViewRow row = (GridViewRow) table.Rows [index];
727 switch (row.RowType) {
728 case DataControlRowType.Separator:
729 case DataControlRowType.Pager:
731 case DataControlRowType.Footer:
742 [WebCategoryAttribute ("Styles")]
743 [PersistenceMode (PersistenceMode.InnerProperty)]
744 [NotifyParentProperty (true)]
745 [DefaultValue (null)]
746 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
747 public TableItemStyle FooterStyle {
749 if (footerStyle == null) {
750 footerStyle = new TableItemStyle ();
751 if (IsTrackingViewState)
752 footerStyle.TrackViewState();
758 [WebCategoryAttribute ("Appearance")]
759 [DefaultValueAttribute (GridLines.Both)]
760 public virtual GridLines GridLines {
762 if (ControlStyleCreated)
763 return ((TableStyle) ControlStyle).GridLines;
764 return GridLines.Both;
766 set { ((TableStyle) ControlStyle).GridLines = value; }
769 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
770 [BrowsableAttribute (false)]
771 public virtual GridViewRow HeaderRow {
774 for (int index = 0, total = table.Rows.Count; index < total; index++) {
775 GridViewRow row = (GridViewRow) table.Rows [index];
776 switch (row.RowType) {
777 case DataControlRowType.Separator:
778 case DataControlRowType.Pager:
780 case DataControlRowType.Header:
791 [WebCategoryAttribute ("Styles")]
792 [PersistenceMode (PersistenceMode.InnerProperty)]
793 [NotifyParentProperty (true)]
794 [DefaultValue (null)]
795 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
796 public TableItemStyle HeaderStyle {
798 if (headerStyle == null) {
799 headerStyle = new TableItemStyle ();
800 if (IsTrackingViewState)
801 headerStyle.TrackViewState();
807 [Category ("Layout")]
808 [DefaultValueAttribute (HorizontalAlign.NotSet)]
809 public virtual HorizontalAlign HorizontalAlign {
811 if (ControlStyleCreated)
812 return ((TableStyle) ControlStyle).HorizontalAlign;
813 return HorizontalAlign.NotSet;
815 set { ((TableStyle) ControlStyle).HorizontalAlign = value; }
818 [BrowsableAttribute (false)]
819 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
820 public virtual int PageCount {
821 get { return pageCount; }
822 private set { pageCount = value; }
825 [WebCategoryAttribute ("Paging")]
826 [BrowsableAttribute (true)]
827 [DefaultValueAttribute (0)]
828 public virtual int PageIndex {
829 get { return pageIndex; }
831 if (value == pageIndex)
838 [DefaultValueAttribute (10)]
839 [WebCategoryAttribute ("Paging")]
840 public virtual int PageSize {
842 object ob = ViewState ["PageSize"];
848 if (value == PageSize)
850 ViewState ["PageSize"] = value;
855 [WebCategoryAttribute ("Paging")]
856 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
857 [NotifyParentPropertyAttribute (true)]
858 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
859 public virtual PagerSettings PagerSettings {
861 if (pagerSettings == null) {
862 pagerSettings = new PagerSettings (this);
863 if (IsTrackingViewState)
864 ((IStateManager)pagerSettings).TrackViewState ();
866 return pagerSettings;
870 [WebCategoryAttribute ("Styles")]
871 [PersistenceMode (PersistenceMode.InnerProperty)]
872 [NotifyParentProperty (true)]
873 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
874 public TableItemStyle PagerStyle {
876 if (pagerStyle == null) {
877 pagerStyle = new TableItemStyle ();
878 if (IsTrackingViewState)
879 pagerStyle.TrackViewState();
886 [DefaultValue (null)]
887 [PersistenceMode (PersistenceMode.InnerProperty)]
889 [TemplateContainerAttribute(typeof(GridViewRow))]
890 public virtual ITemplate PagerTemplate {
891 get { return pagerTemplate; }
892 set { pagerTemplate = value; }
895 [DefaultValueAttribute ("")]
896 [WebCategoryAttribute ("Accessibility")]
897 // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
898 public virtual string RowHeaderColumn {
900 object ob = ViewState ["RowHeaderColumn"];
906 if (value == RowHeaderColumn)
908 ViewState ["RowHeaderColumn"] = value;
913 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
914 [BrowsableAttribute (false)]
915 public virtual GridViewRowCollection Rows {
917 EnsureChildControls ();
919 rows = new GridViewRowCollection (new ArrayList ());
924 [WebCategoryAttribute ("Styles")]
925 [PersistenceMode (PersistenceMode.InnerProperty)]
926 [NotifyParentProperty (true)]
927 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
928 public TableItemStyle RowStyle {
930 if (rowStyle == null) {
931 rowStyle = new TableItemStyle ();
932 if (IsTrackingViewState)
933 rowStyle.TrackViewState();
939 [BrowsableAttribute (false)]
940 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
941 public virtual DataKey SelectedDataKey {
943 if (DataKeyNames.Length == 0)
944 throw new InvalidOperationException (String.Format ("Data keys must be specified on GridView '{0}' before the selected data keys can be retrieved. Use the DataKeyNames property to specify data keys.", ID));
946 if (selectedIndex >= 0 && selectedIndex < DataKeys.Count)
947 return DataKeys [selectedIndex];
955 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
956 public virtual DataKey SelectedPersistedDataKey {
961 DataKey IPersistedSelector.DataKey {
962 get { return SelectedPersistedDataKey; }
963 set { SelectedPersistedDataKey = value; }
966 [BindableAttribute (true)]
967 [DefaultValueAttribute (-1)]
968 public virtual int SelectedIndex {
969 get { return selectedIndex; }
971 if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
972 int oldIndex = selectedIndex;
974 Rows [oldIndex].RowState = GetRowState (oldIndex);
976 selectedIndex = value;
977 if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count)
978 Rows [selectedIndex].RowState = GetRowState (selectedIndex);
982 [BrowsableAttribute (false)]
983 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
984 public virtual GridViewRow SelectedRow {
986 if (selectedIndex >= 0 && selectedIndex < Rows.Count)
987 return Rows [selectedIndex];
993 [WebCategoryAttribute ("Styles")]
994 [PersistenceMode (PersistenceMode.InnerProperty)]
995 [NotifyParentProperty (true)]
996 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
997 public TableItemStyle SelectedRowStyle {
999 if (selectedRowStyle == null) {
1000 selectedRowStyle = new TableItemStyle ();
1001 if (IsTrackingViewState)
1002 selectedRowStyle.TrackViewState();
1004 return selectedRowStyle;
1008 [BrowsableAttribute (false)]
1009 public object SelectedValue {
1011 if (SelectedDataKey != null)
1012 return SelectedDataKey.Value;
1018 [WebCategoryAttribute ("Appearance")]
1019 [DefaultValueAttribute (false)]
1020 public virtual bool ShowFooter {
1022 object ob = ViewState ["ShowFooter"];
1028 if (value == ShowFooter)
1030 ViewState ["ShowFooter"] = value;
1035 [WebCategoryAttribute ("Appearance")]
1036 [DefaultValueAttribute (true)]
1037 public virtual bool ShowHeader {
1039 object ob = ViewState ["ShowHeader"];
1045 if (value == ShowHeader)
1047 ViewState ["ShowHeader"] = value;
1052 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
1053 [BrowsableAttribute (false)]
1054 [DefaultValueAttribute (SortDirection.Ascending)]
1055 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
1056 public virtual SortDirection SortDirection {
1057 get { return sortDirection; }
1059 if (sortDirection == value)
1061 sortDirection = value;
1066 [BrowsableAttribute (false)]
1067 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
1068 public virtual string SortExpression {
1070 if (sortExpression == null)
1071 return String.Empty;
1072 return sortExpression;
1075 if (sortExpression == value)
1077 sortExpression = value;
1082 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
1083 [BrowsableAttribute (false)]
1084 public virtual GridViewRow TopPagerRow {
1091 [WebCategoryAttribute ("Accessibility")]
1092 [DefaultValueAttribute (true)]
1093 public virtual bool UseAccessibleHeader {
1095 object ob = ViewState ["UseAccessibleHeader"];
1101 if (value == UseAccessibleHeader)
1103 ViewState ["UseAccessibleHeader"] = value;
1108 [TypeConverter (typeof(StringArrayConverter))]
1109 [DefaultValue (null)]
1110 public virtual string[] ClientIDRowSuffix {
1115 [BrowsableAttribute(false)]
1116 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1117 public DataKeyArray ClientIDRowSuffixDataKeys {
1119 if (rowSuffixKeys == null) {
1120 rowSuffixKeys = new DataKeyArray (DataKeySuffixList);
1121 if (IsTrackingViewState)
1122 ((IStateManager) rowSuffixKeys).TrackViewState ();
1125 return rowSuffixKeys;
1129 [DefaultValue (false)]
1130 public virtual bool EnablePersistedSelection {
1132 throw new NotImplementedException ();
1136 throw new NotImplementedException ();
1140 IAutoFieldGenerator IFieldControl.FieldsGenerator {
1142 throw new NotImplementedException ();
1146 throw new NotImplementedException ();
1150 [DefaultValue (false)]
1151 public virtual bool ShowHeaderWhenEmpty {
1152 get { return ViewState.GetBool ("ShowHeaderWhenEmpty", false); }
1154 if (value == ShowHeaderWhenEmpty)
1157 ViewState ["ShowHeaderWhenEmpty"] = value;
1162 [PersistenceMode (PersistenceMode.InnerProperty)]
1163 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
1164 [NotifyParentProperty (true)]
1165 public TableItemStyle SortedAscendingCellStyle {
1167 if (sortedAscendingCellStyle == null) {
1168 sortedAscendingCellStyle = new TableItemStyle ();
1169 if (IsTrackingViewState)
1170 ((IStateManager)sortedAscendingCellStyle).TrackViewState ();
1173 return sortedAscendingCellStyle;
1177 [PersistenceMode (PersistenceMode.InnerProperty)]
1178 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
1179 [NotifyParentProperty (true)]
1180 public TableItemStyle SortedAscendingHeaderStyle {
1182 if (sortedAscendingHeaderStyle == null) {
1183 sortedAscendingHeaderStyle = new TableItemStyle ();
1184 if (IsTrackingViewState)
1185 ((IStateManager)sortedAscendingHeaderStyle).TrackViewState ();
1188 return sortedAscendingHeaderStyle;
1192 [PersistenceMode (PersistenceMode.InnerProperty)]
1193 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
1194 [NotifyParentProperty (true)]
1195 public TableItemStyle SortedDescendingCellStyle {
1197 if (sortedDescendingCellStyle == null) {
1198 sortedDescendingCellStyle = new TableItemStyle ();
1199 if (IsTrackingViewState)
1200 ((IStateManager)sortedDescendingCellStyle).TrackViewState ();
1203 return sortedDescendingCellStyle;
1207 [PersistenceMode (PersistenceMode.InnerProperty)]
1208 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
1209 [NotifyParentProperty (true)]
1210 public TableItemStyle SortedDescendingHeaderStyle {
1212 if (sortedDescendingHeaderStyle == null) {
1213 sortedDescendingHeaderStyle = new TableItemStyle ();
1214 if (IsTrackingViewState)
1215 ((IStateManager)sortedDescendingHeaderStyle).TrackViewState ();
1218 return sortedDescendingHeaderStyle;
1222 public virtual bool IsBindableType (Type type)
1224 return type.IsPrimitive || type == typeof (string) || type == typeof (decimal) || type == typeof (DateTime) || type == typeof (Guid);
1227 // MSDN: The CreateDataSourceSelectArguments method is a helper method called by
1228 // the GridView control to create the DataSourceSelectArguments object that
1229 // contains the arguments passed to the data source. In this implementation,
1230 // the DataSourceSelectArguments object contains the arguments for paging operations.
1231 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
1233 DataSourceSelectArguments arg = DataSourceSelectArguments.Empty;
1234 DataSourceView view= GetData();
1235 if (AllowPaging && view.CanPage) {
1236 arg.StartRowIndex = PageIndex * PageSize;
1237 if (view.CanRetrieveTotalRowCount) {
1238 arg.RetrieveTotalRowCount = true;
1239 arg.MaximumRows = PageSize;
1241 arg.MaximumRows = -1;
1244 if (IsBoundUsingDataSourceID && !String.IsNullOrEmpty (sortExpression)) {
1245 if (sortDirection == SortDirection.Ascending)
1246 arg.SortExpression = sortExpression;
1248 arg.SortExpression = sortExpression + " DESC";
1254 protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
1256 bool autoGenerate = AutoGenerateColumns;
1259 IAutoFieldGenerator fieldGenerator = ColumnsGenerator;
1260 if (fieldGenerator != null)
1261 return fieldGenerator.GenerateFields (this);
1264 ArrayList fields = new ArrayList ();
1266 if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
1267 CommandField field = new CommandField ();
1268 field.ShowEditButton = AutoGenerateEditButton;
1269 field.ShowDeleteButton = AutoGenerateDeleteButton;
1270 field.ShowSelectButton = AutoGenerateSelectButton;
1274 fields.AddRange (Columns);
1278 autoFieldProperties = CreateAutoFieldProperties (dataSource);
1280 if (autoFieldProperties != null) {
1281 foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
1282 fields.Add (CreateAutoGeneratedColumn (props));
1289 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
1291 return new AutoGeneratedField (fieldProperties);
1294 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
1296 if(source == null) return null;
1298 PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
1301 ArrayList retVal = new ArrayList();
1303 if (props == null) {
1304 object fitem = null;
1306 PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
1307 BindingFlags.Instance | BindingFlags.Static |
1308 BindingFlags.Public, null, null,
1309 new Type[] { typeof(int) }, null);
1311 if (prop_item != null)
1312 prop_type = prop_item.PropertyType;
1314 if (prop_type == null || prop_type == typeof(object)) {
1315 IEnumerator en = source.GetEnumerator();
1316 if (en != null && en.MoveNext ()) {
1318 _dataEnumerator = en;
1321 prop_type = fitem.GetType();
1324 if (fitem != null && fitem is ICustomTypeDescriptor)
1325 props = TypeDescriptor.GetProperties(fitem);
1326 else if (prop_type != null) {
1327 if (IsBindableType (prop_type)) {
1328 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1329 ((IStateManager)field).TrackViewState();
1330 field.Name = "Item";
1331 field.DataField = BoundField.ThisExpression;
1332 field.Type = prop_type;
1335 props = TypeDescriptor.GetProperties (prop_type);
1339 if (props != null && props.Count > 0) {
1340 foreach (PropertyDescriptor current in props) {
1341 if (IsBindableType (current.PropertyType)) {
1342 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1343 ((IStateManager)field).TrackViewState();
1344 field.Name = current.Name;
1345 field.DataField = current.Name;
1346 for (int i = 0; i < DataKeyNames.Length; i++) {
1347 if (string.Compare (DataKeyNames [i], current.Name, StringComparison.InvariantCultureIgnoreCase) == 0) {
1348 field.IsReadOnly = true;
1352 field.Type = current.PropertyType;
1358 if (retVal.Count > 0)
1359 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1361 return new AutoGeneratedFieldProperties [0];
1364 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1366 GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1370 void RequireBinding ()
1373 RequiresDataBinding = true;
1376 protected virtual Table CreateChildTable ()
1378 return new ContainedTable (this);
1381 void CreateHeaderRow (Table mainTable, DataControlField[] fields, bool dataBinding)
1383 GridViewRow headerRow = CreateRow (-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
1384 InitializeRow (headerRow, fields);
1385 OnRowCreated (new GridViewRowEventArgs (headerRow));
1386 mainTable.Rows.Add (headerRow);
1388 headerRow.DataBind ();
1389 OnRowDataBound (new GridViewRowEventArgs (headerRow));
1393 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1403 PagedDataSource dataSource;
1406 DataSourceView view = GetData ();
1407 dataSource = new PagedDataSource ();
1408 dataSource.DataSource = data;
1411 dataSource.AllowPaging = true;
1412 dataSource.PageSize = PageSize;
1414 dataSource.AllowServerPaging = true;
1415 if (SelectArguments.RetrieveTotalRowCount)
1416 dataSource.VirtualCount = SelectArguments.TotalRowCount;
1418 if (PageIndex >= dataSource.PageCount)
1419 pageIndex = dataSource.PageCount - 1;
1420 dataSource.CurrentPageIndex = PageIndex;
1423 PageCount = dataSource.PageCount;
1425 dataSource = new PagedDataSource ();
1426 dataSource.DataSource = data;
1428 dataSource.AllowPaging = true;
1429 dataSource.PageSize = PageSize;
1430 dataSource.CurrentPageIndex = PageIndex;
1434 bool createPager = AllowPaging && (PageCount >= 1) && PagerSettings.Visible;
1436 ArrayList list = new ArrayList ();
1438 // Creates the set of fields to show
1440 _dataEnumerator = null;
1441 ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1442 int fieldCount = fieldCollection.Count;
1443 DataControlField dcf;
1444 DataControlField[] fields = new DataControlField [fieldCount];
1445 fieldCollection.CopyTo (fields, 0);
1447 for (int i = 0; i < fieldCount; i++) {
1449 dcf.Initialize (AllowSorting, this);
1450 if (EnableSortingAndPagingCallbacks)
1451 dcf.ValidateSupportsCallback ();
1454 bool skip_first = false;
1455 IEnumerator enumerator;
1456 if (_dataEnumerator != null) {
1457 // replaced when creating bound columns
1458 enumerator = _dataEnumerator;
1461 enumerator = dataSource.GetEnumerator ();
1463 // Main table creation
1464 Table mainTable = ContainedTable;
1465 List <DataKey> dataKeyList;
1466 string[] dataKeyNames;
1468 List <DataKey> dataKeySuffixList;
1469 string[] clientIDRowSuffix;
1472 dataKeyList = DataKeyList;
1473 dataKeyNames = DataKeyNames;
1475 dataKeySuffixList = DataKeySuffixList;
1476 clientIDRowSuffix = ClientIDRowSuffix;
1480 dataKeyNames = null;
1482 dataKeySuffixList = null;
1483 clientIDRowSuffix = null;
1487 while (skip_first || enumerator.MoveNext ()) {
1489 object obj = enumerator.Current;
1491 if (list.Count == 0) {
1492 if (createPager && (PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom)) {
1493 topPagerRow = CreatePagerRow (fieldCount, dataSource);
1494 OnRowCreated (new GridViewRowEventArgs (topPagerRow));
1495 mainTable.Rows.Add (topPagerRow);
1497 topPagerRow.DataBind ();
1498 OnRowDataBound (new GridViewRowEventArgs (topPagerRow));
1501 topPagerRow.Visible = false;
1505 CreateHeaderRow (mainTable, fields, dataBinding);
1508 DataControlRowState rstate = GetRowState (list.Count);
1509 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1512 InitializeRow (row, fields);
1513 OnRowCreated (new GridViewRowEventArgs (row));
1514 mainTable.Rows.Add (row);
1517 if (EditIndex == row.RowIndex)
1518 oldEditValues = new DataKey (GetRowValues (row, true, true));
1519 dataKeyList.Add (new DataKey (CreateRowDataKey (row), dataKeyNames));
1521 dataKeySuffixList.Add (new DataKey (CreateRowSuffixDataKey (row), clientIDRowSuffix));
1523 OnRowDataBound (new GridViewRowEventArgs (row));
1527 if (list.Count == 0) {
1529 if (ShowHeader && ShowHeaderWhenEmpty)
1530 CreateHeaderRow (mainTable, fields, dataBinding);
1532 GridViewRow emptyRow = CreateEmptyrRow (fieldCount);
1533 if (emptyRow != null) {
1534 OnRowCreated (new GridViewRowEventArgs (emptyRow));
1535 mainTable.Rows.Add (emptyRow);
1537 emptyRow.DataBind ();
1538 OnRowDataBound (new GridViewRowEventArgs (emptyRow));
1542 if (mainTable.Rows.Count == 0)
1547 GridViewRow footerRow = CreateRow (-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
1548 InitializeRow (footerRow, fields);
1549 OnRowCreated (new GridViewRowEventArgs (footerRow));
1550 mainTable.Rows.Add (footerRow);
1552 footerRow.DataBind ();
1553 OnRowDataBound (new GridViewRowEventArgs (footerRow));
1556 if (createPager && (PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom)) {
1557 bottomPagerRow = CreatePagerRow (fieldCount, dataSource);
1558 OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
1559 mainTable.Rows.Add (bottomPagerRow);
1561 bottomPagerRow.DataBind ();
1562 OnRowDataBound (new GridViewRowEventArgs (bottomPagerRow));
1565 bottomPagerRow.Visible = false;
1569 rows = new GridViewRowCollection (list);
1575 return dataSource.DataSourceCount;
1580 Table ContainedTable {
1582 if (table == null) {
1583 table = CreateChildTable ();
1584 Controls.Add (table);
1591 protected override Style CreateControlStyle ()
1593 TableStyle style = new TableStyle (ViewState);
1594 style.GridLines = GridLines.Both;
1595 style.CellSpacing = 0;
1599 DataControlRowState GetRowState (int index)
1601 DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1602 if (index == SelectedIndex)
1603 rstate |= DataControlRowState.Selected;
1604 if (index == EditIndex)
1605 rstate |= DataControlRowState.Edit;
1609 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1611 GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1612 InitializePager (row, fieldCount, dataSource);
1616 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1618 TableCell cell = new TableCell ();
1620 cell.ColumnSpan = columnSpan;
1622 if (pagerTemplate != null)
1623 pagerTemplate.InstantiateIn (cell);
1625 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
1627 row.Cells.Add (cell);
1630 GridViewRow CreateEmptyrRow (int fieldCount)
1632 if (emptyDataTemplate == null && String.IsNullOrEmpty (EmptyDataText))
1635 GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1636 TableCell cell = new TableCell ();
1637 cell.ColumnSpan = fieldCount;
1639 if (emptyDataTemplate != null)
1640 emptyDataTemplate.InstantiateIn (cell);
1642 cell.Text = EmptyDataText;
1644 row.Cells.Add (cell);
1648 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1650 DataControlCellType ctype;
1651 bool accessibleHeader = false;
1653 switch (row.RowType) {
1654 case DataControlRowType.Header:
1655 ctype = DataControlCellType.Header;
1656 accessibleHeader = UseAccessibleHeader;
1658 case DataControlRowType.Footer:
1659 ctype = DataControlCellType.Footer;
1662 ctype = DataControlCellType.DataCell;
1666 for (int n=0; n<fields.Length; n++) {
1667 DataControlField field = fields [n];
1668 DataControlFieldCell cell;
1670 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1671 cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1673 cell = new DataControlFieldCell (field);
1674 row.Cells.Add (cell);
1675 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1679 void LoadAndCacheProperties (string[] names, object dataItem, ref PropertyDescriptor[] cache)
1684 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
1685 int len = names != null ? names.Length : 0;
1686 cache = new PropertyDescriptor [len];
1687 for (int n = 0; n < len; n++) {
1688 string propName = names [n];
1689 PropertyDescriptor p = props.Find (propName, true);
1691 throw new InvalidOperationException ("Property '" + propName + "' not found in object of type " + dataItem.GetType ());
1696 IOrderedDictionary CreateDictionaryFromProperties (PropertyDescriptor[] cache, object dataItem)
1698 OrderedDictionary dic = new OrderedDictionary ();
1699 foreach (PropertyDescriptor p in cache)
1700 dic [p.Name] = p.GetValue (dataItem);
1704 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1706 object dataItem = row.DataItem;
1707 LoadAndCacheProperties (DataKeyNames, dataItem, ref cachedKeyProperties);
1708 return CreateDictionaryFromProperties (cachedKeyProperties, dataItem);
1711 IOrderedDictionary CreateRowSuffixDataKey (GridViewRow row)
1713 object dataItem = row.DataItem;
1714 LoadAndCacheProperties (ClientIDRowSuffix, dataItem, ref cachedSuffixKeyProperties);
1715 return CreateDictionaryFromProperties (cachedSuffixKeyProperties, dataItem);
1718 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1720 OrderedDictionary dic = new OrderedDictionary ();
1721 ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1725 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1727 DataControlField field;
1728 foreach (TableCell cell in row.Cells) {
1729 DataControlFieldCell c = cell as DataControlFieldCell;
1733 field = c.ContainingField;
1734 if (field != null && !field.Visible)
1737 c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1739 if (!includePrimaryKey && DataKeyNames != null)
1740 foreach (string key in DataKeyNames)
1741 fieldValues.Remove (key);
1744 protected override HtmlTextWriterTag TagKey {
1746 if (EnableSortingAndPagingCallbacks)
1747 return HtmlTextWriterTag.Div;
1749 return HtmlTextWriterTag.Table;
1753 public sealed override void DataBind ()
1755 DataKeyList.Clear ();
1756 cachedKeyProperties = null;
1759 keys = new DataKeyArray (DataKeyList);
1761 GridViewRow row = HeaderRow;
1763 row.Visible = ShowHeader;
1767 row.Visible = ShowFooter;
1770 protected internal override void PerformDataBinding (IEnumerable data)
1772 base.PerformDataBinding (data);
1775 protected internal virtual void PrepareControlHierarchy ()
1780 table.Caption = Caption;
1781 table.CaptionAlign = CaptionAlign;
1782 table.CopyBaseAttributes (this);
1784 foreach (GridViewRow row in table.Rows) {
1785 switch (row.RowType) {
1786 case DataControlRowType.Header:
1787 if (headerStyle != null && !headerStyle.IsEmpty)
1788 row.ControlStyle.MergeWith(headerStyle);
1789 row.Visible = ShowHeader;
1791 case DataControlRowType.Footer:
1792 if (footerStyle != null && !footerStyle.IsEmpty)
1793 row.ControlStyle.MergeWith (footerStyle);
1794 row.Visible = ShowFooter;
1796 case DataControlRowType.Pager:
1797 if (pagerStyle != null && !pagerStyle.IsEmpty)
1798 row.ControlStyle.MergeWith (pagerStyle);
1800 case DataControlRowType.EmptyDataRow:
1801 if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
1802 row.ControlStyle.MergeWith (emptyDataRowStyle);
1804 case DataControlRowType.DataRow:
1805 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
1806 row.ControlStyle.MergeWith (editRowStyle);
1807 if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null && !selectedRowStyle.IsEmpty)
1808 row.ControlStyle.MergeWith (selectedRowStyle);
1809 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
1810 row.ControlStyle.MergeWith (alternatingRowStyle);
1811 if (rowStyle != null && !rowStyle.IsEmpty)
1812 row.ControlStyle.MergeWith (rowStyle);
1818 string sortExpression = SortExpression;
1819 bool haveSorting = !String.IsNullOrEmpty (sortExpression);
1821 foreach (TableCell cell in row.Cells) {
1822 DataControlFieldCell fcell = cell as DataControlFieldCell;
1823 if (fcell != null) {
1824 DataControlField field = fcell.ContainingField;
1827 if (!field.Visible) {
1828 cell.Visible = false;
1832 switch (row.RowType) {
1833 case DataControlRowType.Header:
1834 if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
1835 cell.ControlStyle.MergeWith (field.HeaderStyle);
1838 MergeWithSortingStyle (sortExpression, sortedAscendingHeaderStyle, sortedDescendingHeaderStyle, field, cell);
1841 case DataControlRowType.Footer:
1842 if (field.FooterStyleCreated && !field.FooterStyle.IsEmpty)
1843 cell.ControlStyle.MergeWith (field.FooterStyle);
1846 if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty) {
1847 foreach (Control c in cell.Controls) {
1848 WebControl wc = c as WebControl;
1850 wc.ControlStyle.MergeWith (field.ControlStyle);
1853 if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
1854 cell.ControlStyle.MergeWith (field.ItemStyle);
1857 MergeWithSortingStyle (sortExpression, sortedAscendingCellStyle, sortedDescendingCellStyle, field, cell);
1866 void MergeWithSortingStyle (string sortExpression, TableItemStyle ascending, TableItemStyle descending, DataControlField field, TableCell cell)
1868 if (String.Compare (field.SortExpression, sortExpression, StringComparison.OrdinalIgnoreCase) != 0)
1871 cell.ControlStyle.MergeWith (SortDirection == SortDirection.Ascending ? ascending : descending);
1874 protected internal override void OnInit (EventArgs e)
1878 page.RegisterRequiresControlState (this);
1882 void OnFieldsChanged (object sender, EventArgs args)
1887 protected override void OnDataPropertyChanged ()
1889 base.OnDataPropertyChanged ();
1893 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1895 base.OnDataSourceViewChanged (sender, e);
1899 protected override bool OnBubbleEvent (object source, EventArgs e)
1901 GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1903 bool causesValidation = false;
1904 IButtonControl button = args.CommandSource as IButtonControl;
1905 if (button != null && button.CausesValidation) {
1906 Page.Validate (button.ValidationGroup);
1907 causesValidation = true;
1909 OnRowCommand (args);
1910 string param = args.CommandArgument as string;
1911 if (param == null || param.Length == 0) {
1912 GridViewRow row = args.Row;
1914 param = row.RowIndex.ToString();
1916 ProcessEvent (args.CommandName, param, causesValidation);
1919 return base.OnBubbleEvent (source, e);
1922 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
1924 ValidateEvent (UniqueID, eventArgument);
1925 RaisePostBackEvent (eventArgument);
1928 // This is prolly obsolete
1929 protected virtual void RaisePostBackEvent (string eventArgument)
1931 GridViewCommandEventArgs args;
1932 int i = eventArgument.IndexOf ('$');
1934 args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument.Substring (0, i), eventArgument.Substring (i + 1)));
1936 args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument, null));
1938 OnRowCommand (args);
1939 ProcessEvent (args.CommandName, (string) args.CommandArgument, false);
1942 void ProcessEvent (string eventName, string param, bool causesValidation)
1944 switch (eventName) {
1945 case DataControlCommands.PageCommandName:
1948 case DataControlCommands.FirstPageCommandArgument:
1951 case DataControlCommands.LastPageCommandArgument:
1952 newIndex = PageCount - 1;
1954 case DataControlCommands.NextPageCommandArgument:
1955 newIndex = PageIndex + 1;
1957 case DataControlCommands.PreviousPageCommandArgument:
1958 newIndex = PageIndex - 1;
1962 int.TryParse (param, out paramIndex);
1963 newIndex = paramIndex - 1;
1966 SetPageIndex (newIndex);
1969 case DataControlCommands.FirstPageCommandArgument:
1973 case DataControlCommands.LastPageCommandArgument:
1974 SetPageIndex (PageCount - 1);
1977 case DataControlCommands.NextPageCommandArgument:
1978 if (PageIndex < PageCount - 1)
1979 SetPageIndex (PageIndex + 1);
1982 case DataControlCommands.PreviousPageCommandArgument:
1984 SetPageIndex (PageIndex - 1);
1987 case DataControlCommands.SelectCommandName:
1988 SelectRow (int.Parse (param));
1991 case DataControlCommands.EditCommandName:
1992 SetEditRow (int.Parse (param));
1995 case DataControlCommands.UpdateCommandName:
1996 int editIndex = int.Parse (param);
1997 UpdateRow (Rows [editIndex], editIndex, causesValidation);
2000 case DataControlCommands.CancelCommandName:
2004 case DataControlCommands.DeleteCommandName:
2005 DeleteRow (int.Parse (param));
2008 case DataControlCommands.SortCommandName:
2014 void Sort (string newSortExpression)
2016 SortDirection newDirection = SortDirection.Ascending;
2017 if (sortExpression == newSortExpression && sortDirection == SortDirection.Ascending)
2018 newDirection = SortDirection.Descending;
2020 Sort (newSortExpression, newDirection);
2023 public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
2025 GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
2030 if (IsBoundUsingDataSourceID) {
2033 SortExpression = args.SortExpression;
2034 SortDirection = args.SortDirection;
2037 OnSorted (EventArgs.Empty);
2042 void SelectRow (int index)
2044 GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
2045 OnSelectedIndexChanging (args);
2048 SelectedIndex = args.NewSelectedIndex;
2049 OnSelectedIndexChanged (EventArgs.Empty);
2055 void SetPageIndex (int newIndex)
2057 GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
2058 OnPageIndexChanging (args);
2060 if (args.Cancel || !IsBoundUsingDataSourceID)
2064 PageIndex = args.NewPageIndex;
2065 OnPageIndexChanged (EventArgs.Empty);
2070 void SetEditRow (int index)
2072 GridViewEditEventArgs args = new GridViewEditEventArgs (index);
2073 OnRowEditing (args);
2075 if (args.Cancel || !IsBoundUsingDataSourceID)
2078 EditIndex = args.NewEditIndex;
2083 GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
2084 OnRowCancelingEdit (args);
2086 if (args.Cancel || !IsBoundUsingDataSourceID)
2092 [MonoTODO ("Support two-way binding expressions")]
2093 public virtual void UpdateRow (int rowIndex, bool causesValidation)
2095 if (rowIndex != EditIndex)
2096 throw new NotSupportedException ();
2098 GridViewRow row = Rows [rowIndex];
2099 UpdateRow (row, rowIndex, causesValidation);
2102 void UpdateRow (GridViewRow row, int rowIndex, bool causesValidation)
2104 if (causesValidation && Page != null && !Page.IsValid)
2107 currentEditOldValues = CopyOrderedDictionary (OldEditValues.Values);
2108 currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
2109 currentEditNewValues = GetRowValues (row, false, false);
2111 GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (rowIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
2112 OnRowUpdating (args);
2114 if (args.Cancel || !IsBoundUsingDataSourceID)
2117 DataSourceView view = GetData ();
2119 throw new HttpException ("The DataSourceView associated to data bound control was null");
2120 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
2123 static IOrderedDictionary CopyOrderedDictionary (IOrderedDictionary sourceDic)
2125 OrderedDictionary copyDic = new OrderedDictionary ();
2126 foreach (object key in sourceDic.Keys)
2127 copyDic.Add (key, sourceDic [key]);
2131 bool UpdateCallback (int recordsAffected, Exception exception)
2133 GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
2134 OnRowUpdated (dargs);
2136 if (!dargs.KeepInEditMode)
2139 return dargs.ExceptionHandled;
2142 public virtual void DeleteRow (int rowIndex)
2144 GridViewRow row = Rows [rowIndex];
2145 currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
2146 currentEditNewValues = GetRowValues (row, true, true);
2148 GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
2149 OnRowDeleting (args);
2151 if (args.Cancel || !IsBoundUsingDataSourceID)
2155 DataSourceView view = GetData ();
2157 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
2159 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
2160 OnRowDeleted (dargs);
2164 bool DeleteCallback (int recordsAffected, Exception exception)
2166 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
2167 OnRowDeleted (dargs);
2168 return dargs.ExceptionHandled;
2174 oldEditValues = new DataKey (new OrderedDictionary ());
2175 currentEditRowKeys = null;
2176 currentEditOldValues = null;
2177 currentEditNewValues = null;
2180 protected internal override void LoadControlState (object ob)
2184 object[] state = (object[]) ob;
2185 base.LoadControlState (state[0]);
2186 pageIndex = (int) state[1];
2187 selectedIndex = (int) state[2];
2188 editIndex = (int) state[3];
2189 sortExpression = (string) state[4];
2190 sortDirection = (SortDirection) state[5];
2191 DataKeyNames = (string []) state [6];
2192 if (state [7] != null)
2193 LoadDataKeyArrayState ((object []) state [7], out keys);
2194 if (state [8] != null)
2195 ((IStateManager) OldEditValues).LoadViewState (state [8]);
2196 pageCount = (int)state [9];
2198 if (state [10] != null)
2199 ClientIDRowSuffix = (string[]) state [10];
2200 if (state [11] != null)
2201 LoadDataKeyArrayState ((object []) state [11], out rowSuffixKeys);
2205 protected internal override object SaveControlState ()
2207 if (EnableSortingAndPagingCallbacks) {
2209 ClientScriptManager scriptManager = page != null ? page.ClientScript : null;
2211 if (scriptManager != null) {
2212 scriptManager.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
2213 scriptManager.RegisterHiddenField (ClientID + "_SortExpression", SortExpression);
2214 scriptManager.RegisterHiddenField (ClientID + "_SortDirection", ((int)SortDirection).ToString());
2218 object bstate = base.SaveControlState ();
2219 return new object [] {
2227 SaveDataKeyArrayState (keys),
2228 (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState ()),
2232 SaveDataKeyArrayState (rowSuffixKeys)
2237 object [] SaveDataKeyArrayState (DataKeyArray keys)
2242 object [] state = new object [keys.Count];
2243 for (int i = 0; i < keys.Count; i++)
2244 state [i] = ((IStateManager) keys [i]).SaveViewState ();
2248 void LoadDataKeyArrayState (object [] state, out DataKeyArray keys)
2250 List <DataKey> dataKeyList = DataKeyList;
2251 string[] dataKeyNames = DataKeyNames;
2252 int dataKeyNamesLen = dataKeyNames.Length;
2254 for (int i = 0; i < state.Length; i++) {
2255 DataKey dataKey = new DataKey (new OrderedDictionary (dataKeyNamesLen), dataKeyNames);
2256 ((IStateManager) dataKey).LoadViewState (state [i]);
2257 dataKeyList.Add (dataKey);
2259 keys = new DataKeyArray (dataKeyList);
2262 protected override void TrackViewState ()
2264 base.TrackViewState();
2265 if (columns != null)
2266 ((IStateManager)columns).TrackViewState();
2267 if (pagerSettings != null)
2268 ((IStateManager)pagerSettings).TrackViewState();
2269 if (alternatingRowStyle != null)
2270 ((IStateManager)alternatingRowStyle).TrackViewState();
2271 if (footerStyle != null)
2272 ((IStateManager)footerStyle).TrackViewState();
2273 if (headerStyle != null)
2274 ((IStateManager)headerStyle).TrackViewState();
2275 if (pagerStyle != null)
2276 ((IStateManager)pagerStyle).TrackViewState();
2277 if (rowStyle != null)
2278 ((IStateManager)rowStyle).TrackViewState();
2279 if (selectedRowStyle != null)
2280 ((IStateManager)selectedRowStyle).TrackViewState();
2281 if (editRowStyle != null)
2282 ((IStateManager)editRowStyle).TrackViewState();
2283 if (emptyDataRowStyle != null)
2284 ((IStateManager)emptyDataRowStyle).TrackViewState();
2286 if (sortedAscendingCellStyle != null)
2287 ((IStateManager)sortedAscendingCellStyle).TrackViewState ();
2289 if (sortedAscendingHeaderStyle != null)
2290 ((IStateManager)sortedAscendingHeaderStyle).TrackViewState ();
2292 if (sortedDescendingCellStyle != null)
2293 ((IStateManager)sortedDescendingCellStyle).TrackViewState ();
2295 if (sortedDescendingHeaderStyle != null)
2296 ((IStateManager)sortedDescendingHeaderStyle).TrackViewState ();
2297 if (rowSuffixKeys != null)
2298 ((IStateManager) rowSuffixKeys).TrackViewState ();
2301 ((IStateManager)keys).TrackViewState();
2302 if (autoFieldProperties != null) {
2303 foreach (IStateManager sm in autoFieldProperties)
2304 sm.TrackViewState ();
2308 protected override object SaveViewState ()
2310 object[] autoFieldsData = null;
2312 if (autoFieldProperties != null) {
2313 object[] data = new object [autoFieldProperties.Length];
2314 bool allNull = true;
2315 for (int n = 0; n < data.Length; n++) {
2316 data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
2317 if (data [n] != null)
2321 autoFieldsData = data;
2325 base.SaveViewState(), // 0
2326 (columns == null ? null : ((IStateManager)columns).SaveViewState()), // 1
2327 (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState()), // 2
2328 (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState()), // 3
2329 (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState()), // 4
2330 (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState()), // 5
2331 (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState()), // 6
2332 (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState()), // 7
2333 (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState()), // 8
2334 (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState()), // 9
2335 (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState()), // 10
2336 autoFieldsData, // 11
2338 sortedAscendingCellStyle == null ? null : ((IStateManager)sortedAscendingCellStyle).SaveViewState (), // 12
2339 sortedAscendingHeaderStyle == null ? null : ((IStateManager)sortedAscendingHeaderStyle).SaveViewState (), // 13
2340 sortedDescendingCellStyle == null ? null : ((IStateManager)sortedDescendingCellStyle).SaveViewState (), // 14
2341 sortedDescendingHeaderStyle == null ? null : ((IStateManager)sortedDescendingHeaderStyle).SaveViewState () // 15
2345 for (int i = states.Length - 1; i >= 0; i--) {
2346 if (states [i] != null)
2353 protected override void LoadViewState (object savedState)
2355 if (savedState == null) {
2356 base.LoadViewState (null);
2360 object [] states = (object []) savedState;
2362 if (states[11] != null) {
2363 object[] data = (object[]) states [11];
2364 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
2365 for (int n=0; n<data.Length; n++) {
2366 IStateManager p = new AutoGeneratedFieldProperties ();
2367 p.TrackViewState ();
2368 p.LoadViewState (data [n]);
2369 autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
2373 base.LoadViewState (states[0]);
2375 if (states[1] != null)
2376 ((IStateManager)Columns).LoadViewState (states[1]);
2377 if (states[2] != null)
2378 ((IStateManager)PagerSettings).LoadViewState (states[2]);
2379 if (states[3] != null)
2380 ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
2381 if (states[4] != null)
2382 ((IStateManager)FooterStyle).LoadViewState (states[4]);
2383 if (states[5] != null)
2384 ((IStateManager)HeaderStyle).LoadViewState (states[5]);
2385 if (states[6] != null)
2386 ((IStateManager)PagerStyle).LoadViewState (states[6]);
2387 if (states[7] != null)
2388 ((IStateManager)RowStyle).LoadViewState (states[7]);
2389 if (states[8] != null)
2390 ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
2391 if (states[9] != null)
2392 ((IStateManager)EditRowStyle).LoadViewState (states[9]);
2393 if (states[10] != null)
2394 ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
2396 if (states [12] != null)
2397 ((IStateManager)sortedAscendingCellStyle).LoadViewState (states [12]);
2398 if (states [13] != null)
2399 ((IStateManager)sortedAscendingHeaderStyle).LoadViewState (states [13]);
2400 if (states [14] != null)
2401 ((IStateManager)sortedDescendingCellStyle).LoadViewState (states [14]);
2402 if (states [15] != null)
2403 ((IStateManager)sortedDescendingHeaderStyle).LoadViewState (states [15]);
2407 void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
2409 RaiseCallbackEvent (eventArgs);
2412 protected virtual void RaiseCallbackEvent (string eventArgs)
2414 string[] clientData = eventArgs.Split ('|');
2415 PageIndex = int.Parse (clientData[0]);
2416 SortExpression = HttpUtility.UrlDecode (clientData [1]);
2417 SortDirection = (SortDirection) int.Parse (clientData [2]);
2419 RaisePostBackEvent (clientData[3]);
2423 string ICallbackEventHandler.GetCallbackResult ()
2425 return GetCallbackResult ();
2428 protected virtual string GetCallbackResult ()
2430 PrepareControlHierarchy ();
2432 StringWriter sw = new StringWriter ();
2433 sw.Write (PageIndex.ToString () + '|' + SortExpression + '|' + (int) SortDirection + '|');
2435 HtmlTextWriter writer = new HtmlTextWriter (sw);
2436 RenderGrid (writer);
2437 return sw.ToString ();
2440 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
2442 return GetCallbackScript (control, argument);
2445 protected virtual string GetCallbackScript (IButtonControl control, string argument)
2447 if (EnableSortingAndPagingCallbacks) {
2450 page.ClientScript.RegisterForEventValidation (UniqueID, argument);
2451 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
2456 protected override void OnPagePreLoad (object sender, EventArgs e)
2458 base.OnPagePreLoad (sender, e);
2461 if (page != null && page.IsPostBack && EnableSortingAndPagingCallbacks) {
2462 HttpRequest req = page.Request;
2465 if (int.TryParse (req.Form [ClientID + "_Page"], out value))
2468 if (int.TryParse (req.Form [ClientID + "_SortDirection"], out value))
2469 SortDirection = (SortDirection) value;
2470 SortExpression = req.Form [ClientID + "_SortExpression"];
2475 const string onPreRenderScript = @"var {0} = new Object ();
2476 {0}.pageIndex = {1};
2482 protected internal override void OnPreRender (EventArgs e)
2484 base.OnPreRender (e);
2486 if (EnableSortingAndPagingCallbacks) {
2488 ClientScriptManager scriptManager = page != null ? page.ClientScript : null;
2489 if (scriptManager != null) {
2490 if (!scriptManager.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
2491 string url = scriptManager.GetWebResourceUrl (typeof(GridView), "GridView.js");
2492 scriptManager.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
2495 string cgrid = ClientID + "_data";
2496 string script = String.Format (onPreRenderScript,
2498 ClientScriptManager.GetScriptLiteral (PageIndex),
2499 ClientScriptManager.GetScriptLiteral (SortExpression == null ? String.Empty : SortExpression),
2500 ClientScriptManager.GetScriptLiteral ((int) SortDirection),
2501 ClientScriptManager.GetScriptLiteral (UniqueID),
2504 scriptManager.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
2506 // Make sure the basic script infrastructure is rendered
2507 scriptManager.GetCallbackEventReference (this, "null", String.Empty, "null");
2508 scriptManager.GetPostBackClientHyperlink (this, String.Empty);
2513 protected internal override void Render (HtmlTextWriter writer)
2515 PrepareControlHierarchy ();
2516 if (EnableSortingAndPagingCallbacks)
2517 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_div");
2518 writer.RenderBeginTag (HtmlTextWriterTag.Div);
2520 RenderGrid (writer);
2522 writer.RenderEndTag ();
2525 void RenderGrid (HtmlTextWriter writer)
2530 table.Render (writer);
2533 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
2535 if (control == null)
2536 throw new ArgumentNullException ("control");
2538 if (control.CausesValidation)
2539 throw new InvalidOperationException ("A button that causes validation in GridView '" + ID + "' is attempting to use the container GridView as the post back target. The button should either turn off validation or use itself as the post back container.");
2541 PostBackOptions options = new PostBackOptions (this);
2542 options.Argument = control.CommandName + "$" + control.CommandArgument;
2543 options.RequiresJavaScriptProtocol = true;