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 [DesignerAttribute ("System.Web.UI.Design.WebControls.GridViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
44 [ControlValuePropertyAttribute ("SelectedValue")]
45 [DefaultEventAttribute ("SelectedIndexChanged")]
46 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48 public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer
51 GridViewRowCollection rows;
52 GridViewRow headerRow;
53 GridViewRow footerRow;
54 GridViewRow bottomPagerRow;
55 GridViewRow topPagerRow;
57 IOrderedDictionary currentEditRowKeys;
58 IOrderedDictionary currentEditNewValues;
59 IOrderedDictionary currentEditOldValues;
61 ITemplate pagerTemplate;
62 ITemplate emptyDataTemplate;
64 PropertyDescriptor[] cachedKeyProperties;
67 DataControlFieldCollection columns;
68 PagerSettings pagerSettings;
70 TableItemStyle alternatingRowStyle;
71 TableItemStyle editRowStyle;
72 TableItemStyle emptyDataRowStyle;
73 TableItemStyle footerStyle;
74 TableItemStyle headerStyle;
75 TableItemStyle pagerStyle;
76 TableItemStyle rowStyle;
77 TableItemStyle selectedRowStyle;
79 DataKey oldEditValues;
80 AutoGeneratedFieldProperties[] autoFieldProperties;
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;
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 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 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 virtual 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 object ob = ViewState ["BackImageUrl"];
413 if (ob != null) return (string) ob;
417 ViewState ["BackImageUrl"] = value;
422 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
423 [BrowsableAttribute (false)]
424 public virtual GridViewRow BottomPagerRow {
427 return bottomPagerRow;
431 [WebCategoryAttribute ("Accessibility")]
432 [DefaultValueAttribute ("")]
433 [LocalizableAttribute (true)]
434 public string Caption {
436 object ob = ViewState ["Caption"];
437 if (ob != null) return (string) ob;
441 ViewState ["Caption"] = value;
446 [WebCategoryAttribute ("Accessibility")]
447 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
448 public virtual TableCaptionAlign CaptionAlign
451 object o = ViewState ["CaptionAlign"];
452 if(o != null) return (TableCaptionAlign) o;
453 return TableCaptionAlign.NotSet;
456 ViewState ["CaptionAlign"] = value;
461 [WebCategoryAttribute ("Layout")]
462 [DefaultValueAttribute (-1)]
463 public virtual int CellPadding
466 object o = ViewState ["CellPadding"];
467 if (o != null) return (int) o;
471 ViewState ["CellPadding"] = value;
476 [WebCategoryAttribute ("Layout")]
477 [DefaultValueAttribute (0)]
478 public virtual int CellSpacing
481 object o = ViewState ["CellSpacing"];
482 if (o != null) return (int) o;
486 ViewState ["CellSpacing"] = value;
491 [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
492 [MergablePropertyAttribute (false)]
493 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
494 [DefaultValueAttribute (null)]
495 [WebCategoryAttribute ("Misc")]
496 public virtual DataControlFieldCollection Columns {
498 if (columns == null) {
499 columns = new DataControlFieldCollection ();
500 columns.FieldsChanged += new EventHandler (OnFieldsChanged);
501 if (IsTrackingViewState)
502 ((IStateManager)columns).TrackViewState ();
508 [DefaultValueAttribute (null)]
509 [WebCategoryAttribute ("Data")]
510 [TypeConverter (typeof(StringArrayConverter))]
511 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
512 public virtual string[] DataKeyNames
515 object o = ViewState ["DataKeyNames"];
516 if (o != null) return (string[]) o;
520 ViewState ["DataKeyNames"] = value;
525 [BrowsableAttribute (false)]
526 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
527 public virtual DataKeyArray DataKeys {
534 [WebCategoryAttribute ("Misc")]
535 [DefaultValueAttribute (-1)]
536 public int EditIndex {
546 [WebCategoryAttribute ("Styles")]
547 [PersistenceMode (PersistenceMode.InnerProperty)]
548 [NotifyParentProperty (true)]
549 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
550 public virtual TableItemStyle EditRowStyle {
552 if (editRowStyle == null) {
553 editRowStyle = new TableItemStyle ();
554 if (IsTrackingViewState)
555 editRowStyle.TrackViewState();
561 [WebCategoryAttribute ("Styles")]
562 [PersistenceMode (PersistenceMode.InnerProperty)]
563 [NotifyParentProperty (true)]
564 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
565 public virtual TableItemStyle EmptyDataRowStyle {
567 if (emptyDataRowStyle == null) {
568 emptyDataRowStyle = new TableItemStyle ();
569 if (IsTrackingViewState)
570 emptyDataRowStyle.TrackViewState();
572 return emptyDataRowStyle;
576 [DefaultValue (null)]
577 [TemplateContainer (typeof(GridView), BindingDirection.OneWay)]
578 [PersistenceMode (PersistenceMode.InnerProperty)]
580 public ITemplate EmptyDataTemplate {
581 get { return emptyDataTemplate; }
582 set { emptyDataTemplate = value; RequireBinding (); }
585 [LocalizableAttribute (true)]
586 [WebCategoryAttribute ("Appearance")]
587 [DefaultValueAttribute ("")]
588 public virtual string EmptyDataText {
590 object ob = ViewState ["EmptyDataText"];
591 if (ob != null) return (string) ob;
595 ViewState ["EmptyDataText"] = value;
600 [WebCategoryAttribute ("Behavior")]
601 [DefaultValueAttribute (false)]
602 public virtual bool EnableSortingAndPagingCallbacks {
604 object ob = ViewState ["EnableSortingAndPagingCallbacks"];
605 if (ob != null) return (bool) ob;
609 ViewState ["EnableSortingAndPagingCallbacks"] = value;
614 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
615 [BrowsableAttribute (false)]
616 public virtual GridViewRow FooterRow {
618 if (footerRow == null)
619 footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
624 [WebCategoryAttribute ("Styles")]
625 [PersistenceMode (PersistenceMode.InnerProperty)]
626 [NotifyParentProperty (true)]
627 [DefaultValue (null)]
628 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
629 public virtual TableItemStyle FooterStyle {
631 if (footerStyle == null) {
632 footerStyle = new TableItemStyle ();
633 if (IsTrackingViewState)
634 footerStyle.TrackViewState();
640 [WebCategoryAttribute ("Appearance")]
641 [DefaultValueAttribute (GridLines.Both)]
642 public virtual GridLines GridLines {
644 object ob = ViewState ["GridLines"];
645 if (ob != null) return (GridLines) ob;
646 return GridLines.Both;
649 ViewState ["GridLines"] = value;
653 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
654 [BrowsableAttribute (false)]
655 public virtual GridViewRow HeaderRow {
657 if (headerRow == null)
658 headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
663 [WebCategoryAttribute ("Styles")]
664 [PersistenceMode (PersistenceMode.InnerProperty)]
665 [NotifyParentProperty (true)]
666 [DefaultValue (null)]
667 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
668 public virtual TableItemStyle HeaderStyle {
670 if (headerStyle == null) {
671 headerStyle = new TableItemStyle ();
672 if (IsTrackingViewState)
673 headerStyle.TrackViewState();
679 [Category ("Layout")]
680 [DefaultValueAttribute (HorizontalAlign.NotSet)]
681 public virtual HorizontalAlign HorizontalAlign {
683 object ob = ViewState ["HorizontalAlign"];
684 if (ob != null) return (HorizontalAlign) ob;
685 return HorizontalAlign.NotSet;
688 ViewState ["HorizontalAlign"] = value;
693 [BrowsableAttribute (false)]
694 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
695 public int PageCount {
697 if (pageCount != -1) return pageCount;
703 [WebCategoryAttribute ("Paging")]
704 [BrowsableAttribute (true)]
705 [DefaultValueAttribute (0)]
706 public int PageIndex {
716 [DefaultValueAttribute (10)]
717 [WebCategoryAttribute ("Paging")]
718 public int PageSize {
720 object ob = ViewState ["PageSize"];
721 if (ob != null) return (int) ob;
725 ViewState ["PageSize"] = value;
730 [WebCategoryAttribute ("Paging")]
731 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
732 [NotifyParentPropertyAttribute (true)]
733 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
734 public PagerSettings PagerSettings {
736 if (pagerSettings == null) {
737 pagerSettings = new PagerSettings (this);
738 if (IsTrackingViewState)
739 ((IStateManager)pagerSettings).TrackViewState ();
741 return pagerSettings;
745 [WebCategoryAttribute ("Styles")]
746 [PersistenceMode (PersistenceMode.InnerProperty)]
747 [NotifyParentProperty (true)]
748 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
749 public virtual TableItemStyle PagerStyle {
751 if (pagerStyle == null) {
752 pagerStyle = new TableItemStyle ();
753 if (IsTrackingViewState)
754 pagerStyle.TrackViewState();
761 [DefaultValue (null)]
762 /* DataControlPagerCell isnt specified in the docs */
763 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
764 [PersistenceMode (PersistenceMode.InnerProperty)]
766 public ITemplate PagerTemplate {
767 get { return pagerTemplate; }
768 set { pagerTemplate = value; RequireBinding (); }
771 [DefaultValueAttribute ("")]
772 [WebCategoryAttribute ("Accessibility")]
773 // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
774 public virtual string RowHeaderColumn {
776 object ob = ViewState ["RowHeaderColumn"];
777 if (ob != null) return (string) ob;
781 ViewState ["RowHeaderColumn"] = value;
786 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
787 [BrowsableAttribute (false)]
788 public virtual GridViewRowCollection Rows {
795 [WebCategoryAttribute ("Styles")]
796 [PersistenceMode (PersistenceMode.InnerProperty)]
797 [NotifyParentProperty (true)]
798 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
799 public virtual TableItemStyle RowStyle {
801 if (rowStyle == null) {
802 rowStyle = new TableItemStyle ();
803 if (IsTrackingViewState)
804 rowStyle.TrackViewState();
810 [BrowsableAttribute (false)]
811 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
812 public virtual DataKey SelectedDataKey {
814 if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
815 return DataKeys [selectedIndex];
821 [BindableAttribute (true)]
822 [DefaultValueAttribute (-1)]
823 public int SelectedIndex {
825 return selectedIndex;
828 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
829 int oldIndex = selectedIndex;
831 Rows [oldIndex].RowState = GetRowState (oldIndex);
833 selectedIndex = value;
834 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
835 Rows [selectedIndex].RowState = GetRowState (selectedIndex);
840 [BrowsableAttribute (false)]
841 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
842 public virtual GridViewRow SelectedRow {
844 if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
845 return Rows [selectedIndex];
851 [WebCategoryAttribute ("Styles")]
852 [PersistenceMode (PersistenceMode.InnerProperty)]
853 [NotifyParentProperty (true)]
854 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
855 public virtual TableItemStyle SelectedRowStyle {
857 if (selectedRowStyle == null) {
858 selectedRowStyle = new TableItemStyle ();
859 if (IsTrackingViewState)
860 selectedRowStyle.TrackViewState();
862 return selectedRowStyle;
866 [BrowsableAttribute (false)]
867 public virtual object SelectedValue {
869 if (SelectedDataKey != null)
870 return SelectedDataKey.Value;
876 [WebCategoryAttribute ("Appearance")]
877 [DefaultValueAttribute (false)]
878 public virtual bool ShowFooter {
880 object ob = ViewState ["ShowFooter"];
881 if (ob != null) return (bool) ob;
885 ViewState ["ShowFooter"] = value;
890 [WebCategoryAttribute ("Appearance")]
891 [DefaultValueAttribute (true)]
892 public virtual bool ShowHeader {
894 object ob = ViewState ["ShowHeader"];
895 if (ob != null) return (bool) ob;
899 ViewState ["ShowHeader"] = value;
904 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
905 [BrowsableAttribute (false)]
906 [DefaultValueAttribute (SortDirection.Ascending)]
907 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
908 public virtual SortDirection SortDirection {
909 get { return sortDirection; }
912 [BrowsableAttribute (false)]
913 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
914 public virtual string SortExpression {
915 get { return sortExpression; }
918 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
919 [BrowsableAttribute (false)]
920 public virtual GridViewRow TopPagerRow {
927 [WebCategoryAttribute ("Accessibility")]
928 [DefaultValueAttribute (true)]
929 public virtual bool UseAccessibleHeader {
931 object ob = ViewState ["UseAccessibleHeader"];
932 if (ob != null) return (bool) ob;
936 ViewState ["UseAccessibleHeader"] = value;
941 public virtual bool IsBindableType (Type type)
943 return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
946 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
948 return base.CreateDataSourceSelectArguments ();
951 protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
953 ArrayList fields = new ArrayList ();
955 if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
956 CommandField field = new CommandField ();
957 field.ShowEditButton = AutoGenerateEditButton;
958 field.ShowDeleteButton = AutoGenerateDeleteButton;
959 field.ShowSelectButton = AutoGenerateSelectButton;
963 if (AutoGenerateColumns) {
965 autoFieldProperties = CreateAutoFieldProperties (dataSource);
967 if (autoFieldProperties != null) {
968 foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
969 fields.Add (CreateAutoGeneratedColumn (props));
973 fields.AddRange (Columns);
978 protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
980 return new AutoGeneratedField (fieldProperties);
983 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
985 if(source == null) return null;
987 PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
990 ArrayList retVal = new ArrayList();
996 PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
997 BindingFlags.Instance | BindingFlags.Static |
998 BindingFlags.Public, null, null,
999 new Type[] { typeof(int) }, null);
1001 if (prop_item != null) {
1002 prop_type = prop_item.PropertyType;
1005 if (prop_type == null || prop_type == typeof(object)) {
1006 IEnumerator en = source.GetEnumerator();
1010 prop_type = fitem.GetType();
1013 if (fitem != null && fitem is ICustomTypeDescriptor) {
1014 props = TypeDescriptor.GetProperties(fitem);
1015 } else if (prop_type != null) {
1016 if (IsBindableType (prop_type)) {
1017 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1018 ((IStateManager)field).TrackViewState();
1019 field.Name = "Item";
1020 field.DataField = BoundField.ThisExpression;
1021 field.Type = prop_type;
1024 props = TypeDescriptor.GetProperties (prop_type);
1029 if (props != null && props.Count > 0)
1031 foreach (PropertyDescriptor current in props) {
1032 if (IsBindableType (current.PropertyType)) {
1033 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
1034 ((IStateManager)field).TrackViewState();
1035 field.Name = current.Name;
1036 field.DataField = current.Name;
1037 field.IsReadOnly = current.IsReadOnly;
1038 field.Type = current.PropertyType;
1044 if (retVal.Count > 0)
1045 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
1047 return new AutoGeneratedFieldProperties [0];
1050 protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
1052 GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
1053 OnRowCreated (new GridViewRowEventArgs (row));
1057 void RequireBinding ()
1060 RequiresDataBinding = true;
1065 protected virtual Table CreateChildTable ()
1067 Table table = new Table ();
1068 table.Caption = Caption;
1069 table.CaptionAlign = CaptionAlign;
1070 table.CellPadding = CellPadding;
1071 table.CellSpacing = CellSpacing;
1072 table.HorizontalAlign = HorizontalAlign;
1073 table.BackImageUrl = BackImageUrl;
1077 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
1079 PagedDataSource dataSource;
1082 DataSourceView view = GetData ();
1083 dataSource = new PagedDataSource ();
1084 dataSource.DataSource = data;
1087 dataSource.AllowPaging = true;
1088 dataSource.PageSize = PageSize;
1089 dataSource.CurrentPageIndex = PageIndex;
1091 dataSource.AllowServerPaging = true;
1092 if (view.CanRetrieveTotalRowCount)
1093 dataSource.VirtualCount = SelectArguments.TotalRowCount;
1095 dataSource.DataSourceView = view;
1096 dataSource.DataSourceSelectArguments = SelectArguments;
1097 dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
1102 pageCount = dataSource.PageCount;
1106 dataSource = new PagedDataSource ();
1107 dataSource.DataSource = data;
1109 dataSource.AllowPaging = true;
1110 dataSource.PageSize = PageSize;
1111 dataSource.CurrentPageIndex = PageIndex;
1115 bool showPager = AllowPaging && (PageCount > 1);
1118 table = CreateChildTable ();
1119 Controls.Add (table);
1121 ArrayList list = new ArrayList ();
1122 ArrayList keyList = new ArrayList ();
1124 // Creates the set of fields to show
1126 ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
1127 DataControlField[] fields = new DataControlField [fieldCollection.Count];
1128 fieldCollection.CopyTo (fields, 0);
1130 foreach (DataControlField field in fields) {
1131 field.Initialize (AllowSorting, this);
1132 if (EnableSortingAndPagingCallbacks)
1133 field.ValidateSupportsCallback ();
1136 // Main table creation
1138 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
1139 topPagerRow = CreatePagerRow (fields.Length, dataSource);
1140 table.Rows.Add (topPagerRow);
1144 headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
1145 table.Rows.Add (headerRow);
1146 InitializeRow (headerRow, fields);
1149 foreach (object obj in dataSource) {
1150 DataControlRowState rstate = GetRowState (list.Count);
1151 GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
1154 table.Rows.Add (row);
1155 InitializeRow (row, fields);
1158 OnRowDataBound (new GridViewRowEventArgs (row));
1159 if (EditIndex == row.RowIndex)
1160 oldEditValues = new DataKey (GetRowValues (row, false, true));
1161 keyList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
1163 if (EditIndex == row.RowIndex)
1164 oldEditValues = new DataKey (new OrderedDictionary ());
1165 keyList.Add (new DataKey (new OrderedDictionary (), DataKeyNames));
1168 if (list.Count >= PageSize)
1172 if (list.Count == 0)
1173 table.Rows.Add (CreateEmptyrRow (fields.Length));
1176 footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
1177 table.Rows.Add (footerRow);
1178 InitializeRow (footerRow, fields);
1181 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
1182 bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
1183 table.Rows.Add (bottomPagerRow);
1186 rows = new GridViewRowCollection (list);
1187 keys = new DataKeyArray (keyList);
1192 return dataSource.DataSourceCount;
1196 protected override Style CreateControlStyle ()
1198 return base.CreateControlStyle ();
1201 DataControlRowState GetRowState (int index)
1203 DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1204 if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
1205 if (index == EditIndex) rstate |= DataControlRowState.Edit;
1209 GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
1211 GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
1212 InitializePager (row, fieldCount, dataSource);
1216 protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
1218 TableCell cell = new TableCell ();
1219 cell.ColumnSpan = columnSpan;
1221 if (pagerTemplate != null)
1222 pagerTemplate.InstantiateIn (cell);
1224 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
1226 row.Cells.Add (cell);
1229 GridViewRow CreateEmptyrRow (int fieldCount)
1231 GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1232 TableCell cell = new TableCell ();
1233 cell.ColumnSpan = fieldCount;
1235 if (emptyDataTemplate != null)
1236 emptyDataTemplate.InstantiateIn (cell);
1238 cell.Text = EmptyDataText;
1240 row.Cells.Add (cell);
1244 protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
1246 DataControlCellType ctype;
1247 bool accessibleHeader = false;
1249 switch (row.RowType) {
1250 case DataControlRowType.Header:
1251 ctype = DataControlCellType.Header;
1252 accessibleHeader = UseAccessibleHeader;
1254 case DataControlRowType.Footer:
1255 ctype = DataControlCellType.Footer;
1258 ctype = DataControlCellType.DataCell;
1262 for (int n=0; n<fields.Length; n++) {
1263 DataControlField field = fields [n];
1264 DataControlFieldCell cell;
1265 if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
1266 cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
1268 cell = new DataControlFieldCell (field);
1269 row.Cells.Add (cell);
1270 field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
1274 IOrderedDictionary CreateRowDataKey (GridViewRow row)
1276 if (cachedKeyProperties == null) {
1277 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
1278 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1279 for (int n=0; n<DataKeyNames.Length; n++) {
1280 PropertyDescriptor p = props [DataKeyNames[n]];
1282 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + row.DataItem.GetType());
1283 cachedKeyProperties [n] = p;
1287 OrderedDictionary dic = new OrderedDictionary ();
1288 foreach (PropertyDescriptor p in cachedKeyProperties)
1289 dic [p.Name] = p.GetValue (row.DataItem);
1293 IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1295 OrderedDictionary dic = new OrderedDictionary ();
1296 ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
1300 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
1302 foreach (TableCell cell in row.Cells) {
1303 DataControlFieldCell c = cell as DataControlFieldCell;
1305 c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1307 if (!includePrimaryKey && DataKeyNames != null)
1308 foreach (string key in DataKeyNames)
1309 fieldValues.Remove (key);
1312 protected override HtmlTextWriterTag TagKey {
1314 if (EnableSortingAndPagingCallbacks)
1315 return HtmlTextWriterTag.Div;
1317 return HtmlTextWriterTag.Table;
1321 public sealed override void DataBind ()
1323 DataSourceView view = GetData ();
1324 if (AllowPaging && view.CanPage) {
1325 SelectArguments.StartRowIndex = PageIndex * PageSize;
1326 SelectArguments.MaximumRows = PageSize;
1327 if (view.CanRetrieveTotalRowCount)
1328 SelectArguments.RetrieveTotalRowCount = true;
1331 if (sortExpression != "") {
1332 if (sortDirection == SortDirection.Ascending)
1333 SelectArguments.SortExpression = sortExpression;
1335 SelectArguments.SortExpression = sortExpression + " DESC";
1338 cachedKeyProperties = null;
1342 protected internal override void PerformDataBinding (IEnumerable data)
1344 base.PerformDataBinding (data);
1348 protected internal virtual void PrepareControlHierarchy ()
1350 throw new NotImplementedException ();
1353 protected internal override void OnInit (EventArgs e)
1355 Page.RegisterRequiresControlState (this);
1359 void OnFieldsChanged (object sender, EventArgs args)
1364 protected override void OnDataPropertyChanged ()
1366 base.OnDataPropertyChanged ();
1370 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1372 base.OnDataSourceViewChanged (sender, e);
1376 protected override bool OnBubbleEvent (object source, EventArgs e)
1378 GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
1380 OnRowCommand (args);
1381 ProcessEvent (args.CommandName, args.CommandArgument as string);
1383 return base.OnBubbleEvent (source, e);
1386 // This is prolly obsolete
1387 protected virtual void RaisePostBackEvent (string eventArgument)
1389 int i = eventArgument.IndexOf ('$');
1391 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1393 ProcessEvent (eventArgument, null);
1396 void ProcessEvent (string eventName, string param)
1400 case DataControlCommands.PageCommandName:
1403 case DataControlCommands.FirstPageCommandArgument:
1406 case DataControlCommands.LastPageCommandArgument:
1407 newIndex = PageCount - 1;
1409 case DataControlCommands.NextPageCommandArgument:
1410 if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1412 case DataControlCommands.PreviousPageCommandArgument:
1413 if (PageIndex > 0) newIndex = PageIndex - 1;
1416 newIndex = int.Parse (param) - 1;
1419 ShowPage (newIndex);
1422 case DataControlCommands.FirstPageCommandArgument:
1426 case DataControlCommands.LastPageCommandArgument:
1427 ShowPage (PageCount - 1);
1430 case DataControlCommands.NextPageCommandArgument:
1431 if (PageIndex < PageCount - 1)
1432 ShowPage (PageIndex + 1);
1435 case DataControlCommands.PreviousPageCommandArgument:
1437 ShowPage (PageIndex - 1);
1440 case DataControlCommands.SelectCommandName:
1441 SelectRow (int.Parse (param));
1444 case DataControlCommands.EditCommandName:
1445 EditRow (int.Parse (param));
1448 case DataControlCommands.UpdateCommandName:
1449 UpdateRow (EditIndex, true);
1452 case DataControlCommands.CancelCommandName:
1456 case DataControlCommands.DeleteCommandName:
1457 DeleteRow (int.Parse (param));
1460 case DataControlCommands.SortCommandName:
1466 void Sort (string newSortExpression)
1468 SortDirection newDirection;
1469 if (sortExpression == newSortExpression) {
1470 if (sortDirection == SortDirection.Ascending)
1471 newDirection = SortDirection.Descending;
1473 newDirection = SortDirection.Ascending;
1475 newDirection = sortDirection;
1477 Sort (newSortExpression, newDirection);
1480 public void Sort (string newSortExpression, SortDirection newSortDirection)
1482 GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
1484 if (args.Cancel) return;
1486 sortExpression = args.SortExpression;
1487 sortDirection = args.SortDirection;
1490 OnSorted (EventArgs.Empty);
1493 void SelectRow (int index)
1495 GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
1496 OnSelectedIndexChanging (args);
1498 SelectedIndex = args.NewSelectedIndex;
1499 OnSelectedIndexChanged (EventArgs.Empty);
1503 void ShowPage (int newIndex)
1505 GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
1506 OnPageIndexChanging (args);
1509 PageIndex = args.NewPageIndex;
1510 OnPageIndexChanged (EventArgs.Empty);
1514 void EditRow (int index)
1516 GridViewEditEventArgs args = new GridViewEditEventArgs (index);
1517 OnRowEditing (args);
1519 EditIndex = args.NewEditIndex;
1525 GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
1526 OnRowCancelingEdit (args);
1532 [MonoTODO ("Support two-way binding expressions")]
1533 public virtual void UpdateRow (int rowIndex, bool causesValidation)
1535 if (causesValidation)
1538 if (rowIndex != EditIndex) throw new NotSupportedException ();
1540 currentEditOldValues = oldEditValues.Values;
1542 GridViewRow row = Rows [rowIndex];
1543 currentEditRowKeys = DataKeys [rowIndex].Values;
1544 currentEditNewValues = GetRowValues (row, false, false);
1546 GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (EditIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1547 OnRowUpdating (args);
1549 DataSourceView view = GetData ();
1550 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1551 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1556 bool UpdateCallback (int recordsAffected, Exception exception)
1558 GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1559 OnRowUpdated (dargs);
1561 if (!dargs.KeepInEditMode)
1564 return dargs.ExceptionHandled;
1567 public void DeleteRow (int rowIndex)
1569 GridViewRow row = Rows [rowIndex];
1570 currentEditRowKeys = DataKeys [rowIndex].Values;
1571 currentEditNewValues = GetRowValues (row, true, true);
1573 GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
1574 OnRowDeleting (args);
1578 DataSourceView view = GetData ();
1580 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1582 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1583 OnRowDeleted (dargs);
1588 bool DeleteCallback (int recordsAffected, Exception exception)
1590 GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1591 OnRowDeleted (dargs);
1592 return dargs.ExceptionHandled;
1598 oldEditValues = new DataKey (new OrderedDictionary ());
1599 currentEditRowKeys = null;
1600 currentEditOldValues = null;
1601 currentEditNewValues = null;
1604 protected internal override void LoadControlState (object ob)
1606 if (ob == null) return;
1607 object[] state = (object[]) ob;
1608 base.LoadControlState (state[0]);
1609 pageIndex = (int) state[1];
1610 pageCount = (int) state[2];
1611 selectedIndex = (int) state[3];
1612 editIndex = (int) state[4];
1613 sortExpression = (string) state[5];
1614 sortDirection = (SortDirection) state[6];
1617 protected internal override object SaveControlState ()
1619 object bstate = base.SaveControlState ();
1620 return new object[] {
1621 bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection
1625 protected override void TrackViewState()
1627 base.TrackViewState();
1628 if (columns != null) ((IStateManager)columns).TrackViewState();
1629 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1630 if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
1631 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1632 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1633 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1634 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1635 if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
1636 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1637 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1638 if (keys != null) ((IStateManager)keys).TrackViewState();
1639 if (autoFieldProperties != null) {
1640 foreach (IStateManager sm in autoFieldProperties)
1641 sm.TrackViewState ();
1645 protected override object SaveViewState()
1647 object[] states = new object [14];
1648 states[0] = base.SaveViewState();
1649 states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
1650 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1651 states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
1652 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1653 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1654 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1655 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1656 states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
1657 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1658 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1659 states[11] = (keys == null ? null : ((IStateManager)keys).SaveViewState());
1660 states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1662 if (autoFieldProperties != null) {
1663 object[] data = new object [autoFieldProperties.Length];
1664 bool allNull = true;
1665 for (int n=0; n<data.Length; n++) {
1666 data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
1667 if (data [n] != null) allNull = false;
1669 if (!allNull) states [13] = data;
1672 for (int i = states.Length - 1; i >= 0; i--) {
1673 if (states [i] != null)
1680 protected override void LoadViewState (object savedState)
1682 if (savedState == null) {
1683 base.LoadViewState (null);
1687 object [] states = (object []) savedState;
1689 if (states[13] != null) {
1690 object[] data = (object[]) states [13];
1691 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
1692 for (int n=0; n<data.Length; n++) {
1693 IStateManager p = new AutoGeneratedFieldProperties ();
1694 p.TrackViewState ();
1695 p.LoadViewState (data [n]);
1696 autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
1700 base.LoadViewState (states[0]);
1701 EnsureChildControls ();
1703 if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
1704 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1705 if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
1706 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1707 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1708 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1709 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1710 if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
1711 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1712 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1713 if (states[11] != null) ((IStateManager)DataKeys).LoadViewState (states[11]);
1714 if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1717 string ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
1719 return RaiseCallbackEvent (eventArgs);
1722 protected virtual string RaiseCallbackEvent (string eventArgs)
1724 string[] clientData = eventArgs.Split ('|');
1725 pageIndex = int.Parse (clientData[0]);
1726 sortExpression = HttpUtility.UrlDecode (clientData[1]);
1727 if (sortExpression == "") sortExpression = null;
1730 RaisePostBackEvent (clientData[2]);
1733 StringWriter sw = new StringWriter ();
1734 sw.Write (PageIndex.ToString() + '|' + SortExpression + '|');
1736 HtmlTextWriter writer = new HtmlTextWriter (sw);
1737 RenderGrid (writer);
1738 return sw.ToString ();
1741 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
1743 return GetCallbackScript (control, argument);
1746 protected virtual string GetCallbackScript (IButtonControl control, string argument)
1748 if (EnableSortingAndPagingCallbacks)
1749 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
1755 protected override void OnPagePreLoad (object sender, EventArgs e)
1757 base.OnPagePreLoad (sender, e);
1760 protected internal override void OnPreRender (EventArgs e)
1762 base.OnPreRender (e);
1764 if (EnableSortingAndPagingCallbacks)
1766 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
1767 string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
1768 Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
1771 string cgrid = ClientID + "_data";
1772 string script = string.Format ("var {0} = new Object ();\n", cgrid);
1773 script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
1774 script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
1775 script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
1776 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
1778 // Make sure the basic script infrastructure is rendered
1779 Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
1780 Page.ClientScript.GetPostBackClientHyperlink (this, "");
1784 protected internal override void Render (HtmlTextWriter writer)
1786 if (EnableSortingAndPagingCallbacks)
1787 base.RenderBeginTag (writer);
1789 RenderGrid (writer);
1791 if (EnableSortingAndPagingCallbacks)
1792 base.RenderEndTag (writer);
1795 void RenderGrid (HtmlTextWriter writer)
1797 switch (GridLines) {
1798 case GridLines.Horizontal:
1799 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
1800 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1802 case GridLines.Vertical:
1803 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
1804 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1806 case GridLines.Both:
1807 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
1808 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1811 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
1815 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
1816 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
1817 table.RenderBeginTag (writer);
1819 foreach (GridViewRow row in table.Rows)
1821 switch (row.RowType) {
1822 case DataControlRowType.Header:
1823 if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1825 case DataControlRowType.Footer:
1826 if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1828 case DataControlRowType.Pager:
1829 if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1831 case DataControlRowType.EmptyDataRow:
1832 if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1838 if ((row.RowState & DataControlRowState.Normal) != 0 && rowStyle != null)
1839 rowStyle.AddAttributesToRender (writer, row);
1840 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null)
1841 alternatingRowStyle.AddAttributesToRender (writer, row);
1842 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1843 editRowStyle.AddAttributesToRender (writer, row);
1844 if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null)
1845 selectedRowStyle.AddAttributesToRender (writer, row);
1847 row.RenderBeginTag (writer);
1849 foreach (TableCell cell in row.Cells) {
1850 DataControlFieldCell fcell = cell as DataControlFieldCell;
1851 if (fcell != null) {
1852 Style cellStyle = null;
1853 switch (row.RowType) {
1854 case DataControlRowType.Header: cellStyle = fcell.ContainingField.HeaderStyle; break;
1855 case DataControlRowType.Footer: cellStyle = fcell.ContainingField.FooterStyle; break;
1856 default: cellStyle = fcell.ContainingField.ItemStyle; break;
1858 if (cellStyle != null)
1859 cellStyle.AddAttributesToRender (writer, cell);
1861 cell.Render (writer);
1863 row.RenderEndTag (writer);
1865 table.RenderEndTag (writer);