2 // System.Web.UI.WebControls.DetailsView.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 [ToolboxDataAttribute ("<{0}:DetailsView runat=\"server\" Width=\"125px\" Height=\"50px\"></{0}:DetailsView>")]
\r
44 [DesignerAttribute ("System.Web.UI.Design.WebControls.DetailsViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
45 [ControlValuePropertyAttribute ("SelectedValue")]
46 [DefaultEventAttribute ("PageIndexChanging")]
47 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
49 public class DetailsView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IDataItemContainer
54 DetailsViewRowCollection rows;
55 DetailsViewRow headerRow;
56 DetailsViewRow footerRow;
57 DetailsViewRow bottomPagerRow;
58 DetailsViewRow topPagerRow;
60 IOrderedDictionary currentEditRowKeys;
61 IOrderedDictionary currentEditNewValues;
62 IOrderedDictionary currentEditOldValues;
64 ITemplate pagerTemplate;
65 ITemplate emptyDataTemplate;
66 ITemplate headerTemplate;
67 ITemplate footerTemplate;
69 PropertyDescriptor[] cachedKeyProperties;
70 readonly string[] emptyKeys = new string[0];
72 CommandField commandField;
73 DetailsViewRow commandRow;
76 DataControlFieldCollection columns;
77 PagerSettings pagerSettings;
79 TableItemStyle alternatingRowStyle;
80 TableItemStyle editRowStyle;
81 TableItemStyle insertRowStyle;
82 TableItemStyle emptyDataRowStyle;
83 TableItemStyle footerStyle;
84 TableItemStyle headerStyle;
85 TableItemStyle pagerStyle;
86 TableItemStyle rowStyle;
87 TableItemStyle commandRowStyle;
88 TableItemStyle fieldHeaderStyle;
91 DataKey oldEditValues;
92 AutoGeneratedFieldProperties[] autoFieldProperties;
94 private static readonly object PageIndexChangedEvent = new object();
95 private static readonly object PageIndexChangingEvent = new object();
96 private static readonly object ItemCommandEvent = new object();
97 private static readonly object ItemCreatedEvent = new object();
98 private static readonly object ItemDeletedEvent = new object();
99 private static readonly object ItemDeletingEvent = new object();
100 private static readonly object ItemInsertedEvent = new object();
101 private static readonly object ItemInsertingEvent = new object();
102 private static readonly object ModeChangingEvent = new object();
103 private static readonly object ModeChangedEvent = new object();
104 private static readonly object ItemUpdatedEvent = new object();
105 private static readonly object ItemUpdatingEvent = new object();
109 DetailsViewMode currentMode = DetailsViewMode.ReadOnly;
112 public DetailsView ()
116 public event EventHandler PageIndexChanged {
117 add { Events.AddHandler (PageIndexChangedEvent, value); }
118 remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
121 public event DetailsViewPageEventHandler PageIndexChanging {
122 add { Events.AddHandler (PageIndexChangingEvent, value); }
123 remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
126 public event DetailsViewCommandEventHandler ItemCommand {
127 add { Events.AddHandler (ItemCommandEvent, value); }
128 remove { Events.RemoveHandler (ItemCommandEvent, value); }
131 public event EventHandler ItemCreated {
132 add { Events.AddHandler (ItemCreatedEvent, value); }
133 remove { Events.RemoveHandler (ItemCreatedEvent, value); }
136 public event DetailsViewDeletedEventHandler ItemDeleted {
137 add { Events.AddHandler (ItemDeletedEvent, value); }
138 remove { Events.RemoveHandler (ItemDeletedEvent, value); }
141 public event DetailsViewDeleteEventHandler ItemDeleting {
142 add { Events.AddHandler (ItemDeletingEvent, value); }
143 remove { Events.RemoveHandler (ItemDeletingEvent, value); }
146 public event DetailsViewInsertedEventHandler ItemInserted {
147 add { Events.AddHandler (ItemInsertedEvent, value); }
148 remove { Events.RemoveHandler (ItemInsertedEvent, value); }
151 public event DetailsViewInsertEventHandler ItemInserting {
152 add { Events.AddHandler (ItemInsertingEvent, value); }
153 remove { Events.RemoveHandler (ItemInsertingEvent, value); }
156 public event DetailsViewModeEventHandler ModeChanging {
157 add { Events.AddHandler (ModeChangingEvent, value); }
158 remove { Events.RemoveHandler (ModeChangingEvent, value); }
161 public event EventHandler ModeChanged {
162 add { Events.AddHandler (ModeChangedEvent, value); }
163 remove { Events.RemoveHandler (ModeChangedEvent, value); }
166 public event DetailsViewUpdatedEventHandler ItemUpdated {
167 add { Events.AddHandler (ItemUpdatedEvent, value); }
168 remove { Events.RemoveHandler (ItemUpdatedEvent, value); }
171 public event DetailsViewUpdateEventHandler ItemUpdating {
172 add { Events.AddHandler (ItemUpdatingEvent, value); }
173 remove { Events.RemoveHandler (ItemUpdatingEvent, value); }
176 protected virtual void OnPageIndexChanged (EventArgs e)
178 if (Events != null) {
179 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
180 if (eh != null) eh (this, e);
184 protected virtual void OnPageIndexChanging (DetailsViewPageEventArgs e)
186 if (Events != null) {
187 DetailsViewPageEventHandler eh = (DetailsViewPageEventHandler) Events [PageIndexChangingEvent];
188 if (eh != null) eh (this, e);
192 protected virtual void OnItemCommand (DetailsViewCommandEventArgs e)
194 if (Events != null) {
195 DetailsViewCommandEventHandler eh = (DetailsViewCommandEventHandler) Events [ItemCommandEvent];
196 if (eh != null) eh (this, e);
200 protected virtual void OnItemCreated (EventArgs e)
202 if (Events != null) {
203 EventHandler eh = (EventHandler) Events [ItemCreatedEvent];
204 if (eh != null) eh (this, e);
208 protected virtual void OnItemDeleted (DetailsViewDeletedEventArgs e)
210 if (Events != null) {
211 DetailsViewDeletedEventHandler eh = (DetailsViewDeletedEventHandler) Events [ItemDeletedEvent];
212 if (eh != null) eh (this, e);
216 protected virtual void OnItemInserted (DetailsViewInsertedEventArgs e)
218 if (Events != null) {
219 DetailsViewInsertedEventHandler eh = (DetailsViewInsertedEventHandler) Events [ItemInsertedEvent];
220 if (eh != null) eh (this, e);
224 protected virtual void OnItemInserting (DetailsViewInsertEventArgs e)
226 if (Events != null) {
227 DetailsViewInsertEventHandler eh = (DetailsViewInsertEventHandler) Events [ItemInsertingEvent];
228 if (eh != null) eh (this, e);
232 protected virtual void OnItemDeleting (DetailsViewDeleteEventArgs e)
234 if (Events != null) {
235 DetailsViewDeleteEventHandler eh = (DetailsViewDeleteEventHandler) Events [ItemDeletingEvent];
236 if (eh != null) eh (this, e);
240 protected virtual void OnModeChanged (EventArgs e)
242 if (Events != null) {
243 EventHandler eh = (EventHandler) Events [ModeChangedEvent];
244 if (eh != null) eh (this, e);
248 protected virtual void OnModeChanging (DetailsViewModeEventArgs e)
250 if (Events != null) {
251 DetailsViewModeEventHandler eh = (DetailsViewModeEventHandler) Events [ModeChangingEvent];
252 if (eh != null) eh (this, e);
256 protected virtual void OnItemUpdated (DetailsViewUpdatedEventArgs e)
258 if (Events != null) {
259 DetailsViewUpdatedEventHandler eh = (DetailsViewUpdatedEventHandler) Events [ItemUpdatedEvent];
260 if (eh != null) eh (this, e);
264 protected virtual void OnItemUpdating (DetailsViewUpdateEventArgs e)
266 if (Events != null) {
267 DetailsViewUpdateEventHandler eh = (DetailsViewUpdateEventHandler) Events [ItemUpdatingEvent];
268 if (eh != null) eh (this, e);
273 [WebCategoryAttribute ("Paging")]
274 [DefaultValueAttribute (false)]
275 public bool AllowPaging {
277 object ob = ViewState ["AllowPaging"];
278 if (ob != null) return (bool) ob;
282 ViewState ["AllowPaging"] = value;
287 [DefaultValueAttribute (null)]
288 [WebCategoryAttribute ("Styles")]
289 [PersistenceMode (PersistenceMode.InnerProperty)]
290 [NotifyParentProperty (true)]
291 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
292 public virtual TableItemStyle AlternatingRowStyle {
294 if (alternatingRowStyle == null) {
295 alternatingRowStyle = new TableItemStyle ();
296 if (IsTrackingViewState)
297 alternatingRowStyle.TrackViewState();
299 return alternatingRowStyle;
303 [WebCategoryAttribute ("Behavior")]
304 [DefaultValueAttribute (false)]
305 public virtual bool AutoGenerateEditButton {
307 object ob = ViewState ["AutoGenerateEditButton"];
308 if (ob != null) return (bool) ob;
312 ViewState ["AutoGenerateEditButton"] = value;
317 [WebCategoryAttribute ("Behavior")]
318 [DefaultValueAttribute (false)]
319 public virtual bool AutoGenerateDeleteButton {
321 object ob = ViewState ["AutoGenerateDeleteButton"];
322 if (ob != null) return (bool) ob;
326 ViewState ["AutoGenerateDeleteButton"] = value;
331 [WebCategoryAttribute ("Behavior")]
332 [DefaultValueAttribute (false)]
333 public virtual bool AutoGenerateInsertButton {
335 object ob = ViewState ["AutoGenerateInsertButton"];
336 if (ob != null) return (bool) ob;
340 ViewState ["AutoGenerateInsertButton"] = value;
345 [WebCategoryAttribute ("Behavior")]
346 [DefaultValueAttribute (true)]
347 public virtual bool AutoGenerateRows {
349 object ob = ViewState ["AutoGenerateRows"];
350 if (ob != null) return (bool) ob;
354 ViewState ["AutoGenerateRows"] = value;
359 [UrlPropertyAttribute]
360 [WebCategoryAttribute ("Appearance")]
361 [DefaultValueAttribute ("")]
362 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
363 public virtual string BackImageUrl {
365 object ob = ViewState ["BackImageUrl"];
366 if (ob != null) return (string) ob;
370 ViewState ["BackImageUrl"] = value;
375 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
376 [BrowsableAttribute (false)]
377 public virtual DetailsViewRow BottomPagerRow {
380 return bottomPagerRow;
384 [WebCategoryAttribute ("Accessibility")]
385 [DefaultValueAttribute ("")]
386 [LocalizableAttribute (true)]
387 public string Caption {
389 object ob = ViewState ["Caption"];
390 if (ob != null) return (string) ob;
394 ViewState ["Caption"] = value;
399 [WebCategoryAttribute ("Accessibility")]
400 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
401 public virtual TableCaptionAlign CaptionAlign
404 object o = ViewState ["CaptionAlign"];
405 if(o != null) return (TableCaptionAlign) o;
406 return TableCaptionAlign.NotSet;
409 ViewState ["CaptionAlign"] = value;
414 [WebCategoryAttribute ("Layout")]
415 [DefaultValueAttribute (-1)]
416 public virtual int CellPadding
419 object o = ViewState ["CellPadding"];
420 if (o != null) return (int) o;
424 ViewState ["CellPadding"] = value;
429 [WebCategoryAttribute ("Layout")]
430 [DefaultValueAttribute (0)]
431 public virtual int CellSpacing
434 object o = ViewState ["CellSpacing"];
435 if (o != null) return (int) o;
439 ViewState ["CellSpacing"] = value;
444 [DefaultValueAttribute (null)]
445 [WebCategoryAttribute ("Styles")]
446 [PersistenceMode (PersistenceMode.InnerProperty)]
447 [NotifyParentProperty (true)]
448 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
449 public virtual TableItemStyle CommandRowStyle {
451 if (commandRowStyle == null) {
452 commandRowStyle = new TableItemStyle ();
453 if (IsTrackingViewState)
454 commandRowStyle.TrackViewState();
456 return commandRowStyle;
460 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
461 [BrowsableAttribute (false)]
\r
462 public DetailsViewMode CurrentMode {
468 [DefaultValueAttribute (DetailsViewMode.ReadOnly)]
\r
469 [WebCategoryAttribute ("Behavior")]
\r
470 public virtual DetailsViewMode DefaultMode {
472 object o = ViewState ["DefaultMode"];
473 if (o != null) return (DetailsViewMode) o;
474 return DetailsViewMode.ReadOnly;
477 ViewState ["DefaultMode"] = value;
482 [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
483 [MergablePropertyAttribute (false)]
484 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
485 [DefaultValueAttribute (null)]
486 [WebCategoryAttribute ("Misc")]
487 public virtual DataControlFieldCollection Fields {
489 if (columns == null) {
490 columns = new DataControlFieldCollection ();
491 columns.FieldsChanged += new EventHandler (OnFieldsChanged);
492 if (IsTrackingViewState)
493 ((IStateManager)columns).TrackViewState ();
499 [DefaultValueAttribute (null)]
500 [WebCategoryAttribute ("Data")]
501 [TypeConverter (typeof(StringArrayConverter))]
502 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
503 public virtual string[] DataKeyNames
506 object o = ViewState ["DataKeyNames"];
507 if (o != null) return (string[]) o;
511 ViewState ["DataKeyNames"] = value;
516 [BrowsableAttribute (false)]
517 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
518 public virtual DataKey DataKey {
525 [WebCategoryAttribute ("Styles")]
526 [PersistenceMode (PersistenceMode.InnerProperty)]
527 [NotifyParentProperty (true)]
528 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
529 [DefaultValueAttribute (null)]
\r
530 public virtual TableItemStyle EditRowStyle {
532 if (editRowStyle == null) {
533 editRowStyle = new TableItemStyle ();
534 if (IsTrackingViewState)
535 editRowStyle.TrackViewState();
541 [WebCategoryAttribute ("Styles")]
542 [PersistenceMode (PersistenceMode.InnerProperty)]
543 [NotifyParentProperty (true)]
544 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
545 [DefaultValueAttribute (null)]
\r
546 public virtual TableItemStyle EmptyDataRowStyle {
548 if (emptyDataRowStyle == null) {
549 emptyDataRowStyle = new TableItemStyle ();
550 if (IsTrackingViewState)
551 emptyDataRowStyle.TrackViewState();
553 return emptyDataRowStyle;
557 [DefaultValue (null)]
558 [TemplateContainer (typeof(DetailsView), BindingDirection.OneWay)]
559 [PersistenceMode (PersistenceMode.InnerProperty)]
561 public ITemplate EmptyDataTemplate {
562 get { return emptyDataTemplate; }
563 set { emptyDataTemplate = value; RequireBinding (); }
566 [LocalizableAttribute (true)]
567 [WebCategoryAttribute ("Appearance")]
568 [DefaultValueAttribute ("")]
569 public virtual string EmptyDataText {
571 object ob = ViewState ["EmptyDataText"];
572 if (ob != null) return (string) ob;
576 ViewState ["EmptyDataText"] = value;
581 [WebCategoryAttribute ("Behavior")]
582 [DefaultValueAttribute (false)]
583 public virtual bool EnablePagingCallbacks {
585 object ob = ViewState ["EnablePagingCallbacks"];
586 if (ob != null) return (bool) ob;
590 ViewState ["EnablePagingCallbacks"] = value;
595 [WebCategoryAttribute ("Styles")]
596 [PersistenceMode (PersistenceMode.InnerProperty)]
597 [NotifyParentProperty (true)]
598 [DefaultValue (null)]
599 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
600 public virtual TableItemStyle FieldHeaderStyle {
602 if (fieldHeaderStyle == null) {
603 fieldHeaderStyle = new TableItemStyle ();
604 if (IsTrackingViewState)
605 fieldHeaderStyle.TrackViewState();
607 return fieldHeaderStyle;
611 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
612 [BrowsableAttribute (false)]
613 public virtual DetailsViewRow FooterRow {
615 EnsureChildControls ();
620 [DefaultValue (null)]
621 [TemplateContainer (typeof(DetailsView), BindingDirection.OneWay)]
622 [PersistenceMode (PersistenceMode.InnerProperty)]
624 public ITemplate FooterTemplate {
625 get { return footerTemplate; }
626 set { footerTemplate = value; RequireBinding (); }
629 [LocalizableAttribute (true)]
\r
630 [WebCategoryAttribute ("Appearance")]
\r
631 [DefaultValueAttribute ("")]
\r
632 public string FooterText {
634 object ob = ViewState ["FooterText"];
635 if (ob != null) return (string) ob;
639 ViewState ["FooterText"] = value;
644 [WebCategoryAttribute ("Styles")]
645 [PersistenceMode (PersistenceMode.InnerProperty)]
646 [NotifyParentProperty (true)]
647 [DefaultValue (null)]
648 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
649 public virtual TableItemStyle FooterStyle {
651 if (footerStyle == null) {
652 footerStyle = new TableItemStyle ();
653 if (IsTrackingViewState)
654 footerStyle.TrackViewState();
660 [WebCategoryAttribute ("Appearance")]
661 [DefaultValueAttribute (GridLines.Both)]
662 public virtual GridLines GridLines {
664 object ob = ViewState ["GridLines"];
665 if (ob != null) return (GridLines) ob;
666 return GridLines.Both;
669 ViewState ["GridLines"] = value;
673 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
674 [BrowsableAttribute (false)]
675 public virtual DetailsViewRow HeaderRow {
677 EnsureChildControls ();
682 [WebCategoryAttribute ("Styles")]
683 [PersistenceMode (PersistenceMode.InnerProperty)]
684 [NotifyParentProperty (true)]
685 [DefaultValue (null)]
686 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
687 public virtual TableItemStyle HeaderStyle {
689 if (headerStyle == null) {
690 headerStyle = new TableItemStyle ();
691 if (IsTrackingViewState)
692 headerStyle.TrackViewState();
698 [DefaultValue (null)]
699 [TemplateContainer (typeof(DetailsView), BindingDirection.OneWay)]
700 [PersistenceMode (PersistenceMode.InnerProperty)]
702 public ITemplate HeaderTemplate {
703 get { return headerTemplate; }
704 set { headerTemplate = value; RequireBinding (); }
707 [LocalizableAttribute (true)]
\r
708 [WebCategoryAttribute ("Appearance")]
\r
709 [DefaultValueAttribute ("")]
\r
710 public string HeaderText {
712 object ob = ViewState ["HeaderText"];
713 if (ob != null) return (string) ob;
717 ViewState ["HeaderText"] = value;
722 [DefaultValueAttribute (HorizontalAlign.NotSet)]
723 public virtual HorizontalAlign HorizontalAlign {
725 object ob = ViewState ["HorizontalAlign"];
726 if (ob != null) return (HorizontalAlign) ob;
727 return HorizontalAlign.NotSet;
730 ViewState ["HorizontalAlign"] = value;
735 [WebCategoryAttribute ("Styles")]
736 [PersistenceMode (PersistenceMode.InnerProperty)]
737 [NotifyParentProperty (true)]
738 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
739 [DefaultValueAttribute (null)]
\r
740 public virtual TableItemStyle InsertRowStyle {
742 if (insertRowStyle == null) {
743 insertRowStyle = new TableItemStyle ();
744 if (IsTrackingViewState)
745 insertRowStyle.TrackViewState();
747 return insertRowStyle;
751 [BrowsableAttribute (false)]
752 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
753 public int PageCount {
755 if (pageCount != -1) return pageCount;
761 [WebCategoryAttribute ("Paging")]
762 [BindableAttribute (true, BindingDirection.OneWay)]
\r
763 [DefaultValueAttribute (0)]
764 public int PageIndex {
774 [WebCategoryAttribute ("Paging")]
775 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
776 [NotifyParentPropertyAttribute (true)]
777 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
778 public PagerSettings PagerSettings {
780 if (pagerSettings == null) {
781 pagerSettings = new PagerSettings (this);
782 if (IsTrackingViewState)
783 ((IStateManager)pagerSettings).TrackViewState ();
785 return pagerSettings;
789 [WebCategoryAttribute ("Styles")]
790 [PersistenceMode (PersistenceMode.InnerProperty)]
791 [NotifyParentProperty (true)]
792 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
793 public virtual TableItemStyle PagerStyle {
795 if (pagerStyle == null) {
796 pagerStyle = new TableItemStyle ();
797 if (IsTrackingViewState)
798 pagerStyle.TrackViewState();
805 [DefaultValue (null)]
806 [TemplateContainer (typeof(DetailsView), BindingDirection.OneWay)]
807 [PersistenceMode (PersistenceMode.InnerProperty)]
809 public ITemplate PagerTemplate {
810 get { return pagerTemplate; }
811 set { pagerTemplate = value; RequireBinding (); }
814 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
815 [BrowsableAttribute (false)]
816 public virtual DetailsViewRowCollection Rows {
823 [WebCategoryAttribute ("Styles")]
824 [PersistenceMode (PersistenceMode.InnerProperty)]
825 [NotifyParentProperty (true)]
826 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
827 [DefaultValue (null)]
828 public virtual TableItemStyle RowStyle {
830 if (rowStyle == null) {
831 rowStyle = new TableItemStyle ();
832 if (IsTrackingViewState)
833 rowStyle.TrackViewState();
839 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
840 [BrowsableAttribute (false)]
841 public virtual object SelectedValue {
842 get { return DataKey.Value; }
845 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
846 [BrowsableAttribute (false)]
847 public virtual DetailsViewRow TopPagerRow {
854 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
855 [BrowsableAttribute (false)]
856 public object DataItem {
863 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
864 [BrowsableAttribute (false)]
865 public int DataItemCount {
866 get { return PageCount; }
869 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
870 [BrowsableAttribute (false)]
871 public int DataItemIndex {
872 get { return PageIndex; }
875 int IDataItemContainer.DisplayIndex {
876 get { return PageIndex; }
879 public virtual bool IsBindableType (Type type)
881 return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
884 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
886 return base.CreateDataSourceSelectArguments ();
889 protected virtual ICollection CreateFieldSet (object dataItem, bool useDataSource)
891 ArrayList fields = new ArrayList ();
893 if (AutoGenerateRows) {
895 if (dataItem != null)
896 fields.AddRange (CreateAutoGeneratedRows (dataItem));
898 if (autoFieldProperties != null) {
899 foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
900 fields.Add (CreateAutoGeneratedRow (props));
905 fields.AddRange (Fields);
907 if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateInsertButton) {
908 CommandField field = new CommandField ();
909 field.ShowEditButton = AutoGenerateEditButton;
910 field.ShowDeleteButton = AutoGenerateDeleteButton;
911 field.ShowInsertButton = AutoGenerateInsertButton;
913 commandField = field;
919 protected virtual ICollection CreateAutoGeneratedRows (object dataItem)
921 ArrayList list = new ArrayList ();
922 autoFieldProperties = CreateAutoFieldProperties (dataItem);
923 foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
924 list.Add (CreateAutoGeneratedRow (props));
928 protected virtual AutoGeneratedField CreateAutoGeneratedRow (AutoGeneratedFieldProperties fieldProperties)
930 return new AutoGeneratedField (fieldProperties);
933 AutoGeneratedFieldProperties[] CreateAutoFieldProperties (object dataItem)
935 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
937 ArrayList retVal = new ArrayList();
938 if (props != null && props.Count > 0)
940 foreach (PropertyDescriptor current in props) {
941 if (IsBindableType (current.PropertyType)) {
942 AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
943 ((IStateManager)field).TrackViewState();
944 field.Name = current.Name;
945 field.DataField = current.Name;
946 field.IsReadOnly = current.IsReadOnly;
947 field.Type = current.PropertyType;
953 if (retVal.Count > 0)
954 return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
956 return new AutoGeneratedFieldProperties [0];
959 protected virtual DetailsViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
961 DetailsViewRow row = new DetailsViewRow (rowIndex, rowType, rowState);
962 OnItemCreated (EventArgs.Empty);
966 void RequireBinding ()
969 RequiresDataBinding = true;
974 protected virtual Table CreateTable ()
976 Table table = new Table ();
977 table.Caption = Caption;
978 table.CaptionAlign = CaptionAlign;
979 table.CellPadding = CellPadding;
980 table.CellSpacing = CellSpacing;
981 table.HorizontalAlign = HorizontalAlign;
982 table.BackImageUrl = BackImageUrl;
986 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
988 PagedDataSource dataSource;
991 DataSourceView view = GetData ();
992 dataSource = new PagedDataSource ();
993 dataSource.DataSource = data;
996 dataSource.AllowPaging = true;
997 dataSource.PageSize = 1;
998 dataSource.CurrentPageIndex = PageIndex;
1000 dataSource.AllowServerPaging = true;
1001 if (view.CanRetrieveTotalRowCount)
1002 dataSource.VirtualCount = SelectArguments.TotalRowCount;
1004 dataSource.DataSourceView = view;
1005 dataSource.DataSourceSelectArguments = SelectArguments;
1006 dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
1011 pageCount = dataSource.PageCount;
1015 dataSource = new PagedDataSource ();
1016 dataSource.DataSource = data;
1018 dataSource.AllowPaging = true;
1019 dataSource.PageSize = 1;
1020 dataSource.CurrentPageIndex = PageIndex;
1024 bool showPager = AllowPaging && (PageCount > 1);
1027 table = CreateTable ();
1028 Controls.Add (table);
1030 ArrayList list = new ArrayList ();
1032 if (!Page.IsPostBack)
1033 currentMode = DefaultMode;
1036 // Gets the current data item
1038 IEnumerator e = dataSource.GetEnumerator ();
1040 dataItem = e.Current;
1044 // Creates the set of fields to show
1046 ICollection fieldCollection = CreateFieldSet (dataItem, dataBinding);
1047 DataControlField[] fields = new DataControlField [fieldCollection.Count];
1048 fieldCollection.CopyTo (fields, 0);
1050 foreach (DataControlField field in fields) {
1051 field.Initialize (false, this);
1052 if (EnablePagingCallbacks)
1053 field.ValidateSupportsCallback ();
1056 // Main table creation
1058 if (HeaderText.Length != 0 || headerTemplate != null) {
1059 headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
1060 DataControlFieldCell cell = new DataControlFieldCell (null);
1061 cell.ColumnSpan = 2;
1062 if (headerTemplate != null)
1063 headerTemplate.InstantiateIn (cell);
1065 cell.Text = HeaderText;
1066 headerRow.Cells.Add (cell);
1067 table.Rows.Add (headerRow);
1070 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
1071 topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
1072 InitializePager (topPagerRow, dataSource);
1073 table.Rows.Add (topPagerRow);
1076 if (dataSource.Count > 0) {
1077 foreach (DataControlField field in fields) {
1078 DataControlRowState rstate = GetRowState (list.Count);
1079 DetailsViewRow row = CreateRow (list.Count, DataControlRowType.DataRow, rstate);
1080 InitializeRow (row, field);
1081 table.Rows.Add (row);
1084 if (commandField == field)
1088 if (CurrentMode == DetailsViewMode.Edit)
1089 oldEditValues = new DataKey (new OrderedDictionary ());
1090 key = new DataKey (new OrderedDictionary (), DataKeyNames);
1093 table.Rows.Add (CreateEmptyrRow ());
1096 rows = new DetailsViewRowCollection (list);
1098 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
1099 bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
1100 InitializePager (bottomPagerRow, dataSource);
1101 table.Rows.Add (bottomPagerRow);
1104 if (FooterText.Length != 0 || footerTemplate != null) {
1105 footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
1106 DataControlFieldCell cell = new DataControlFieldCell (null);
1107 cell.ColumnSpan = 2;
1108 if (footerTemplate != null)
1109 footerTemplate.InstantiateIn (cell);
1111 cell.Text = FooterText;
1112 footerRow.Cells.Add (cell);
1113 table.Rows.Add (footerRow);
1119 return dataSource.DataSourceCount;
1122 DataControlRowState GetRowState (int index)
1124 DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
1125 if (CurrentMode == DetailsViewMode.Edit) rstate |= DataControlRowState.Edit;
1126 else if (CurrentMode == DetailsViewMode.Insert) rstate |= DataControlRowState.Insert;
1130 protected virtual void InitializePager (DetailsViewRow row, PagedDataSource dataSource)
1132 TableCell cell = new TableCell ();
1133 cell.ColumnSpan = 2;
1135 if (pagerTemplate != null)
1136 pagerTemplate.InstantiateIn (cell);
1138 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
1140 row.Cells.Add (cell);
1143 DetailsViewRow CreateEmptyrRow ()
1145 DetailsViewRow row = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
1146 TableCell cell = new TableCell ();
1147 cell.ColumnSpan = 2;
1149 if (emptyDataTemplate != null)
1150 emptyDataTemplate.InstantiateIn (cell);
1152 cell.Text = EmptyDataText;
1154 row.Cells.Add (cell);
1158 protected virtual void InitializeRow (DetailsViewRow row, DataControlField field)
1160 DataControlFieldCell cell;
1162 if (field.ShowHeader) {
1163 cell = new DataControlFieldCell (field);
1164 row.Cells.Add (cell);
1165 field.InitializeCell (cell, DataControlCellType.Header, row.RowState, row.RowIndex);
1168 cell = new DataControlFieldCell (field);
1169 if (!field.ShowHeader)
1170 cell.ColumnSpan = 2;
1171 row.Cells.Add (cell);
1172 field.InitializeCell (cell, DataControlCellType.DataCell, row.RowState, row.RowIndex);
1175 IOrderedDictionary CreateRowDataKey (object dataItem)
1177 if (cachedKeyProperties == null) {
1178 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
1179 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1180 for (int n=0; n<DataKeyNames.Length; n++) {
1181 PropertyDescriptor p = props [DataKeyNames[n]];
1183 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
1184 cachedKeyProperties [n] = p;
1188 OrderedDictionary dic = new OrderedDictionary ();
1189 foreach (PropertyDescriptor p in cachedKeyProperties)
1190 dic [p.Name] = p.GetValue (dataItem);
1194 IOrderedDictionary GetRowValues (bool includeReadOnlyFields, bool includePrimaryKey)
1196 OrderedDictionary dic = new OrderedDictionary ();
1197 ExtractRowValues (dic, includeReadOnlyFields, includePrimaryKey);
1201 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeReadOnlyFields, bool includePrimaryKey)
1203 foreach (DetailsViewRow row in Rows) {
1204 if (row.Cells.Count < 1) continue;
1205 DataControlFieldCell c = row.Cells[row.Cells.Count-1] as DataControlFieldCell;
1207 c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
1209 if (!includePrimaryKey && DataKeyNames != null)
1210 foreach (string key in DataKeyNames)
1211 fieldValues.Remove (key);
1214 protected override HtmlTextWriterTag TagKey {
1216 if (EnablePagingCallbacks)
1217 return HtmlTextWriterTag.Div;
1219 return HtmlTextWriterTag.Table;
1223 public sealed override void DataBind ()
1225 DataSourceView view = GetData ();
1226 if (AllowPaging && view.CanPage) {
1227 SelectArguments.StartRowIndex = PageIndex;
1228 SelectArguments.MaximumRows = 1;
1229 if (view.CanRetrieveTotalRowCount)
1230 SelectArguments.RetrieveTotalRowCount = true;
1233 cachedKeyProperties = null;
1236 if (dataItem != null) {
1237 if (CurrentMode == DetailsViewMode.Edit)
1238 oldEditValues = new DataKey (GetRowValues (false, true));
1239 key = new DataKey (CreateRowDataKey (dataItem), DataKeyNames);
1243 protected override void PerformDataBinding (IEnumerable data)
1245 base.PerformDataBinding (data);
1248 protected override void OnInit (EventArgs e)
1250 Page.RegisterRequiresControlState (this);
1254 void OnFieldsChanged (object sender, EventArgs args)
1259 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1261 base.OnDataSourceViewChanged (sender, e);
1265 protected override bool OnBubbleEvent (object source, EventArgs e)
1267 DetailsViewCommandEventArgs args = e as DetailsViewCommandEventArgs;
1269 OnItemCommand (args);
1270 ProcessEvent (args.CommandName, args.CommandArgument as string);
1272 return base.OnBubbleEvent (source, e);
1275 // TODO: This is prolly obsolete
1276 protected virtual void RaisePostBackEvent (string eventArgument)
1278 int i = eventArgument.IndexOf ('$');
1280 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1282 ProcessEvent (eventArgument, null);
1285 void ProcessEvent (string eventName, string param)
1289 case DataControlCommands.PageCommandName:
1292 case DataControlCommands.FirstPageCommandArgument:
1295 case DataControlCommands.LastPageCommandArgument:
1296 newIndex = PageCount - 1;
1298 case DataControlCommands.NextPageCommandArgument:
1299 if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1301 case DataControlCommands.PreviousPageCommandArgument:
1302 if (PageIndex > 0) newIndex = PageIndex - 1;
1305 newIndex = int.Parse (param) - 1;
1308 ShowPage (newIndex);
1311 case DataControlCommands.FirstPageCommandArgument:
1315 case DataControlCommands.LastPageCommandArgument:
1316 ShowPage (PageCount - 1);
1319 case DataControlCommands.NextPageCommandArgument:
1320 if (PageIndex < PageCount - 1)
1321 ShowPage (PageIndex + 1);
1324 case DataControlCommands.PreviousPageCommandArgument:
1326 ShowPage (PageIndex - 1);
1329 case DataControlCommands.EditCommandName:
1330 ChangeMode (DetailsViewMode.Edit);
1333 case DataControlCommands.NewCommandName:
1334 ChangeMode (DetailsViewMode.Insert);
1337 case DataControlCommands.UpdateCommandName:
1338 UpdateItem (param, true);
1341 case DataControlCommands.CancelCommandName:
1345 case DataControlCommands.DeleteCommandName:
1349 case DataControlCommands.InsertCommandName:
1355 void ShowPage (int newIndex)
1357 DetailsViewPageEventArgs args = new DetailsViewPageEventArgs (newIndex);
1358 OnPageIndexChanging (args);
1361 PageIndex = args.NewPageIndex;
1362 OnPageIndexChanged (EventArgs.Empty);
1366 public void ChangeMode (DetailsViewMode newMode)
1368 DetailsViewModeEventArgs args = new DetailsViewModeEventArgs (newMode, false);
1369 OnModeChanging (args);
1371 currentMode = args.NewMode;
1372 OnModeChanged (EventArgs.Empty);
1379 DetailsViewModeEventArgs args = new DetailsViewModeEventArgs (DetailsViewMode.ReadOnly, true);
1380 OnModeChanging (args);
1386 public virtual void UpdateItem (bool causesValidation)
1388 UpdateItem (null, causesValidation);
1391 void UpdateItem (string param, bool causesValidation)
1393 if (causesValidation)
1396 if (currentMode != DetailsViewMode.Edit) throw new NotSupportedException ();
1398 currentEditOldValues = oldEditValues.Values;
1400 currentEditRowKeys = DataKey.Values;
1401 currentEditNewValues = GetRowValues (false, false);
1403 DetailsViewUpdateEventArgs args = new DetailsViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1404 OnItemUpdating (args);
1406 DataSourceView view = GetData ();
1407 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1408 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1413 bool UpdateCallback (int recordsAffected, Exception exception)
1415 DetailsViewUpdatedEventArgs dargs = new DetailsViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1416 OnItemUpdated (dargs);
1418 if (!dargs.KeepInEditMode)
1421 return dargs.ExceptionHandled;
1424 public virtual void InsertItem (bool causesValidation)
1426 InsertItem (null, causesValidation);
1429 void InsertItem (string param, bool causesValidation)
1431 if (causesValidation)
1434 if (currentMode != DetailsViewMode.Insert) throw new NotSupportedException ();
1436 currentEditNewValues = GetRowValues (false, true);
1437 DetailsViewInsertEventArgs args = new DetailsViewInsertEventArgs (param, currentEditNewValues);
1438 OnItemInserting (args);
1440 DataSourceView view = GetData ();
1441 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1442 view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
1447 bool InsertCallback (int recordsAffected, Exception exception)
1449 DetailsViewInsertedEventArgs dargs = new DetailsViewInsertedEventArgs (recordsAffected, exception, currentEditNewValues);
1450 OnItemInserted (dargs);
1452 if (!dargs.KeepInInsertMode)
1455 return dargs.ExceptionHandled;
1458 public void DeleteItem ()
1460 currentEditRowKeys = DataKey.Values;
1461 currentEditNewValues = GetRowValues (true, true);
1463 DetailsViewDeleteEventArgs args = new DetailsViewDeleteEventArgs (PageIndex, currentEditRowKeys, currentEditNewValues);
1464 OnItemDeleting (args);
1467 if (PageIndex == PageCount - 1)
1470 DataSourceView view = GetData ();
1472 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1474 DetailsViewDeletedEventArgs dargs = new DetailsViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1475 OnItemDeleted (dargs);
1480 bool DeleteCallback (int recordsAffected, Exception exception)
1482 DetailsViewDeletedEventArgs dargs = new DetailsViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1483 OnItemDeleted (dargs);
1484 return dargs.ExceptionHandled;
1489 ChangeMode (DefaultMode);
1490 oldEditValues = new DataKey (new OrderedDictionary ());
1491 currentEditRowKeys = null;
1492 currentEditOldValues = null;
1493 currentEditNewValues = null;
1497 protected internal override void LoadControlState (object ob)
1499 if (ob == null) return;
1500 object[] state = (object[]) ob;
1501 base.LoadControlState (state[0]);
1502 pageIndex = (int) state[1];
1503 pageCount = (int) state[2];
1504 currentMode = (DetailsViewMode) state[3];
1507 protected internal override object SaveControlState ()
1509 object bstate = base.SaveControlState ();
1510 return new object[] {
1511 bstate, pageIndex, pageCount, currentMode
1515 protected override void TrackViewState()
1517 base.TrackViewState();
1518 if (columns != null) ((IStateManager)columns).TrackViewState();
1519 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1520 if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
1521 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1522 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1523 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1524 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1525 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1526 if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
1527 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1528 if (key != null) ((IStateManager)key).TrackViewState();
1529 if (autoFieldProperties != null) {
1530 foreach (IStateManager sm in autoFieldProperties)
1531 sm.TrackViewState ();
1535 protected override object SaveViewState()
1537 object[] states = new object [14];
1538 states[0] = base.SaveViewState();
1539 states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
1540 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1541 states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
1542 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1543 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1544 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1545 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1546 states[8] = (insertRowStyle == null ? null : ((IStateManager)insertRowStyle).SaveViewState());
1547 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1548 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1549 states[11] = (key == null ? null : ((IStateManager)key).SaveViewState());
1550 states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1552 if (autoFieldProperties != null) {
1553 object[] data = new object [autoFieldProperties.Length];
1554 bool allNull = true;
1555 for (int n=0; n<data.Length; n++) {
1556 data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
1557 if (data [n] != null) allNull = false;
1559 if (!allNull) states [13] = data;
1562 for (int i = states.Length - 1; i >= 0; i--) {
1563 if (states [i] != null)
1570 protected override void LoadViewState (object savedState)
1572 if (savedState == null) {
1573 base.LoadViewState (null);
1577 object [] states = (object []) savedState;
1579 if (states[13] != null) {
1580 object[] data = (object[]) states [13];
1581 autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
1582 for (int n=0; n<data.Length; n++) {
1583 IStateManager p = new AutoGeneratedFieldProperties ();
1584 p.TrackViewState ();
1585 p.LoadViewState (data [n]);
1586 autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
1590 base.LoadViewState (states[0]);
1591 EnsureChildControls ();
1593 if (states[1] != null) ((IStateManager)Fields).LoadViewState (states[1]);
1594 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1595 if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
1596 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1597 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1598 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1599 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1600 if (states[8] != null) ((IStateManager)InsertRowStyle).LoadViewState (states[8]);
1601 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1602 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1603 if (states[11] != null) ((IStateManager)DataKey).LoadViewState (states[11]);
1604 if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1607 string ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
1609 return RaiseCallbackEvent (eventArgs);
1612 protected virtual string RaiseCallbackEvent (string eventArgs)
1614 string[] clientData = eventArgs.Split ('|');
1615 pageIndex = int.Parse (clientData[0]);
1618 RaisePostBackEvent (clientData[2]);
1621 StringWriter sw = new StringWriter ();
1622 sw.Write (PageIndex.ToString());
1624 HtmlTextWriter writer = new HtmlTextWriter (sw);
1625 RenderGrid (writer);
1626 return sw.ToString ();
1629 string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
1631 if (EnablePagingCallbacks)
1632 return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
1637 protected override void OnPreRender (EventArgs e)
1639 base.OnPreRender (e);
1641 if (EnablePagingCallbacks)
1643 if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
1644 string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
1645 Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
1648 string cgrid = ClientID + "_data";
1649 string script = string.Format ("var {0} = new Object ();\n", cgrid);
1650 script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
1651 script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
1652 Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
1654 // Make sure the basic script infrastructure is rendered
1655 Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
1656 Page.ClientScript.GetPostBackClientHyperlink (this, "");
1660 protected override void Render (HtmlTextWriter writer)
1662 if (EnablePagingCallbacks)
1663 base.RenderBeginTag (writer);
1665 RenderGrid (writer);
1667 if (EnablePagingCallbacks)
1668 base.RenderEndTag (writer);
1671 void RenderGrid (HtmlTextWriter writer)
1673 switch (GridLines) {
1674 case GridLines.Horizontal:
1675 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
1676 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1678 case GridLines.Vertical:
1679 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
1680 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1682 case GridLines.Both:
1683 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
1684 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1687 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
1691 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
1692 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
1693 table.RenderBeginTag (writer);
1695 foreach (DetailsViewRow row in table.Rows)
1697 switch (row.RowType) {
1698 case DataControlRowType.Header:
1699 if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1701 case DataControlRowType.Footer:
1702 if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1704 case DataControlRowType.Pager:
1705 if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1707 case DataControlRowType.EmptyDataRow:
1708 if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1711 if (rowStyle != null) rowStyle.AddAttributesToRender (writer, row);
1715 if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null)
1716 alternatingRowStyle.AddAttributesToRender (writer, row);
1717 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1718 editRowStyle.AddAttributesToRender (writer, row);
1719 if ((row.RowState & DataControlRowState.Insert) != 0 && insertRowStyle != null)
1720 insertRowStyle.AddAttributesToRender (writer, row);
1722 if (row == commandRow && commandRowStyle != null)
1723 commandRowStyle.AddAttributesToRender (writer, row);
1725 row.RenderBeginTag (writer);
1727 for (int n=0; n<row.Cells.Count; n++) {
1728 DataControlFieldCell fcell = row.Cells[n] as DataControlFieldCell;
1729 if (fcell != null && fcell.ContainingField != null) {
1730 if (n == 0 && fcell.ContainingField.ShowHeader) {
1731 if (fieldHeaderStyle != null)
1732 fieldHeaderStyle.AddAttributesToRender (writer, fcell);
1733 fcell.ContainingField.HeaderStyle.AddAttributesToRender (writer, fcell);
1736 fcell.ContainingField.ItemStyle.AddAttributesToRender (writer, fcell);
1738 row.Cells[n].Render (writer);
1740 row.RenderEndTag (writer);
1742 table.RenderEndTag (writer);