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;
78 DataKey oldEditValues;
79 AutoGeneratedFieldProperties[] autoFieldProperties;
80 string [] dataKeyNames = null;
81 readonly string[] emptyKeys = new string[0];
83 private static readonly object PageIndexChangedEvent = new object();
84 private static readonly object PageIndexChangingEvent = new object();
85 private static readonly object RowCancelingEditEvent = new object();
86 private static readonly object RowCommandEvent = new object();
87 private static readonly object RowCreatedEvent = new object();
88 private static readonly object RowDataBoundEvent = new object();
89 private static readonly object RowDeletedEvent = new object();
90 private static readonly object RowDeletingEvent = new object();
91 private static readonly object RowEditingEvent = new object();
92 private static readonly object RowUpdatedEvent = new object();
93 private static readonly object RowUpdatingEvent = new object();
94 private static readonly object SelectedIndexChangedEvent = new object();
95 private static readonly object SelectedIndexChangingEvent = new object();
96 private static readonly object SortedEvent = new object();
97 private static readonly object SortingEvent = new object();
102 int selectedIndex = -1;
104 SortDirection sortDirection = SortDirection.Ascending;
105 string sortExpression = string.Empty;
111 public event EventHandler PageIndexChanged {
112 add { Events.AddHandler (PageIndexChangedEvent, value); }
113 remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
116 public event GridViewPageEventHandler PageIndexChanging {
117 add { Events.AddHandler (PageIndexChangingEvent, value); }
118 remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
121 public event GridViewCancelEditEventHandler RowCancelingEdit {
122 add { Events.AddHandler (RowCancelingEditEvent, value); }
123 remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
126 public event GridViewCommandEventHandler RowCommand {
127 add { Events.AddHandler (RowCommandEvent, value); }
128 remove { Events.RemoveHandler (RowCommandEvent, value); }
131 public event GridViewRowEventHandler RowCreated {
132 add { Events.AddHandler (RowCreatedEvent, value); }
133 remove { Events.RemoveHandler (RowCreatedEvent, value); }
136 public event GridViewRowEventHandler RowDataBound {
137 add { Events.AddHandler (RowDataBoundEvent, value); }
138 remove { Events.RemoveHandler (RowDataBoundEvent, value); }
141 public event GridViewDeletedEventHandler RowDeleted {
142 add { Events.AddHandler (RowDeletedEvent, value); }
143 remove { Events.RemoveHandler (RowDeletedEvent, value); }
146 public event GridViewDeleteEventHandler RowDeleting {
147 add { Events.AddHandler (RowDeletingEvent, value); }
148 remove { Events.RemoveHandler (RowDeletingEvent, value); }
151 public event GridViewEditEventHandler RowEditing {
152 add { Events.AddHandler (RowEditingEvent, value); }
153 remove { Events.RemoveHandler (RowEditingEvent, value); }
156 public event GridViewUpdatedEventHandler RowUpdated {
157 add { Events.AddHandler (RowUpdatedEvent, value); }
158 remove { Events.RemoveHandler (RowUpdatedEvent, value); }
161 public event GridViewUpdateEventHandler RowUpdating {
162 add { Events.AddHandler (RowUpdatingEvent, value); }
163 remove { Events.RemoveHandler (RowUpdatingEvent, value); }
166 public event EventHandler SelectedIndexChanged {
167 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
168 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
171 public event GridViewSelectEventHandler SelectedIndexChanging {
172 add { Events.AddHandler (SelectedIndexChangingEvent, value); }
173 remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
176 public event EventHandler Sorted {
177 add { Events.AddHandler (SortedEvent, value); }
178 remove { Events.RemoveHandler (SortedEvent, value); }
181 public event GridViewSortEventHandler Sorting {
182 add { Events.AddHandler (SortingEvent, value); }
183 remove { Events.RemoveHandler (SortingEvent, value); }
186 protected virtual void OnPageIndexChanged (EventArgs e)
188 if (Events != null) {
189 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
190 if (eh != null) eh (this, e);
194 protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
196 if (Events != null) {
197 GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
198 if (eh != null) eh (this, e);
202 protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
204 if (Events != null) {
205 GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
206 if (eh != null) eh (this, e);
210 protected virtual void OnRowCommand (GridViewCommandEventArgs e)
212 if (Events != null) {
213 GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
214 if (eh != null) eh (this, e);
218 protected virtual void OnRowCreated (GridViewRowEventArgs e)
220 if (Events != null) {
221 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
222 if (eh != null) eh (this, e);
226 protected virtual void OnRowDataBound (GridViewRowEventArgs e)
228 if (Events != null) {
229 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
230 if (eh != null) eh (this, e);
234 protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
236 if (Events != null) {
237 GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
238 if (eh != null) eh (this, e);
242 protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
244 if (Events != null) {
245 GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
246 if (eh != null) eh (this, e);
250 protected virtual void OnRowEditing (GridViewEditEventArgs e)
252 if (Events != null) {
253 GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
254 if (eh != null) eh (this, e);
258 protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
260 if (Events != null) {
261 GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
262 if (eh != null) eh (this, e);
266 protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
268 if (Events != null) {
269 GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
270 if (eh != null) eh (this, e);
274 protected virtual void OnSelectedIndexChanged (EventArgs e)
276 if (Events != null) {
277 EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
278 if (eh != null) eh (this, e);
282 protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
284 if (Events != null) {
285 GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
286 if (eh != null) eh (this, e);
290 protected virtual void OnSorted (EventArgs e)
292 if (Events != null) {
293 EventHandler eh = (EventHandler) Events [SortedEvent];
294 if (eh != null) eh (this, e);
298 protected virtual void OnSorting (GridViewSortEventArgs e)
300 if (Events != null) {
301 GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
302 if (eh != null) eh (this, e);
307 [WebCategoryAttribute ("Paging")]
308 [DefaultValueAttribute (false)]
309 public virtual bool AllowPaging {
311 object ob = ViewState ["AllowPaging"];
312 if (ob != null) return (bool) ob;
316 ViewState ["AllowPaging"] = value;
321 [WebCategoryAttribute ("Behavior")]
322 [DefaultValueAttribute (false)]
323 public virtual bool AllowSorting {
325 object ob = ViewState ["AllowSorting"];
326 if (ob != null) return (bool) ob;
330 ViewState ["AllowSorting"] = value;
335 [WebCategoryAttribute ("Styles")]
336 [PersistenceMode (PersistenceMode.InnerProperty)]
337 [NotifyParentProperty (true)]
338 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
339 public TableItemStyle AlternatingRowStyle {
341 if (alternatingRowStyle == null) {
342 alternatingRowStyle = new TableItemStyle ();
343 if (IsTrackingViewState)
344 alternatingRowStyle.TrackViewState();
346 return alternatingRowStyle;
350 [WebCategoryAttribute ("Behavior")]
351 [DefaultValueAttribute (false)]
352 public virtual bool AutoGenerateEditButton {
354 object ob = ViewState ["AutoGenerateEditButton"];
355 if (ob != null) return (bool) ob;
359 ViewState ["AutoGenerateEditButton"] = value;
364 [WebCategoryAttribute ("Behavior")]
365 [DefaultValueAttribute (false)]
366 public virtual bool AutoGenerateDeleteButton {
368 object ob = ViewState ["AutoGenerateDeleteButton"];
369 if (ob != null) return (bool) ob;
373 ViewState ["AutoGenerateDeleteButton"] = value;
378 [WebCategoryAttribute ("Behavior")]
379 [DefaultValueAttribute (false)]
380 public virtual bool AutoGenerateSelectButton {
382 object ob = ViewState ["AutoGenerateSelectButton"];
383 if (ob != null) return (bool) ob;
387 ViewState ["AutoGenerateSelectButton"] = value;
392 [WebCategoryAttribute ("Behavior")]
393 [DefaultValueAttribute (true)]
394 public virtual bool AutoGenerateColumns {
396 object ob = ViewState ["AutoGenerateColumns"];
397 if (ob != null) return (bool) ob;
401 ViewState ["AutoGenerateColumns"] = value;
406 [UrlPropertyAttribute]
407 [WebCategoryAttribute ("Appearance")]
408 [DefaultValueAttribute ("")]
409 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
410 public virtual string BackImageUrl {
412 if (ControlStyleCreated)
413 return ((TableStyle) ControlStyle).BackImageUrl;
417 ((TableStyle) ControlStyle).BackImageUrl = value;
421 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
422 [BrowsableAttribute (false)]
423 public virtual GridViewRow BottomPagerRow {
426 return bottomPagerRow;
430 [WebCategoryAttribute ("Accessibility")]
431 [DefaultValueAttribute ("")]
432 [LocalizableAttribute (true)]
433 public virtual string Caption {
435 object ob = ViewState ["Caption"];
436 if (ob != null) return (string) ob;
440 ViewState ["Caption"] = value;
445 [WebCategoryAttribute ("Accessibility")]
446 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
447 public virtual TableCaptionAlign CaptionAlign
450 object o = ViewState ["CaptionAlign"];
451 if(o != null) return (TableCaptionAlign) o;
452 return TableCaptionAlign.NotSet;
455 ViewState ["CaptionAlign"] = value;
460 [WebCategoryAttribute ("Layout")]
461 [DefaultValueAttribute (-1)]
462 public virtual int CellPadding
465 if (ControlStyleCreated)
466 return ((TableStyle) ControlStyle).CellPadding;
470 ((TableStyle) ControlStyle).CellPadding = value;
474 [WebCategoryAttribute ("Layout")]
475 [DefaultValueAttribute (0)]
476 public virtual int CellSpacing
479 if (ControlStyleCreated)
480 return ((TableStyle) ControlStyle).CellSpacing;
484 ((TableStyle) ControlStyle).CellSpacing = value;
488 [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
489 [MergablePropertyAttribute (false)]
490 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
491 [DefaultValueAttribute (null)]
492 [WebCategoryAttribute ("Misc")]
493 public virtual DataControlFieldCollection Columns {
495 if (columns == null) {
496 columns = new DataControlFieldCollection ();
497 columns.FieldsChanged += new EventHandler (OnFieldsChanged);
498 if (IsTrackingViewState)
499 ((IStateManager)columns).TrackViewState ();
505 [DefaultValueAttribute (null)]
506 [WebCategoryAttribute ("Data")]
507 [TypeConverter (typeof(StringArrayConverter))]
508 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
509 public virtual string[] DataKeyNames
512 if (dataKeyNames != null)
517 dataKeyNames = value;
522 [BrowsableAttribute (false)]
523 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
524 public virtual DataKeyArray DataKeys {
531 [WebCategoryAttribute ("Misc")]
532 [DefaultValueAttribute (-1)]
533 public virtual int EditIndex {
543 [WebCategoryAttribute ("Styles")]
544 [PersistenceMode (PersistenceMode.InnerProperty)]
545 [NotifyParentProperty (true)]
546 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
547 public TableItemStyle EditRowStyle {
549 if (editRowStyle == null) {
550 editRowStyle = new TableItemStyle ();
551 if (IsTrackingViewState)
552 editRowStyle.TrackViewState();
558 [WebCategoryAttribute ("Styles")]
559 [PersistenceMode (PersistenceMode.InnerProperty)]
560 [NotifyParentProperty (true)]
561 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
562 public TableItemStyle EmptyDataRowStyle {
564 if (emptyDataRowStyle == null) {
565 emptyDataRowStyle = new TableItemStyle ();
566 if (IsTrackingViewState)
567 emptyDataRowStyle.TrackViewState();
569 return emptyDataRowStyle;
573 [DefaultValue (null)]
574 [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
575 [PersistenceMode (PersistenceMode.InnerProperty)]
577 public virtual ITemplate EmptyDataTemplate {
578 get { return emptyDataTemplate; }
579 set { emptyDataTemplate = value; RequireBinding (); }
582 [LocalizableAttribute (true)]
583 [WebCategoryAttribute ("Appearance")]
584 [DefaultValueAttribute ("")]
585 public virtual string EmptyDataText {
587 object ob = ViewState ["EmptyDataText"];
588 if (ob != null) return (string) ob;
592 ViewState ["EmptyDataText"] = value;
597 [WebCategoryAttribute ("Behavior")]
598 [DefaultValueAttribute (false)]
599 public virtual bool EnableSortingAndPagingCallbacks {
601 object ob = ViewState ["EnableSortingAndPagingCallbacks"];
602 if (ob != null) return (bool) ob;
606 ViewState ["EnableSortingAndPagingCallbacks"] = value;
611 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
612 [BrowsableAttribute (false)]
613 public virtual GridViewRow FooterRow {
616 for (int index = table.Rows.Count - 1; index >= 0; index--) {
617 GridViewRow row = (GridViewRow) table.Rows [index];
618 switch (row.RowType) {
619 case DataControlRowType.Separator:
620 case DataControlRowType.Pager:
622 case DataControlRowType.Footer:
633 [WebCategoryAttribute ("Styles")]
634 [PersistenceMode (PersistenceMode.InnerProperty)]
635 [NotifyParentProperty (true)]
636 [DefaultValue (null)]
637 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
638 public TableItemStyle FooterStyle {
640 if (footerStyle == null) {
641 footerStyle = new TableItemStyle ();
642 if (IsTrackingViewState)
643 footerStyle.TrackViewState();
649 [WebCategoryAttribute ("Appearance")]
650 [DefaultValueAttribute (GridLines.Both)]
651 public virtual GridLines GridLines {
653 if (ControlStyleCreated)
654 return ((TableStyle) ControlStyle).GridLines;
655 return GridLines.Both;
658 ((TableStyle) ControlStyle).GridLines = value;
662 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
663 [BrowsableAttribute (false)]
664 public virtual GridViewRow HeaderRow {
667 for (int index = 0, total = table.Rows.Count; index < total; index++) {
668 GridViewRow row = (GridViewRow) table.Rows [index];
669 switch (row.RowType) {
670 case DataControlRowType.Separator:
671 case DataControlRowType.Pager:
673 case DataControlRowType.Header:
684 [WebCategoryAttribute ("Styles")]
685 [PersistenceMode (PersistenceMode.InnerProperty)]
686 [NotifyParentProperty (true)]
687 [DefaultValue (null)]
688 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
689 public TableItemStyle HeaderStyle {
691 if (headerStyle == null) {
692 headerStyle = new TableItemStyle ();
693 if (IsTrackingViewState)
694 headerStyle.TrackViewState();
700 [Category ("Layout")]
701 [DefaultValueAttribute (HorizontalAlign.NotSet)]
702 public virtual HorizontalAlign HorizontalAlign {
704 if (ControlStyleCreated)
705 return ((TableStyle) ControlStyle).HorizontalAlign;
706 return HorizontalAlign.NotSet;
709 ((TableStyle) ControlStyle).HorizontalAlign = value;
713 [BrowsableAttribute (false)]
714 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
715 public virtual int PageCount {
717 if (pageCount != 0) return pageCount;
723 [WebCategoryAttribute ("Paging")]
724 [BrowsableAttribute (true)]
725 [DefaultValueAttribute (0)]
726 public virtual int PageIndex {
736 [DefaultValueAttribute (10)]
737 [WebCategoryAttribute ("Paging")]
738 public virtual int PageSize {
740 object ob = ViewState ["PageSize"];
741 if (ob != null) return (int) ob;
745 ViewState ["PageSize"] = value;
750 [WebCategoryAttribute ("Paging")]
751 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
752 [NotifyParentPropertyAttribute (true)]
753 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
754 public virtual PagerSettings PagerSettings {
756 if (pagerSettings == null) {
757 pagerSettings = new PagerSettings (this);
758 if (IsTrackingViewState)
759 ((IStateManager)pagerSettings).TrackViewState ();
761 return pagerSettings;
765 [WebCategoryAttribute ("Styles")]
766 [PersistenceMode (PersistenceMode.InnerProperty)]
767 [NotifyParentProperty (true)]
768 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
769 public TableItemStyle PagerStyle {
771 if (pagerStyle == null) {
772 pagerStyle = new TableItemStyle ();
773 if (IsTrackingViewState)
774 pagerStyle.TrackViewState();
781 [DefaultValue (null)]
782 /* DataControlPagerCell isnt specified in the docs */
783 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
784 [PersistenceMode (PersistenceMode.InnerProperty)]
786 public virtual ITemplate PagerTemplate {
787 get { return pagerTemplate; }
788 set { pagerTemplate = value; RequireBinding (); }
791 [DefaultValueAttribute ("")]
792 [WebCategoryAttribute ("Accessibility")]
793 // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
794 public virtual string RowHeaderColumn {
796 object ob = ViewState ["RowHeaderColumn"];
797 if (ob != null) return (string) ob;
801 ViewState ["RowHeaderColumn"] = value;
806 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
807 [BrowsableAttribute (false)]
808 public virtual GridViewRowCollection Rows {
815 [WebCategoryAttribute ("Styles")]
816 [PersistenceMode (PersistenceMode.InnerProperty)]
817 [NotifyParentProperty (true)]
818 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
819 public TableItemStyle RowStyle {
821 if (rowStyle == null) {
822 rowStyle = new TableItemStyle ();
823 if (IsTrackingViewState)
824 rowStyle.TrackViewState();
830 [BrowsableAttribute (false)]
831 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
832 public virtual DataKey SelectedDataKey {
834 if (DataKeys == null)
835 throw new InvalidOperationException ("DataKeys");
837 if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
838 return DataKeys [selectedIndex];
844 [BindableAttribute (true)]
845 [DefaultValueAttribute (-1)]
846 public virtual int SelectedIndex {
848 return selectedIndex;
851 if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
852 int oldIndex = selectedIndex;
854 Rows [oldIndex].RowState = GetRowState (oldIndex);
856 selectedIndex = value;
857 if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
858 Rows [selectedIndex].RowState = GetRowState (selectedIndex);
863 [BrowsableAttribute (false)]
864 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
865 public virtual GridViewRow SelectedRow {
867 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
868 return Rows [selectedIndex];
874 [WebCategoryAttribute ("Styles")]
875 [PersistenceMode (PersistenceMode.InnerProperty)]
876 [NotifyParentProperty (true)]
877 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
878 public TableItemStyle SelectedRowStyle {
880 if (selectedRowStyle == null) {
881 selectedRowStyle = new TableItemStyle ();
882 if (IsTrackingViewState)
883 selectedRowStyle.TrackViewState();
885 return selectedRowStyle;
889 [BrowsableAttribute (false)]
890 public object SelectedValue {
892 if (SelectedDataKey != null)
893 return SelectedDataKey.Value;
899 [WebCategoryAttribute ("Appearance")]
900 [DefaultValueAttribute (false)]
901 public virtual bool ShowFooter {
903 object ob = ViewState ["ShowFooter"];
904 if (ob != null) return (bool) ob;
908 ViewState ["ShowFooter"] = value;
913 [WebCategoryAttribute ("Appearance")]
914 [DefaultValueAttribute (true)]
915 public virtual bool ShowHeader {
917 object ob = ViewState ["ShowHeader"];
918 if (ob != null) return (bool) ob;
922 ViewState ["ShowHeader"] = value;
927 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
928 [BrowsableAttribute (false)]
929 [DefaultValueAttribute (SortDirection.Ascending)]
930 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
931 public virtual SortDirection SortDirection {
932 get { return sortDirection; }
935 [BrowsableAttribute (false)]
936 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
937 public virtual string SortExpression {
938 get { return sortExpression; }
941 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
942 [BrowsableAttribute (false)]
943 public virtual GridViewRow TopPagerRow {
950 [WebCategoryAttribute ("Accessibility")]
951 [DefaultValueAttribute (true)]
952 public virtual bool UseAccessibleHeader {
954 object ob = ViewState ["UseAccessibleHeader"];
955 if (ob != null) return (bool) ob;
959 ViewState ["UseAccessibleHeader"] = value;
964 public virtual bool IsBindableType (Type type)
966 return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
969 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
971 return base.CreateDataSourceSelectArguments ();
974 protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
976 ArrayList fields = new ArrayList ();
977 fields.AddRange (Columns);
979 if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
980 CommandField field = new CommandField ();
981 field.ShowEditButton = AutoGenerateEditButton;
982 field.ShowDeleteButton = AutoGenerateDeleteButton;
983 field.ShowSelectButton = AutoGenerateSelectButton;
987 if (AutoGenerateColumns) {
989 autoFieldProperties = CreateAutoFieldProperties (dataSource);
991 if (autoFieldProperties != null) {
992 foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
993 fields.Add (CreateAutoGeneratedColumn (props));
1000 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
1002 return new AutoGeneratedField (fieldProperties);
1005 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
1007 if(source == null) return null;
1009 PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
1012 ArrayList retVal = new ArrayList();
1016 object fitem = null;
1018 PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
1019 BindingFlags.Instance | BindingFlags.Static |
1020 BindingFlags.Public, null, null,
1021 new Type[] { typeof(int) }, null);
1023 if (prop_item != null) {
1024 prop_type = prop_item.PropertyType;
1027 if (prop_type == null || prop_type == typeof(object)) {
1028 IEnumerator en = source.GetEnumerator();
1032 prop_type = fitem.GetType();
1035 if (fitem != null && fitem is ICustomTypeDescriptor) {
1036 props = TypeDescriptor.GetProperties(fitem);
1037 } else if (prop_type != null) {
1038 if (IsBindableType (prop_type)) {
1039 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1040 ((IStateManager)field).TrackViewState();
1041 field.Name = "Item";
1042 field.DataField = BoundField.ThisExpression;
1043 field.Type = prop_type;
1046 props = TypeDescriptor.GetProperties (prop_type);
1051 if (props != null && props.Count > 0)
1053 foreach (PropertyDescriptor current in props) {
1054 if (IsBindableType (current.PropertyType)) {
1055 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1056 ((IStateManager)field).TrackViewState();
1057 field.Name = current.Name;
1058 field.DataField = current.Name;
1059 field.IsReadOnly = current.IsReadOnly;
1060 field.Type = current.PropertyType;
1066 if (retVal.Count > 0)
1067 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1069 return new AutoGeneratedFieldProperties [0];
1072 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1074 GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1075 OnRowCreated (new GridViewRowEventArgs (row));
1079 void RequireBinding ()
1082 RequiresDataBinding = true;
1087 protected virtual Table CreateChildTable ()
1089 return new ContainedTable (this);
1092 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1094 PagedDataSource dataSource;
1097 DataSourceView view = GetData ();
1098 dataSource = new PagedDataSource ();
1099 dataSource.DataSource = data;
1102 dataSource.AllowPaging = true;
1103 dataSource.PageSize = PageSize;
1104 dataSource.CurrentPageIndex = PageIndex;
1106 dataSource.AllowServerPaging = true;
1107 if (view.CanRetrieveTotalRowCount)
1108 dataSource.VirtualCount = SelectArguments.TotalRowCount;
1112 pageCount = dataSource.PageCount;
1116 dataSource = new PagedDataSource ();
1117 dataSource.DataSource = data;
1119 dataSource.AllowPaging = true;
1120 dataSource.PageSize = PageSize;
1121 dataSource.CurrentPageIndex = PageIndex;
1125 bool showPager = AllowPaging && (PageCount > 1);
1128 table = CreateChildTable ();
1129 Controls.Add (table);
1131 ArrayList list = new ArrayList ();
1132 ArrayList keyList = new ArrayList ();
1134 // Creates the set of fields to show
1136 ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1137 DataControlField[] fields = new DataControlField [fieldCollection.Count];
1138 fieldCollection.CopyTo (fields, 0);
1140 foreach (DataControlField field in fields) {
1141 field.Initialize (AllowSorting, this);
1142 if (EnableSortingAndPagingCallbacks)
1143 field.ValidateSupportsCallback ();
1146 // Main table creation
1148 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
1149 topPagerRow = CreatePagerRow (fields.Length, dataSource);
1150 table.Rows.Add (topPagerRow);
1153 GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
1154 table.Rows.Add (headerRow);
1155 InitializeRow (headerRow, fields);
1157 foreach (object obj in dataSource) {
1158 DataControlRowState rstate = GetRowState (list.Count);
1159 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1162 table.Rows.Add (row);
1163 InitializeRow (row, fields);
1166 OnRowDataBound (new GridViewRowEventArgs (row));
1167 if (EditIndex == row.RowIndex)
1168 oldEditValues = new DataKey (GetRowValues (row, false, true));
1169 keyList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
1171 if (EditIndex == row.RowIndex)
1172 oldEditValues = new DataKey (new OrderedDictionary ());
1173 keyList.Add (new DataKey (new OrderedDictionary (), DataKeyNames));
1176 if (list.Count >= PageSize)
1180 if (list.Count == 0)
1181 table.Rows.Add (CreateEmptyrRow (fields.Length));
1183 GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
1184 table.Rows.Add (footerRow);
1185 InitializeRow (footerRow, fields);
1187 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
1188 bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
1189 table.Rows.Add (bottomPagerRow);
1192 rows = new GridViewRowCollection (list);
1193 keys = new DataKeyArray (keyList);
1195 return dataSource.DataSourceCount;
1198 protected override Style CreateControlStyle ()
1200 TableStyle style = new TableStyle (ViewState);
1201 style.GridLines = GridLines.Both;
1202 style.CellSpacing = 0;
1206 DataControlRowState GetRowState (int index)
1208 DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1209 if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
1210 if (index == EditIndex) rstate |= DataControlRowState.Edit;
1214 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1216 GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1217 InitializePager (row, fieldCount, dataSource);
1221 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1223 TableCell cell = new TableCell ();
1225 cell.ColumnSpan = columnSpan;
1227 if (pagerTemplate != null)
1228 pagerTemplate.InstantiateIn (cell);
1230 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount, pagerStyle));
1232 row.Cells.Add (cell);
1235 GridViewRow CreateEmptyrRow (int fieldCount)
1237 GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1238 TableCell cell = new TableCell ();
1239 cell.ColumnSpan = fieldCount;
1241 if (emptyDataTemplate != null)
1242 emptyDataTemplate.InstantiateIn (cell);
1244 cell.Text = EmptyDataText;
1246 row.Cells.Add (cell);
1250 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1252 DataControlCellType ctype;
1253 bool accessibleHeader = false;
1255 switch (row.RowType) {
1256 case DataControlRowType.Header:
1257 ctype = DataControlCellType.Header;
1258 accessibleHeader = UseAccessibleHeader;
1260 case DataControlRowType.Footer:
1261 ctype = DataControlCellType.Footer;
1264 ctype = DataControlCellType.DataCell;
1268 for (int n=0; n<fields.Length; n++) {
1269 DataControlField field = fields [n];
1270 DataControlFieldCell cell;
1271 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1272 cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1274 cell = new DataControlFieldCell (field);
1275 row.Cells.Add (cell);
1276 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1280 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1282 if (cachedKeyProperties == null) {
1283 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
1284 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1285 for (int n=0; n<DataKeyNames.Length; n++) {
1286 PropertyDescriptor p = props [DataKeyNames[n]];
1288 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + row.DataItem.GetType());
1289 cachedKeyProperties [n] = p;
1293 OrderedDictionary dic = new OrderedDictionary ();
1294 foreach (PropertyDescriptor p in cachedKeyProperties)
1295 dic [p.Name] = p.GetValue (row.DataItem);
1299 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1301 OrderedDictionary dic = new OrderedDictionary ();
1302 ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1306 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1308 foreach (TableCell cell in row.Cells) {
1309 DataControlFieldCell c = cell as DataControlFieldCell;
1311 c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1313 if (!includePrimaryKey && DataKeyNames != null)
1314 foreach (string key in DataKeyNames)
1315 fieldValues.Remove (key);
1318 protected override HtmlTextWriterTag TagKey {
1320 if (EnableSortingAndPagingCallbacks)
1321 return HtmlTextWriterTag.Div;
1323 return HtmlTextWriterTag.Table;
1327 public sealed override void DataBind ()
1329 DataSourceView view = GetData ();
1330 if (AllowPaging && view.CanPage) {
1331 SelectArguments.StartRowIndex = PageIndex * PageSize;
1332 SelectArguments.MaximumRows = PageSize;
1333 if (view.CanRetrieveTotalRowCount)
1334 SelectArguments.RetrieveTotalRowCount = true;
1337 if (sortExpression != "") {
1338 if (sortDirection == SortDirection.Ascending)
1339 SelectArguments.SortExpression = sortExpression;
1341 SelectArguments.SortExpression = sortExpression + " DESC";
1344 cachedKeyProperties = null;
1348 protected internal override void PerformDataBinding (IEnumerable data)
1350 base.PerformDataBinding (data);
1354 protected internal virtual void PrepareControlHierarchy ()
1356 throw new NotImplementedException ();
1359 protected internal override void OnInit (EventArgs e)
1361 Page.RegisterRequiresControlState (this);
1365 void OnFieldsChanged (object sender, EventArgs args)
1370 protected override void OnDataPropertyChanged ()
1372 base.OnDataPropertyChanged ();
1376 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1378 base.OnDataSourceViewChanged (sender, e);
1382 protected override bool OnBubbleEvent (object source, EventArgs e)
1384 GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1386 OnRowCommand (args);
1387 string param = args.CommandArgument as string;
1388 if (param == null || param.Length == 0) {
1389 GridViewRow row = args.Row;
1391 param = row.RowIndex.ToString();
1393 ProcessEvent (args.CommandName, param);
1395 return base.OnBubbleEvent (source, e);
1398 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
1400 RaisePostBackEvent (eventArgument);
1403 // This is prolly obsolete
1404 protected virtual void RaisePostBackEvent (string eventArgument)
1406 int i = eventArgument.IndexOf ('$');
1408 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1410 ProcessEvent (eventArgument, null);
1413 void ProcessEvent (string eventName, string param)
1417 case DataControlCommands.PageCommandName:
1420 case DataControlCommands.FirstPageCommandArgument:
1423 case DataControlCommands.LastPageCommandArgument:
1424 newIndex = PageCount - 1;
1426 case DataControlCommands.NextPageCommandArgument:
1427 if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1429 case DataControlCommands.PreviousPageCommandArgument:
1430 if (PageIndex > 0) newIndex = PageIndex - 1;
1433 newIndex = int.Parse (param) - 1;
1436 ShowPage (newIndex);
1439 case DataControlCommands.FirstPageCommandArgument:
1443 case DataControlCommands.LastPageCommandArgument:
1444 ShowPage (PageCount - 1);
1447 case DataControlCommands.NextPageCommandArgument:
1448 if (PageIndex < PageCount - 1)
1449 ShowPage (PageIndex + 1);
1452 case DataControlCommands.PreviousPageCommandArgument:
1454 ShowPage (PageIndex - 1);
1457 case DataControlCommands.SelectCommandName:
1458 SelectRow (int.Parse (param));
1461 case DataControlCommands.EditCommandName:
1462 EditRow (int.Parse (param));
1465 case DataControlCommands.UpdateCommandName:
1466 UpdateRow (EditIndex, true);
1469 case DataControlCommands.CancelCommandName:
1473 case DataControlCommands.DeleteCommandName:
1474 DeleteRow (int.Parse (param));
1477 case DataControlCommands.SortCommandName:
1483 void Sort (string newSortExpression)
1485 SortDirection newDirection;
1486 if (sortExpression == newSortExpression) {
1487 if (sortDirection == SortDirection.Ascending)
1488 newDirection = SortDirection.Descending;
1490 newDirection = SortDirection.Ascending;
1492 newDirection = sortDirection;
1494 Sort (newSortExpression, newDirection);
1497 public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
1499 GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
1501 if (args.Cancel) return;
1503 sortExpression = args.SortExpression;
1504 sortDirection = args.SortDirection;
1507 OnSorted (EventArgs.Empty);
1510 void SelectRow (int index)
1512 GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
1513 OnSelectedIndexChanging (args);
1515 SelectedIndex = args.NewSelectedIndex;
1516 OnSelectedIndexChanged (EventArgs.Empty);
1520 void ShowPage (int newIndex)
1522 GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
1523 OnPageIndexChanging (args);
1526 PageIndex = args.NewPageIndex;
1527 OnPageIndexChanged (EventArgs.Empty);
1531 void EditRow (int index)
1533 GridViewEditEventArgs args = new GridViewEditEventArgs (index);
1534 OnRowEditing (args);
1536 EditIndex = args.NewEditIndex;
1542 GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
1543 OnRowCancelingEdit (args);
1549 [MonoTODO ("Support two-way binding expressions")]
1550 public virtual void UpdateRow (int rowIndex, bool causesValidation)
1552 if (causesValidation)
1555 if (rowIndex != EditIndex) throw new NotSupportedException ();
1557 currentEditOldValues = oldEditValues.Values;
1559 GridViewRow row = Rows [rowIndex];
1560 currentEditRowKeys = DataKeys [rowIndex].Values;
1561 currentEditNewValues = GetRowValues (row, false, false);
1563 GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (EditIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1564 OnRowUpdating (args);
1566 DataSourceView view = GetData ();
1567 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1568 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1573 bool UpdateCallback (int recordsAffected, Exception exception)
1575 GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1576 OnRowUpdated (dargs);
1578 if (!dargs.KeepInEditMode)
1581 return dargs.ExceptionHandled;
1584 public virtual void DeleteRow (int rowIndex)
1586 GridViewRow row = Rows [rowIndex];
1587 currentEditRowKeys = DataKeys [rowIndex].Values;
1588 currentEditNewValues = GetRowValues (row, true, true);
1590 GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
1591 OnRowDeleting (args);
1595 DataSourceView view = GetData ();
1597 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1599 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1600 OnRowDeleted (dargs);
1605 bool DeleteCallback (int recordsAffected, Exception exception)
1607 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1608 OnRowDeleted (dargs);
1609 return dargs.ExceptionHandled;
1615 oldEditValues = new DataKey (new OrderedDictionary ());
1616 currentEditRowKeys = null;
1617 currentEditOldValues = null;
1618 currentEditNewValues = null;
1621 protected internal override void LoadControlState (object ob)
1623 if (ob == null) return;
1624 object[] state = (object[]) ob;
1625 base.LoadControlState (state[0]);
1626 pageIndex = (int) state[1];
1627 pageCount = (int) state[2];
1628 selectedIndex = (int) state[3];
1629 editIndex = (int) state[4];
1630 sortExpression = (string) state[5];
1631 sortDirection = (SortDirection) state[6];
1632 DataKeyNames = (string []) state [7];
1635 protected internal override object SaveControlState ()
1637 object bstate = base.SaveControlState ();
1638 return new object[] {
1639 bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection, DataKeyNames
1643 protected override void TrackViewState()
1645 base.TrackViewState();
1646 if (columns != null) ((IStateManager)columns).TrackViewState();
1647 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1648 if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
1649 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1650 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1651 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1652 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1653 if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
1654 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1655 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1656 if (keys != null) ((IStateManager)keys).TrackViewState();
1657 if (autoFieldProperties != null) {
1658 foreach (IStateManager sm in autoFieldProperties)
1659 sm.TrackViewState ();
1663 protected override object SaveViewState()
1665 object[] states = new object [14];
1666 states[0] = base.SaveViewState();
1667 states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
1668 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1669 states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
1670 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1671 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1672 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1673 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1674 states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
1675 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1676 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1677 states[11] = (keys == null ? null : ((IStateManager)keys).SaveViewState());
1678 states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1680 if (autoFieldProperties != null) {
1681 object[] data = new object [autoFieldProperties.Length];
1682 bool allNull = true;
1683 for (int n=0; n<data.Length; n++) {
1684 data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
1685 if (data [n] != null) allNull = false;
1687 if (!allNull) states [13] = data;
1690 for (int i = states.Length - 1; i >= 0; i--) {
1691 if (states [i] != null)
1698 protected override void LoadViewState (object savedState)
1700 if (savedState == null) {
1701 base.LoadViewState (null);
1705 object [] states = (object []) savedState;
1707 if (states[13] != null) {
1708 object[] data = (object[]) states [13];
1709 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
1710 for (int n=0; n<data.Length; n++) {
1711 IStateManager p = new AutoGeneratedFieldProperties ();
1712 p.TrackViewState ();
1713 p.LoadViewState (data [n]);
1714 autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
1718 base.LoadViewState (states[0]);
1719 EnsureChildControls ();
1721 if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
1722 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1723 if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
1724 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1725 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1726 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1727 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1728 if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
1729 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1730 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1731 if (states[11] != null) ((IStateManager)DataKeys).LoadViewState (states[11]);
1732 if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1735 void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
1737 RaiseCallbackEvent (eventArgs);
1740 string callbackResult;
1741 protected virtual void RaiseCallbackEvent (string eventArgs)
1743 string[] clientData = eventArgs.Split ('|');
1744 pageIndex = int.Parse (clientData[0]);
1745 sortExpression = HttpUtility.UrlDecode (clientData[1]);
1746 if (sortExpression == "") sortExpression = null;
1749 RaisePostBackEvent (clientData[2]);
1752 StringWriter sw = new StringWriter ();
1753 sw.Write (PageIndex.ToString() + '|' + SortExpression + '|');
1755 HtmlTextWriter writer = new HtmlTextWriter (sw);
1756 RenderGrid (writer);
1757 callbackResult = sw.ToString ();
1760 string ICallbackEventHandler.GetCallbackResult ()
1762 return GetCallbackResult ();
1765 protected virtual string GetCallbackResult ()
1767 return callbackResult;
1770 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
1772 return GetCallbackScript (control, argument);
1775 protected virtual string GetCallbackScript (IButtonControl control, string argument)
1777 if (EnableSortingAndPagingCallbacks)
1778 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
1784 protected override void OnPagePreLoad (object sender, EventArgs e)
1786 base.OnPagePreLoad (sender, e);
1789 protected internal override void OnPreRender (EventArgs e)
1791 base.OnPreRender (e);
1793 if (EnableSortingAndPagingCallbacks)
1795 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
1796 string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
1797 Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
1800 string cgrid = ClientID + "_data";
1801 string script = string.Format ("var {0} = new Object ();\n", cgrid);
1802 script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
1803 script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
1804 script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
1805 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
1807 // Make sure the basic script infrastructure is rendered
1808 Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
1809 Page.ClientScript.GetPostBackClientHyperlink (this, "");
1813 protected internal override void Render (HtmlTextWriter writer)
1815 if (EnableSortingAndPagingCallbacks)
1816 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
1817 writer.RenderBeginTag (HtmlTextWriterTag.Div);
1819 RenderGrid (writer);
1821 writer.RenderEndTag ();
1824 void RenderGrid (HtmlTextWriter writer)
1829 table.Caption = Caption;
1830 table.CaptionAlign = CaptionAlign;
1831 table.RenderBeginTag (writer);
1833 foreach (GridViewRow row in table.Rows)
1835 switch (row.RowType) {
1836 case DataControlRowType.Header:
1837 if (!ShowHeader) continue;
1838 if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1840 case DataControlRowType.Footer:
1841 if (!ShowFooter) continue;
1842 if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1844 case DataControlRowType.Pager:
1845 if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1847 case DataControlRowType.EmptyDataRow:
1848 if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1854 if ((row.RowState & DataControlRowState.Normal) != 0 && rowStyle != null)
1855 rowStyle.AddAttributesToRender (writer, row);
1856 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null)
1857 alternatingRowStyle.AddAttributesToRender (writer, row);
1858 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1859 editRowStyle.AddAttributesToRender (writer, row);
1860 if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null)
1861 selectedRowStyle.AddAttributesToRender (writer, row);
1863 row.RenderBeginTag (writer);
1865 foreach (TableCell cell in row.Cells) {
1866 DataControlFieldCell fcell = cell as DataControlFieldCell;
1867 if (fcell != null) {
1868 Style cellStyle = null;
1869 switch (row.RowType) {
1870 case DataControlRowType.Header: cellStyle = fcell.ContainingField.HeaderStyle; break;
1871 case DataControlRowType.Footer: cellStyle = fcell.ContainingField.FooterStyle; break;
1872 default: cellStyle = fcell.ContainingField.ItemStyle; break;
1874 if (cellStyle != null)
1875 cellStyle.AddAttributesToRender (writer, cell);
1877 cell.Render (writer);
1879 row.RenderEndTag (writer);
1881 table.RenderEndTag (writer);
1885 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
1887 PostBackOptions pbo = new PostBackOptions (this, control.CommandName + "$" + control.CommandArgument);