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 readonly string[] emptyKeys = new string[0];
82 private static readonly object PageIndexChangedEvent = new object();
83 private static readonly object PageIndexChangingEvent = new object();
84 private static readonly object RowCancelingEditEvent = new object();
85 private static readonly object RowCommandEvent = new object();
86 private static readonly object RowCreatedEvent = new object();
87 private static readonly object RowDataBoundEvent = new object();
88 private static readonly object RowDeletedEvent = new object();
89 private static readonly object RowDeletingEvent = new object();
90 private static readonly object RowEditingEvent = new object();
91 private static readonly object RowUpdatedEvent = new object();
92 private static readonly object RowUpdatingEvent = new object();
93 private static readonly object SelectedIndexChangedEvent = new object();
94 private static readonly object SelectedIndexChangingEvent = new object();
95 private static readonly object SortedEvent = new object();
96 private static readonly object SortingEvent = new object();
101 int selectedIndex = -1;
103 SortDirection sortDirection = SortDirection.Ascending;
104 string sortExpression;
110 public event EventHandler PageIndexChanged {
111 add { Events.AddHandler (PageIndexChangedEvent, value); }
112 remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
115 public event GridViewPageEventHandler PageIndexChanging {
116 add { Events.AddHandler (PageIndexChangingEvent, value); }
117 remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
120 public event GridViewCancelEditEventHandler RowCancelingEdit {
121 add { Events.AddHandler (RowCancelingEditEvent, value); }
122 remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
125 public event GridViewCommandEventHandler RowCommand {
126 add { Events.AddHandler (RowCommandEvent, value); }
127 remove { Events.RemoveHandler (RowCommandEvent, value); }
130 public event GridViewRowEventHandler RowCreated {
131 add { Events.AddHandler (RowCreatedEvent, value); }
132 remove { Events.RemoveHandler (RowCreatedEvent, value); }
135 public event GridViewRowEventHandler RowDataBound {
136 add { Events.AddHandler (RowDataBoundEvent, value); }
137 remove { Events.RemoveHandler (RowDataBoundEvent, value); }
140 public event GridViewDeletedEventHandler RowDeleted {
141 add { Events.AddHandler (RowDeletedEvent, value); }
142 remove { Events.RemoveHandler (RowDeletedEvent, value); }
145 public event GridViewDeleteEventHandler RowDeleting {
146 add { Events.AddHandler (RowDeletingEvent, value); }
147 remove { Events.RemoveHandler (RowDeletingEvent, value); }
150 public event GridViewEditEventHandler RowEditing {
151 add { Events.AddHandler (RowEditingEvent, value); }
152 remove { Events.RemoveHandler (RowEditingEvent, value); }
155 public event GridViewUpdatedEventHandler RowUpdated {
156 add { Events.AddHandler (RowUpdatedEvent, value); }
157 remove { Events.RemoveHandler (RowUpdatedEvent, value); }
160 public event GridViewUpdateEventHandler RowUpdating {
161 add { Events.AddHandler (RowUpdatingEvent, value); }
162 remove { Events.RemoveHandler (RowUpdatingEvent, value); }
165 public event EventHandler SelectedIndexChanged {
166 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
167 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
170 public event GridViewSelectEventHandler SelectedIndexChanging {
171 add { Events.AddHandler (SelectedIndexChangingEvent, value); }
172 remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
175 public event EventHandler Sorted {
176 add { Events.AddHandler (SortedEvent, value); }
177 remove { Events.RemoveHandler (SortedEvent, value); }
180 public event GridViewSortEventHandler Sorting {
181 add { Events.AddHandler (SortingEvent, value); }
182 remove { Events.RemoveHandler (SortingEvent, value); }
185 protected virtual void OnPageIndexChanged (EventArgs e)
187 if (Events != null) {
188 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
189 if (eh != null) eh (this, e);
193 protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
195 if (Events != null) {
196 GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
197 if (eh != null) eh (this, e);
201 protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
203 if (Events != null) {
204 GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
205 if (eh != null) eh (this, e);
209 protected virtual void OnRowCommand (GridViewCommandEventArgs e)
211 if (Events != null) {
212 GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
213 if (eh != null) eh (this, e);
217 protected virtual void OnRowCreated (GridViewRowEventArgs e)
219 if (Events != null) {
220 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
221 if (eh != null) eh (this, e);
225 protected virtual void OnRowDataBound (GridViewRowEventArgs e)
227 if (Events != null) {
228 GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
229 if (eh != null) eh (this, e);
233 protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
235 if (Events != null) {
236 GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
237 if (eh != null) eh (this, e);
241 protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
243 if (Events != null) {
244 GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
245 if (eh != null) eh (this, e);
249 protected virtual void OnRowEditing (GridViewEditEventArgs e)
251 if (Events != null) {
252 GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
253 if (eh != null) eh (this, e);
257 protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
259 if (Events != null) {
260 GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
261 if (eh != null) eh (this, e);
265 protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
267 if (Events != null) {
268 GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
269 if (eh != null) eh (this, e);
273 protected virtual void OnSelectedIndexChanged (EventArgs e)
275 if (Events != null) {
276 EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
277 if (eh != null) eh (this, e);
281 protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
283 if (Events != null) {
284 GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
285 if (eh != null) eh (this, e);
289 protected virtual void OnSorted (EventArgs e)
291 if (Events != null) {
292 EventHandler eh = (EventHandler) Events [SortedEvent];
293 if (eh != null) eh (this, e);
297 protected virtual void OnSorting (GridViewSortEventArgs e)
299 if (Events != null) {
300 GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
301 if (eh != null) eh (this, e);
306 [WebCategoryAttribute ("Paging")]
307 [DefaultValueAttribute (false)]
308 public virtual bool AllowPaging {
310 object ob = ViewState ["AllowPaging"];
311 if (ob != null) return (bool) ob;
315 ViewState ["AllowPaging"] = value;
320 [WebCategoryAttribute ("Behavior")]
321 [DefaultValueAttribute (false)]
322 public virtual bool AllowSorting {
324 object ob = ViewState ["AllowSorting"];
325 if (ob != null) return (bool) ob;
329 ViewState ["AllowSorting"] = value;
334 [WebCategoryAttribute ("Styles")]
335 [PersistenceMode (PersistenceMode.InnerProperty)]
336 [NotifyParentProperty (true)]
337 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
338 public TableItemStyle AlternatingRowStyle {
340 if (alternatingRowStyle == null) {
341 alternatingRowStyle = new TableItemStyle ();
342 if (IsTrackingViewState)
343 alternatingRowStyle.TrackViewState();
345 return alternatingRowStyle;
349 [WebCategoryAttribute ("Behavior")]
350 [DefaultValueAttribute (false)]
351 public virtual bool AutoGenerateEditButton {
353 object ob = ViewState ["AutoGenerateEditButton"];
354 if (ob != null) return (bool) ob;
358 ViewState ["AutoGenerateEditButton"] = value;
363 [WebCategoryAttribute ("Behavior")]
364 [DefaultValueAttribute (false)]
365 public virtual bool AutoGenerateDeleteButton {
367 object ob = ViewState ["AutoGenerateDeleteButton"];
368 if (ob != null) return (bool) ob;
372 ViewState ["AutoGenerateDeleteButton"] = value;
377 [WebCategoryAttribute ("Behavior")]
378 [DefaultValueAttribute (false)]
379 public virtual bool AutoGenerateSelectButton {
381 object ob = ViewState ["AutoGenerateSelectButton"];
382 if (ob != null) return (bool) ob;
386 ViewState ["AutoGenerateSelectButton"] = value;
391 [WebCategoryAttribute ("Behavior")]
392 [DefaultValueAttribute (true)]
393 public virtual bool AutoGenerateColumns {
395 object ob = ViewState ["AutoGenerateColumns"];
396 if (ob != null) return (bool) ob;
400 ViewState ["AutoGenerateColumns"] = value;
405 [UrlPropertyAttribute]
406 [WebCategoryAttribute ("Appearance")]
407 [DefaultValueAttribute ("")]
408 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
409 public virtual string BackImageUrl {
411 object ob = ViewState ["BackImageUrl"];
412 if (ob != null) return (string) ob;
416 ViewState ["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 object o = ViewState ["CellPadding"];
466 if (o != null) return (int) o;
470 ViewState ["CellPadding"] = value;
475 [WebCategoryAttribute ("Layout")]
476 [DefaultValueAttribute (0)]
477 public virtual int CellSpacing
480 object o = ViewState ["CellSpacing"];
481 if (o != null) return (int) o;
485 ViewState ["CellSpacing"] = value;
490 [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
491 [MergablePropertyAttribute (false)]
492 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
493 [DefaultValueAttribute (null)]
494 [WebCategoryAttribute ("Misc")]
495 public virtual DataControlFieldCollection Columns {
497 if (columns == null) {
498 columns = new DataControlFieldCollection ();
499 columns.FieldsChanged += new EventHandler (OnFieldsChanged);
500 if (IsTrackingViewState)
501 ((IStateManager)columns).TrackViewState ();
507 [DefaultValueAttribute (null)]
508 [WebCategoryAttribute ("Data")]
509 [TypeConverter (typeof(StringArrayConverter))]
510 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
511 public virtual string[] DataKeyNames
514 object o = ViewState ["DataKeyNames"];
515 if (o != null) return (string[]) o;
519 ViewState ["DataKeyNames"] = value;
524 [BrowsableAttribute (false)]
525 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
526 public virtual DataKeyArray DataKeys {
533 [WebCategoryAttribute ("Misc")]
534 [DefaultValueAttribute (-1)]
535 public virtual int EditIndex {
545 [WebCategoryAttribute ("Styles")]
546 [PersistenceMode (PersistenceMode.InnerProperty)]
547 [NotifyParentProperty (true)]
548 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
549 public TableItemStyle EditRowStyle {
551 if (editRowStyle == null) {
552 editRowStyle = new TableItemStyle ();
553 if (IsTrackingViewState)
554 editRowStyle.TrackViewState();
560 [WebCategoryAttribute ("Styles")]
561 [PersistenceMode (PersistenceMode.InnerProperty)]
562 [NotifyParentProperty (true)]
563 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
564 public TableItemStyle EmptyDataRowStyle {
566 if (emptyDataRowStyle == null) {
567 emptyDataRowStyle = new TableItemStyle ();
568 if (IsTrackingViewState)
569 emptyDataRowStyle.TrackViewState();
571 return emptyDataRowStyle;
575 [DefaultValue (null)]
576 [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
577 [PersistenceMode (PersistenceMode.InnerProperty)]
579 public virtual ITemplate EmptyDataTemplate {
580 get { return emptyDataTemplate; }
581 set { emptyDataTemplate = value; RequireBinding (); }
584 [LocalizableAttribute (true)]
585 [WebCategoryAttribute ("Appearance")]
586 [DefaultValueAttribute ("")]
587 public virtual string EmptyDataText {
589 object ob = ViewState ["EmptyDataText"];
590 if (ob != null) return (string) ob;
594 ViewState ["EmptyDataText"] = value;
599 [WebCategoryAttribute ("Behavior")]
600 [DefaultValueAttribute (false)]
601 public virtual bool EnableSortingAndPagingCallbacks {
603 object ob = ViewState ["EnableSortingAndPagingCallbacks"];
604 if (ob != null) return (bool) ob;
608 ViewState ["EnableSortingAndPagingCallbacks"] = value;
613 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
614 [BrowsableAttribute (false)]
615 public virtual GridViewRow FooterRow {
618 for (int index = table.Rows.Count - 1; index >= 0; index--) {
619 GridViewRow row = (GridViewRow) table.Rows [index];
620 switch (row.RowType) {
621 case DataControlRowType.Separator:
622 case DataControlRowType.Pager:
624 case DataControlRowType.Footer:
635 [WebCategoryAttribute ("Styles")]
636 [PersistenceMode (PersistenceMode.InnerProperty)]
637 [NotifyParentProperty (true)]
638 [DefaultValue (null)]
639 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
640 public TableItemStyle FooterStyle {
642 if (footerStyle == null) {
643 footerStyle = new TableItemStyle ();
644 if (IsTrackingViewState)
645 footerStyle.TrackViewState();
651 [WebCategoryAttribute ("Appearance")]
652 [DefaultValueAttribute (GridLines.Both)]
653 public virtual GridLines GridLines {
655 object ob = ViewState ["GridLines"];
656 if (ob != null) return (GridLines) ob;
657 return GridLines.Both;
660 ViewState ["GridLines"] = value;
664 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
665 [BrowsableAttribute (false)]
666 public virtual GridViewRow HeaderRow {
669 for (int index = 0, total = table.Rows.Count; index < total; index++) {
670 GridViewRow row = (GridViewRow) table.Rows [index];
671 switch (row.RowType) {
672 case DataControlRowType.Separator:
673 case DataControlRowType.Pager:
675 case DataControlRowType.Header:
686 [WebCategoryAttribute ("Styles")]
687 [PersistenceMode (PersistenceMode.InnerProperty)]
688 [NotifyParentProperty (true)]
689 [DefaultValue (null)]
690 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
691 public TableItemStyle HeaderStyle {
693 if (headerStyle == null) {
694 headerStyle = new TableItemStyle ();
695 if (IsTrackingViewState)
696 headerStyle.TrackViewState();
702 [Category ("Layout")]
703 [DefaultValueAttribute (HorizontalAlign.NotSet)]
704 public virtual HorizontalAlign HorizontalAlign {
706 object ob = ViewState ["HorizontalAlign"];
707 if (ob != null) return (HorizontalAlign) ob;
708 return HorizontalAlign.NotSet;
711 ViewState ["HorizontalAlign"] = value;
716 [BrowsableAttribute (false)]
717 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
718 public virtual int PageCount {
720 if (pageCount != -1) return pageCount;
726 [WebCategoryAttribute ("Paging")]
727 [BrowsableAttribute (true)]
728 [DefaultValueAttribute (0)]
729 public virtual int PageIndex {
739 [DefaultValueAttribute (10)]
740 [WebCategoryAttribute ("Paging")]
741 public virtual int PageSize {
743 object ob = ViewState ["PageSize"];
744 if (ob != null) return (int) ob;
748 ViewState ["PageSize"] = value;
753 [WebCategoryAttribute ("Paging")]
754 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
755 [NotifyParentPropertyAttribute (true)]
756 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
757 public virtual PagerSettings PagerSettings {
759 if (pagerSettings == null) {
760 pagerSettings = new PagerSettings (this);
761 if (IsTrackingViewState)
762 ((IStateManager)pagerSettings).TrackViewState ();
764 return pagerSettings;
768 [WebCategoryAttribute ("Styles")]
769 [PersistenceMode (PersistenceMode.InnerProperty)]
770 [NotifyParentProperty (true)]
771 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
772 public TableItemStyle PagerStyle {
774 if (pagerStyle == null) {
775 pagerStyle = new TableItemStyle ();
776 if (IsTrackingViewState)
777 pagerStyle.TrackViewState();
784 [DefaultValue (null)]
785 /* DataControlPagerCell isnt specified in the docs */
786 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
787 [PersistenceMode (PersistenceMode.InnerProperty)]
789 public virtual ITemplate PagerTemplate {
790 get { return pagerTemplate; }
791 set { pagerTemplate = value; RequireBinding (); }
794 [DefaultValueAttribute ("")]
795 [WebCategoryAttribute ("Accessibility")]
796 // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
797 public virtual string RowHeaderColumn {
799 object ob = ViewState ["RowHeaderColumn"];
800 if (ob != null) return (string) ob;
804 ViewState ["RowHeaderColumn"] = value;
809 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
810 [BrowsableAttribute (false)]
811 public virtual GridViewRowCollection Rows {
818 [WebCategoryAttribute ("Styles")]
819 [PersistenceMode (PersistenceMode.InnerProperty)]
820 [NotifyParentProperty (true)]
821 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
822 public TableItemStyle RowStyle {
824 if (rowStyle == null) {
825 rowStyle = new TableItemStyle ();
826 if (IsTrackingViewState)
827 rowStyle.TrackViewState();
833 [BrowsableAttribute (false)]
834 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
835 public virtual DataKey SelectedDataKey {
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 ();
978 if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
979 CommandField field = new CommandField ();
980 field.ShowEditButton = AutoGenerateEditButton;
981 field.ShowDeleteButton = AutoGenerateDeleteButton;
982 field.ShowSelectButton = AutoGenerateSelectButton;
986 if (AutoGenerateColumns) {
988 autoFieldProperties = CreateAutoFieldProperties (dataSource);
990 if (autoFieldProperties != null) {
991 foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
992 fields.Add (CreateAutoGeneratedColumn (props));
996 fields.AddRange (Columns);
1001 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
1003 return new AutoGeneratedField (fieldProperties);
1006 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
1008 if(source == null) return null;
1010 PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
1013 ArrayList retVal = new ArrayList();
1017 object fitem = null;
1019 PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
1020 BindingFlags.Instance | BindingFlags.Static |
1021 BindingFlags.Public, null, null,
1022 new Type[] { typeof(int) }, null);
1024 if (prop_item != null) {
1025 prop_type = prop_item.PropertyType;
1028 if (prop_type == null || prop_type == typeof(object)) {
1029 IEnumerator en = source.GetEnumerator();
1033 prop_type = fitem.GetType();
1036 if (fitem != null && fitem is ICustomTypeDescriptor) {
1037 props = TypeDescriptor.GetProperties(fitem);
1038 } else if (prop_type != null) {
1039 if (IsBindableType (prop_type)) {
1040 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1041 ((IStateManager)field).TrackViewState();
1042 field.Name = "Item";
1043 field.DataField = BoundField.ThisExpression;
1044 field.Type = prop_type;
1047 props = TypeDescriptor.GetProperties (prop_type);
1052 if (props != null && props.Count > 0)
1054 foreach (PropertyDescriptor current in props) {
1055 if (IsBindableType (current.PropertyType)) {
1056 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1057 ((IStateManager)field).TrackViewState();
1058 field.Name = current.Name;
1059 field.DataField = current.Name;
1060 field.IsReadOnly = current.IsReadOnly;
1061 field.Type = current.PropertyType;
1067 if (retVal.Count > 0)
1068 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1070 return new AutoGeneratedFieldProperties [0];
1073 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1075 GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1076 OnRowCreated (new GridViewRowEventArgs (row));
1080 void RequireBinding ()
1083 RequiresDataBinding = true;
1088 protected virtual Table CreateChildTable ()
1090 Table table = new Table ();
1091 table.Caption = Caption;
1092 table.CaptionAlign = CaptionAlign;
1093 table.CellPadding = CellPadding;
1094 table.CellSpacing = CellSpacing;
1095 table.HorizontalAlign = HorizontalAlign;
1096 table.BackImageUrl = BackImageUrl;
1100 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1102 PagedDataSource dataSource;
1105 DataSourceView view = GetData ();
1106 dataSource = new PagedDataSource ();
1107 dataSource.DataSource = data;
1110 dataSource.AllowPaging = true;
1111 dataSource.PageSize = PageSize;
1112 dataSource.CurrentPageIndex = PageIndex;
1114 dataSource.AllowServerPaging = true;
1115 if (view.CanRetrieveTotalRowCount)
1116 dataSource.VirtualCount = SelectArguments.TotalRowCount;
1118 dataSource.DataSourceView = view;
1119 dataSource.DataSourceSelectArguments = SelectArguments;
1120 dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
1125 pageCount = dataSource.PageCount;
1129 dataSource = new PagedDataSource ();
1130 dataSource.DataSource = data;
1132 dataSource.AllowPaging = true;
1133 dataSource.PageSize = PageSize;
1134 dataSource.CurrentPageIndex = PageIndex;
1138 bool showPager = AllowPaging && (PageCount > 1);
1141 table = CreateChildTable ();
1142 Controls.Add (table);
1144 ArrayList list = new ArrayList ();
1145 ArrayList keyList = new ArrayList ();
1147 // Creates the set of fields to show
1149 ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1150 DataControlField[] fields = new DataControlField [fieldCollection.Count];
1151 fieldCollection.CopyTo (fields, 0);
1153 foreach (DataControlField field in fields) {
1154 field.Initialize (AllowSorting, this);
1155 if (EnableSortingAndPagingCallbacks)
1156 field.ValidateSupportsCallback ();
1159 // Main table creation
1161 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
1162 topPagerRow = CreatePagerRow (fields.Length, dataSource);
1163 table.Rows.Add (topPagerRow);
1166 GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
1167 table.Rows.Add (headerRow);
1168 InitializeRow (headerRow, fields);
1170 foreach (object obj in dataSource) {
1171 DataControlRowState rstate = GetRowState (list.Count);
1172 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1175 table.Rows.Add (row);
1176 InitializeRow (row, fields);
1179 OnRowDataBound (new GridViewRowEventArgs (row));
1180 if (EditIndex == row.RowIndex)
1181 oldEditValues = new DataKey (GetRowValues (row, false, true));
1182 keyList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
1184 if (EditIndex == row.RowIndex)
1185 oldEditValues = new DataKey (new OrderedDictionary ());
1186 keyList.Add (new DataKey (new OrderedDictionary (), DataKeyNames));
1189 if (list.Count >= PageSize)
1193 if (list.Count == 0)
1194 table.Rows.Add (CreateEmptyrRow (fields.Length));
1196 GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
1197 table.Rows.Add (footerRow);
1198 InitializeRow (footerRow, fields);
1200 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
1201 bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
1202 table.Rows.Add (bottomPagerRow);
1205 rows = new GridViewRowCollection (list);
1206 keys = new DataKeyArray (keyList);
1208 return dataSource.DataSourceCount;
1212 protected override Style CreateControlStyle ()
1214 return base.CreateControlStyle ();
1217 DataControlRowState GetRowState (int index)
1219 DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1220 if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
1221 if (index == EditIndex) rstate |= DataControlRowState.Edit;
1225 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1227 GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1228 InitializePager (row, fieldCount, dataSource);
1232 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1234 TableCell cell = new TableCell ();
1235 cell.ColumnSpan = columnSpan;
1237 if (pagerTemplate != null)
1238 pagerTemplate.InstantiateIn (cell);
1240 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
1242 row.Cells.Add (cell);
1245 GridViewRow CreateEmptyrRow (int fieldCount)
1247 GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1248 TableCell cell = new TableCell ();
1249 cell.ColumnSpan = fieldCount;
1251 if (emptyDataTemplate != null)
1252 emptyDataTemplate.InstantiateIn (cell);
1254 cell.Text = EmptyDataText;
1256 row.Cells.Add (cell);
1260 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1262 DataControlCellType ctype;
1263 bool accessibleHeader = false;
1265 switch (row.RowType) {
1266 case DataControlRowType.Header:
1267 ctype = DataControlCellType.Header;
1268 accessibleHeader = UseAccessibleHeader;
1270 case DataControlRowType.Footer:
1271 ctype = DataControlCellType.Footer;
1274 ctype = DataControlCellType.DataCell;
1278 for (int n=0; n<fields.Length; n++) {
1279 DataControlField field = fields [n];
1280 DataControlFieldCell cell;
1281 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1282 cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1284 cell = new DataControlFieldCell (field);
1285 row.Cells.Add (cell);
1286 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1290 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1292 if (cachedKeyProperties == null) {
1293 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
1294 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1295 for (int n=0; n<DataKeyNames.Length; n++) {
1296 PropertyDescriptor p = props [DataKeyNames[n]];
1298 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + row.DataItem.GetType());
1299 cachedKeyProperties [n] = p;
1303 OrderedDictionary dic = new OrderedDictionary ();
1304 foreach (PropertyDescriptor p in cachedKeyProperties)
1305 dic [p.Name] = p.GetValue (row.DataItem);
1309 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1311 OrderedDictionary dic = new OrderedDictionary ();
1312 ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1316 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1318 foreach (TableCell cell in row.Cells) {
1319 DataControlFieldCell c = cell as DataControlFieldCell;
1321 c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1323 if (!includePrimaryKey && DataKeyNames != null)
1324 foreach (string key in DataKeyNames)
1325 fieldValues.Remove (key);
1328 protected override HtmlTextWriterTag TagKey {
1330 if (EnableSortingAndPagingCallbacks)
1331 return HtmlTextWriterTag.Div;
1333 return HtmlTextWriterTag.Table;
1337 public sealed override void DataBind ()
1339 DataSourceView view = GetData ();
1340 if (AllowPaging && view.CanPage) {
1341 SelectArguments.StartRowIndex = PageIndex * PageSize;
1342 SelectArguments.MaximumRows = PageSize;
1343 if (view.CanRetrieveTotalRowCount)
1344 SelectArguments.RetrieveTotalRowCount = true;
1347 if (sortExpression != "") {
1348 if (sortDirection == SortDirection.Ascending)
1349 SelectArguments.SortExpression = sortExpression;
1351 SelectArguments.SortExpression = sortExpression + " DESC";
1354 cachedKeyProperties = null;
1358 protected internal override void PerformDataBinding (IEnumerable data)
1360 base.PerformDataBinding (data);
1364 protected internal virtual void PrepareControlHierarchy ()
1366 throw new NotImplementedException ();
1369 protected internal override void OnInit (EventArgs e)
1371 Page.RegisterRequiresControlState (this);
1375 void OnFieldsChanged (object sender, EventArgs args)
1380 protected override void OnDataPropertyChanged ()
1382 base.OnDataPropertyChanged ();
1386 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1388 base.OnDataSourceViewChanged (sender, e);
1392 protected override bool OnBubbleEvent (object source, EventArgs e)
1394 GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1396 OnRowCommand (args);
1397 string param = args.CommandArgument as string;
1398 if (param == null || param.Length == 0) {
1399 GridViewRow row = args.Row;
1401 param = row.RowIndex.ToString();
1403 ProcessEvent (args.CommandName, param);
1405 return base.OnBubbleEvent (source, e);
1408 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
1410 RaisePostBackEvent (eventArgument);
1413 // This is prolly obsolete
1414 protected virtual void RaisePostBackEvent (string eventArgument)
1416 int i = eventArgument.IndexOf ('$');
1418 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1420 ProcessEvent (eventArgument, null);
1423 void ProcessEvent (string eventName, string param)
1427 case DataControlCommands.PageCommandName:
1430 case DataControlCommands.FirstPageCommandArgument:
1433 case DataControlCommands.LastPageCommandArgument:
1434 newIndex = PageCount - 1;
1436 case DataControlCommands.NextPageCommandArgument:
1437 if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1439 case DataControlCommands.PreviousPageCommandArgument:
1440 if (PageIndex > 0) newIndex = PageIndex - 1;
1443 newIndex = int.Parse (param) - 1;
1446 ShowPage (newIndex);
1449 case DataControlCommands.FirstPageCommandArgument:
1453 case DataControlCommands.LastPageCommandArgument:
1454 ShowPage (PageCount - 1);
1457 case DataControlCommands.NextPageCommandArgument:
1458 if (PageIndex < PageCount - 1)
1459 ShowPage (PageIndex + 1);
1462 case DataControlCommands.PreviousPageCommandArgument:
1464 ShowPage (PageIndex - 1);
1467 case DataControlCommands.SelectCommandName:
1468 SelectRow (int.Parse (param));
1471 case DataControlCommands.EditCommandName:
1472 EditRow (int.Parse (param));
1475 case DataControlCommands.UpdateCommandName:
1476 UpdateRow (EditIndex, true);
1479 case DataControlCommands.CancelCommandName:
1483 case DataControlCommands.DeleteCommandName:
1484 DeleteRow (int.Parse (param));
1487 case DataControlCommands.SortCommandName:
1493 void Sort (string newSortExpression)
1495 SortDirection newDirection;
1496 if (sortExpression == newSortExpression) {
1497 if (sortDirection == SortDirection.Ascending)
1498 newDirection = SortDirection.Descending;
1500 newDirection = SortDirection.Ascending;
1502 newDirection = sortDirection;
1504 Sort (newSortExpression, newDirection);
1507 public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
1509 GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
1511 if (args.Cancel) return;
1513 sortExpression = args.SortExpression;
1514 sortDirection = args.SortDirection;
1517 OnSorted (EventArgs.Empty);
1520 void SelectRow (int index)
1522 GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
1523 OnSelectedIndexChanging (args);
1525 SelectedIndex = args.NewSelectedIndex;
1526 OnSelectedIndexChanged (EventArgs.Empty);
1530 void ShowPage (int newIndex)
1532 GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
1533 OnPageIndexChanging (args);
1536 PageIndex = args.NewPageIndex;
1537 OnPageIndexChanged (EventArgs.Empty);
1541 void EditRow (int index)
1543 GridViewEditEventArgs args = new GridViewEditEventArgs (index);
1544 OnRowEditing (args);
1546 EditIndex = args.NewEditIndex;
1552 GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
1553 OnRowCancelingEdit (args);
1559 [MonoTODO ("Support two-way binding expressions")]
1560 public virtual void UpdateRow (int rowIndex, bool causesValidation)
1562 if (causesValidation)
1565 if (rowIndex != EditIndex) throw new NotSupportedException ();
1567 currentEditOldValues = oldEditValues.Values;
1569 GridViewRow row = Rows [rowIndex];
1570 currentEditRowKeys = DataKeys [rowIndex].Values;
1571 currentEditNewValues = GetRowValues (row, false, false);
1573 GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (EditIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1574 OnRowUpdating (args);
1576 DataSourceView view = GetData ();
1577 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1578 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1583 bool UpdateCallback (int recordsAffected, Exception exception)
1585 GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1586 OnRowUpdated (dargs);
1588 if (!dargs.KeepInEditMode)
1591 return dargs.ExceptionHandled;
1594 public virtual void DeleteRow (int rowIndex)
1596 GridViewRow row = Rows [rowIndex];
1597 currentEditRowKeys = DataKeys [rowIndex].Values;
1598 currentEditNewValues = GetRowValues (row, true, true);
1600 GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
1601 OnRowDeleting (args);
1605 DataSourceView view = GetData ();
1607 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1609 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1610 OnRowDeleted (dargs);
1615 bool DeleteCallback (int recordsAffected, Exception exception)
1617 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1618 OnRowDeleted (dargs);
1619 return dargs.ExceptionHandled;
1625 oldEditValues = new DataKey (new OrderedDictionary ());
1626 currentEditRowKeys = null;
1627 currentEditOldValues = null;
1628 currentEditNewValues = null;
1631 protected internal override void LoadControlState (object ob)
1633 if (ob == null) return;
1634 object[] state = (object[]) ob;
1635 base.LoadControlState (state[0]);
1636 pageIndex = (int) state[1];
1637 pageCount = (int) state[2];
1638 selectedIndex = (int) state[3];
1639 editIndex = (int) state[4];
1640 sortExpression = (string) state[5];
1641 sortDirection = (SortDirection) state[6];
1644 protected internal override object SaveControlState ()
1646 object bstate = base.SaveControlState ();
1647 return new object[] {
1648 bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection
1652 protected override void TrackViewState()
1654 base.TrackViewState();
1655 if (columns != null) ((IStateManager)columns).TrackViewState();
1656 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1657 if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
1658 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1659 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1660 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1661 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1662 if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
1663 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1664 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1665 if (keys != null) ((IStateManager)keys).TrackViewState();
1666 if (autoFieldProperties != null) {
1667 foreach (IStateManager sm in autoFieldProperties)
1668 sm.TrackViewState ();
1672 protected override object SaveViewState()
1674 object[] states = new object [14];
1675 states[0] = base.SaveViewState();
1676 states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
1677 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1678 states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
1679 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1680 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1681 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1682 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1683 states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
1684 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1685 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1686 states[11] = (keys == null ? null : ((IStateManager)keys).SaveViewState());
1687 states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1689 if (autoFieldProperties != null) {
1690 object[] data = new object [autoFieldProperties.Length];
1691 bool allNull = true;
1692 for (int n=0; n<data.Length; n++) {
1693 data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
1694 if (data [n] != null) allNull = false;
1696 if (!allNull) states [13] = data;
1699 for (int i = states.Length - 1; i >= 0; i--) {
1700 if (states [i] != null)
1707 protected override void LoadViewState (object savedState)
1709 if (savedState == null) {
1710 base.LoadViewState (null);
1714 object [] states = (object []) savedState;
1716 if (states[13] != null) {
1717 object[] data = (object[]) states [13];
1718 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
1719 for (int n=0; n<data.Length; n++) {
1720 IStateManager p = new AutoGeneratedFieldProperties ();
1721 p.TrackViewState ();
1722 p.LoadViewState (data [n]);
1723 autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
1727 base.LoadViewState (states[0]);
1728 EnsureChildControls ();
1730 if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
1731 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1732 if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
1733 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1734 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1735 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1736 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1737 if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
1738 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1739 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1740 if (states[11] != null) ((IStateManager)DataKeys).LoadViewState (states[11]);
1741 if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1744 void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
1746 RaiseCallbackEvent (eventArgs);
1749 string callbackResult;
1750 protected virtual void RaiseCallbackEvent (string eventArgs)
1752 string[] clientData = eventArgs.Split ('|');
1753 pageIndex = int.Parse (clientData[0]);
1754 sortExpression = HttpUtility.UrlDecode (clientData[1]);
1755 if (sortExpression == "") sortExpression = null;
1758 RaisePostBackEvent (clientData[2]);
1761 StringWriter sw = new StringWriter ();
1762 sw.Write (PageIndex.ToString() + '|' + SortExpression + '|');
1764 HtmlTextWriter writer = new HtmlTextWriter (sw);
1765 RenderGrid (writer);
1766 callbackResult = sw.ToString ();
1769 string ICallbackEventHandler.GetCallbackResult ()
1771 return GetCallbackResult ();
1774 protected virtual string GetCallbackResult ()
1776 return callbackResult;
1779 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
1781 return GetCallbackScript (control, argument);
1784 protected virtual string GetCallbackScript (IButtonControl control, string argument)
1786 if (EnableSortingAndPagingCallbacks)
1787 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
1793 protected override void OnPagePreLoad (object sender, EventArgs e)
1795 base.OnPagePreLoad (sender, e);
1798 protected internal override void OnPreRender (EventArgs e)
1800 base.OnPreRender (e);
1802 if (EnableSortingAndPagingCallbacks)
1804 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
1805 string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
1806 Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
1809 string cgrid = ClientID + "_data";
1810 string script = string.Format ("var {0} = new Object ();\n", cgrid);
1811 script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
1812 script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
1813 script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
1814 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
1816 // Make sure the basic script infrastructure is rendered
1817 Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
1818 Page.ClientScript.GetPostBackClientHyperlink (this, "");
1822 protected internal override void Render (HtmlTextWriter writer)
1824 if (EnableSortingAndPagingCallbacks)
1825 base.RenderBeginTag (writer);
1827 RenderGrid (writer);
1829 if (EnableSortingAndPagingCallbacks)
1830 base.RenderEndTag (writer);
1833 void RenderGrid (HtmlTextWriter writer)
1835 switch (GridLines) {
1836 case GridLines.Horizontal:
1837 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
1838 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1840 case GridLines.Vertical:
1841 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
1842 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1844 case GridLines.Both:
1845 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
1846 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1849 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
1853 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
1854 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
1855 table.RenderBeginTag (writer);
1857 foreach (GridViewRow row in table.Rows)
1859 switch (row.RowType) {
1860 case DataControlRowType.Header:
1861 if (!ShowHeader) continue;
1862 if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1864 case DataControlRowType.Footer:
1865 if (!ShowFooter) continue;
1866 if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1868 case DataControlRowType.Pager:
1869 if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1871 case DataControlRowType.EmptyDataRow:
1872 if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1878 if ((row.RowState & DataControlRowState.Normal) != 0 && rowStyle != null)
1879 rowStyle.AddAttributesToRender (writer, row);
1880 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null)
1881 alternatingRowStyle.AddAttributesToRender (writer, row);
1882 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1883 editRowStyle.AddAttributesToRender (writer, row);
1884 if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null)
1885 selectedRowStyle.AddAttributesToRender (writer, row);
1887 row.RenderBeginTag (writer);
1889 foreach (TableCell cell in row.Cells) {
1890 DataControlFieldCell fcell = cell as DataControlFieldCell;
1891 if (fcell != null) {
1892 Style cellStyle = null;
1893 switch (row.RowType) {
1894 case DataControlRowType.Header: cellStyle = fcell.ContainingField.HeaderStyle; break;
1895 case DataControlRowType.Footer: cellStyle = fcell.ContainingField.FooterStyle; break;
1896 default: cellStyle = fcell.ContainingField.ItemStyle; break;
1898 if (cellStyle != null)
1899 cellStyle.AddAttributesToRender (writer, cell);
1901 cell.Render (writer);
1903 row.RenderEndTag (writer);
1905 table.RenderEndTag (writer);
1909 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
1911 throw new NotImplementedException ();