2 // System.Web.UI.WebControls.FormView.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.FormViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
44 [ControlValuePropertyAttribute ("SelectedValue")]
45 [DefaultEventAttribute ("PageIndexChanging")]
46 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48 public class FormView: CompositeDataBoundControl, IDataItemContainer
53 FormViewRow headerRow;
54 FormViewRow footerRow;
55 FormViewRow bottomPagerRow;
56 FormViewRow topPagerRow;
59 IOrderedDictionary currentEditRowKeys;
60 IOrderedDictionary currentEditNewValues;
61 IOrderedDictionary currentEditOldValues;
63 ITemplate pagerTemplate;
64 ITemplate emptyDataTemplate;
65 ITemplate headerTemplate;
66 ITemplate footerTemplate;
67 ITemplate editItemTemplate;
68 ITemplate insertItemTemplate;
69 ITemplate itemTemplate;
71 PropertyDescriptor[] cachedKeyProperties;
72 readonly string[] emptyKeys = new string[0];
75 PagerSettings pagerSettings;
77 TableItemStyle editRowStyle;
78 TableItemStyle insertRowStyle;
79 TableItemStyle emptyDataRowStyle;
80 TableItemStyle footerStyle;
81 TableItemStyle headerStyle;
82 TableItemStyle pagerStyle;
83 TableItemStyle rowStyle;
86 DataKey oldEditValues;
89 private static readonly object PageIndexChangedEvent = new object();
90 private static readonly object PageIndexChangingEvent = new object();
91 private static readonly object ItemCommandEvent = new object();
92 private static readonly object ItemCreatedEvent = new object();
93 private static readonly object ItemDeletedEvent = new object();
94 private static readonly object ItemDeletingEvent = new object();
95 private static readonly object ItemInsertedEvent = new object();
96 private static readonly object ItemInsertingEvent = new object();
97 private static readonly object ModeChangingEvent = new object();
98 private static readonly object ModeChangedEvent = new object();
99 private static readonly object ItemUpdatedEvent = new object();
100 private static readonly object ItemUpdatingEvent = new object();
104 FormViewMode currentMode = FormViewMode.ReadOnly;
111 public event EventHandler PageIndexChanged {
112 add { Events.AddHandler (PageIndexChangedEvent, value); }
113 remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
116 public event FormViewPageEventHandler PageIndexChanging {
117 add { Events.AddHandler (PageIndexChangingEvent, value); }
118 remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
121 public event FormViewCommandEventHandler ItemCommand {
122 add { Events.AddHandler (ItemCommandEvent, value); }
123 remove { Events.RemoveHandler (ItemCommandEvent, value); }
126 public event EventHandler ItemCreated {
127 add { Events.AddHandler (ItemCreatedEvent, value); }
128 remove { Events.RemoveHandler (ItemCreatedEvent, value); }
131 public event FormViewDeletedEventHandler ItemDeleted {
132 add { Events.AddHandler (ItemDeletedEvent, value); }
133 remove { Events.RemoveHandler (ItemDeletedEvent, value); }
136 public event FormViewDeleteEventHandler ItemDeleting {
137 add { Events.AddHandler (ItemDeletingEvent, value); }
138 remove { Events.RemoveHandler (ItemDeletingEvent, value); }
141 public event FormViewInsertedEventHandler ItemInserted {
142 add { Events.AddHandler (ItemInsertedEvent, value); }
143 remove { Events.RemoveHandler (ItemInsertedEvent, value); }
146 public event FormViewInsertEventHandler ItemInserting {
147 add { Events.AddHandler (ItemInsertingEvent, value); }
148 remove { Events.RemoveHandler (ItemInsertingEvent, value); }
151 public event FormViewModeEventHandler ModeChanging {
152 add { Events.AddHandler (ModeChangingEvent, value); }
153 remove { Events.RemoveHandler (ModeChangingEvent, value); }
156 public event EventHandler ModeChanged {
157 add { Events.AddHandler (ModeChangedEvent, value); }
158 remove { Events.RemoveHandler (ModeChangedEvent, value); }
161 public event FormViewUpdatedEventHandler ItemUpdated {
162 add { Events.AddHandler (ItemUpdatedEvent, value); }
163 remove { Events.RemoveHandler (ItemUpdatedEvent, value); }
166 public event FormViewUpdateEventHandler ItemUpdating {
167 add { Events.AddHandler (ItemUpdatingEvent, value); }
168 remove { Events.RemoveHandler (ItemUpdatingEvent, value); }
171 protected virtual void OnPageIndexChanged (EventArgs e)
173 if (Events != null) {
174 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
175 if (eh != null) eh (this, e);
179 protected virtual void OnPageIndexChanging (FormViewPageEventArgs e)
181 if (Events != null) {
182 FormViewPageEventHandler eh = (FormViewPageEventHandler) Events [PageIndexChangingEvent];
183 if (eh != null) eh (this, e);
187 protected virtual void OnItemCommand (FormViewCommandEventArgs e)
189 if (Events != null) {
190 FormViewCommandEventHandler eh = (FormViewCommandEventHandler) Events [ItemCommandEvent];
191 if (eh != null) eh (this, e);
195 protected virtual void OnItemCreated (EventArgs e)
197 if (Events != null) {
198 EventHandler eh = (EventHandler) Events [ItemCreatedEvent];
199 if (eh != null) eh (this, e);
203 protected virtual void OnItemDeleted (FormViewDeletedEventArgs e)
205 if (Events != null) {
206 FormViewDeletedEventHandler eh = (FormViewDeletedEventHandler) Events [ItemDeletedEvent];
207 if (eh != null) eh (this, e);
211 protected virtual void OnItemInserted (FormViewInsertedEventArgs e)
213 if (Events != null) {
214 FormViewInsertedEventHandler eh = (FormViewInsertedEventHandler) Events [ItemInsertedEvent];
215 if (eh != null) eh (this, e);
219 protected virtual void OnItemInserting (FormViewInsertEventArgs e)
221 if (Events != null) {
222 FormViewInsertEventHandler eh = (FormViewInsertEventHandler) Events [ItemInsertingEvent];
223 if (eh != null) eh (this, e);
227 protected virtual void OnItemDeleting (FormViewDeleteEventArgs e)
229 if (Events != null) {
230 FormViewDeleteEventHandler eh = (FormViewDeleteEventHandler) Events [ItemDeletingEvent];
231 if (eh != null) eh (this, e);
235 protected virtual void OnModeChanged (EventArgs e)
237 if (Events != null) {
238 EventHandler eh = (EventHandler) Events [ModeChangedEvent];
239 if (eh != null) eh (this, e);
243 protected virtual void OnModeChanging (FormViewModeEventArgs e)
245 if (Events != null) {
246 FormViewModeEventHandler eh = (FormViewModeEventHandler) Events [ModeChangingEvent];
247 if (eh != null) eh (this, e);
251 protected virtual void OnItemUpdated (FormViewUpdatedEventArgs e)
253 if (Events != null) {
254 FormViewUpdatedEventHandler eh = (FormViewUpdatedEventHandler) Events [ItemUpdatedEvent];
255 if (eh != null) eh (this, e);
259 protected virtual void OnItemUpdating (FormViewUpdateEventArgs e)
261 if (Events != null) {
262 FormViewUpdateEventHandler eh = (FormViewUpdateEventHandler) Events [ItemUpdatingEvent];
263 if (eh != null) eh (this, e);
268 [WebCategoryAttribute ("Paging")]
269 [DefaultValueAttribute (false)]
270 public bool AllowPaging {
272 object ob = ViewState ["AllowPaging"];
273 if (ob != null) return (bool) ob;
277 ViewState ["AllowPaging"] = value;
282 [UrlPropertyAttribute]
283 [WebCategoryAttribute ("Appearance")]
284 [DefaultValueAttribute ("")]
285 [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")]
286 public virtual string BackImageUrl {
288 object ob = ViewState ["BackImageUrl"];
289 if (ob != null) return (string) ob;
293 ViewState ["BackImageUrl"] = value;
298 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
299 [BrowsableAttribute (false)]
300 public virtual FormViewRow BottomPagerRow {
303 return bottomPagerRow;
307 [WebCategoryAttribute ("Accessibility")]
308 [DefaultValueAttribute ("")]
309 [LocalizableAttribute (true)]
310 public string Caption {
312 object ob = ViewState ["Caption"];
313 if (ob != null) return (string) ob;
317 ViewState ["Caption"] = value;
322 [WebCategoryAttribute ("Accessibility")]
323 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
324 public virtual TableCaptionAlign CaptionAlign
327 object o = ViewState ["CaptionAlign"];
328 if(o != null) return (TableCaptionAlign) o;
329 return TableCaptionAlign.NotSet;
332 ViewState ["CaptionAlign"] = value;
337 [WebCategoryAttribute ("Layout")]
338 [DefaultValueAttribute (-1)]
339 public virtual int CellPadding
342 object o = ViewState ["CellPadding"];
343 if (o != null) return (int) o;
347 ViewState ["CellPadding"] = value;
352 [WebCategoryAttribute ("Layout")]
353 [DefaultValueAttribute (0)]
354 public virtual int CellSpacing
357 object o = ViewState ["CellSpacing"];
358 if (o != null) return (int) o;
362 ViewState ["CellSpacing"] = value;
367 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
368 [BrowsableAttribute (false)]
\r
369 public FormViewMode CurrentMode {
375 [DefaultValueAttribute (FormViewMode.ReadOnly)]
\r
376 [WebCategoryAttribute ("Behavior")]
\r
377 public virtual FormViewMode DefaultMode {
379 object o = ViewState ["DefaultMode"];
380 if (o != null) return (FormViewMode) o;
381 return FormViewMode.ReadOnly;
384 ViewState ["DefaultMode"] = value;
389 [DefaultValueAttribute (null)]
390 [WebCategoryAttribute ("Data")]
391 [TypeConverter (typeof(StringArrayConverter))]
392 [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")]
393 public virtual string[] DataKeyNames
396 object o = ViewState ["DataKeyNames"];
397 if (o != null) return (string[]) o;
401 ViewState ["DataKeyNames"] = value;
406 [BrowsableAttribute (false)]
407 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
408 public virtual DataKey DataKey {
415 [DefaultValue (null)]
416 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
417 [PersistenceMode (PersistenceMode.InnerProperty)]
419 public ITemplate EditItemTemplate {
420 get { return editItemTemplate; }
421 set { editItemTemplate = value; RequireBinding (); }
424 [WebCategoryAttribute ("Styles")]
425 [PersistenceMode (PersistenceMode.InnerProperty)]
426 [NotifyParentProperty (true)]
427 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
428 [DefaultValueAttribute (null)]
\r
429 public virtual TableItemStyle EditRowStyle {
431 if (editRowStyle == null) {
432 editRowStyle = new TableItemStyle ();
433 if (IsTrackingViewState)
434 editRowStyle.TrackViewState();
440 [WebCategoryAttribute ("Styles")]
441 [PersistenceMode (PersistenceMode.InnerProperty)]
442 [NotifyParentProperty (true)]
443 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
444 [DefaultValueAttribute (null)]
\r
445 public virtual TableItemStyle EmptyDataRowStyle {
447 if (emptyDataRowStyle == null) {
448 emptyDataRowStyle = new TableItemStyle ();
449 if (IsTrackingViewState)
450 emptyDataRowStyle.TrackViewState();
452 return emptyDataRowStyle;
456 [DefaultValue (null)]
457 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
458 [PersistenceMode (PersistenceMode.InnerProperty)]
460 public ITemplate EmptyDataTemplate {
461 get { return emptyDataTemplate; }
462 set { emptyDataTemplate = value; RequireBinding (); }
465 [LocalizableAttribute (true)]
466 [WebCategoryAttribute ("Appearance")]
467 [DefaultValueAttribute ("")]
468 public virtual string EmptyDataText {
470 object ob = ViewState ["EmptyDataText"];
471 if (ob != null) return (string) ob;
475 ViewState ["EmptyDataText"] = value;
480 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
481 [BrowsableAttribute (false)]
482 public virtual FormViewRow FooterRow {
484 EnsureChildControls ();
489 [DefaultValue (null)]
490 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
491 [PersistenceMode (PersistenceMode.InnerProperty)]
493 public ITemplate FooterTemplate {
494 get { return footerTemplate; }
495 set { footerTemplate = value; RequireBinding (); }
498 [LocalizableAttribute (true)]
\r
499 [WebCategoryAttribute ("Appearance")]
\r
500 [DefaultValueAttribute ("")]
\r
501 public string FooterText {
503 object ob = ViewState ["FooterText"];
504 if (ob != null) return (string) ob;
508 ViewState ["FooterText"] = value;
513 [WebCategoryAttribute ("Styles")]
514 [PersistenceMode (PersistenceMode.InnerProperty)]
515 [NotifyParentProperty (true)]
516 [DefaultValue (null)]
517 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
518 public virtual TableItemStyle FooterStyle {
520 if (footerStyle == null) {
521 footerStyle = new TableItemStyle ();
522 if (IsTrackingViewState)
523 footerStyle.TrackViewState();
529 [WebCategoryAttribute ("Appearance")]
530 [DefaultValueAttribute (GridLines.None)]
531 public virtual GridLines GridLines {
533 object ob = ViewState ["GridLines"];
534 if (ob != null) return (GridLines) ob;
535 return GridLines.None;
538 ViewState ["GridLines"] = value;
542 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
543 [BrowsableAttribute (false)]
544 public virtual FormViewRow HeaderRow {
546 EnsureChildControls ();
551 [WebCategoryAttribute ("Styles")]
552 [PersistenceMode (PersistenceMode.InnerProperty)]
553 [NotifyParentProperty (true)]
554 [DefaultValue (null)]
555 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
556 public virtual TableItemStyle HeaderStyle {
558 if (headerStyle == null) {
559 headerStyle = new TableItemStyle ();
560 if (IsTrackingViewState)
561 headerStyle.TrackViewState();
567 [DefaultValue (null)]
568 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
569 [PersistenceMode (PersistenceMode.InnerProperty)]
571 public ITemplate HeaderTemplate {
572 get { return headerTemplate; }
573 set { headerTemplate = value; RequireBinding (); }
576 [LocalizableAttribute (true)]
\r
577 [WebCategoryAttribute ("Appearance")]
\r
578 [DefaultValueAttribute ("")]
\r
579 public string HeaderText {
581 object ob = ViewState ["HeaderText"];
582 if (ob != null) return (string) ob;
586 ViewState ["HeaderText"] = value;
591 [DefaultValueAttribute (HorizontalAlign.NotSet)]
592 public virtual HorizontalAlign HorizontalAlign {
594 object ob = ViewState ["HorizontalAlign"];
595 if (ob != null) return (HorizontalAlign) ob;
596 return HorizontalAlign.NotSet;
599 ViewState ["HorizontalAlign"] = value;
604 [DefaultValue (null)]
605 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
606 [PersistenceMode (PersistenceMode.InnerProperty)]
608 public ITemplate InsertItemTemplate {
609 get { return insertItemTemplate; }
610 set { insertItemTemplate = value; RequireBinding (); }
613 [WebCategoryAttribute ("Styles")]
614 [PersistenceMode (PersistenceMode.InnerProperty)]
615 [NotifyParentProperty (true)]
616 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
617 [DefaultValueAttribute (null)]
\r
618 public virtual TableItemStyle InsertRowStyle {
620 if (insertRowStyle == null) {
621 insertRowStyle = new TableItemStyle ();
622 if (IsTrackingViewState)
623 insertRowStyle.TrackViewState();
625 return insertRowStyle;
629 [DefaultValue (null)]
630 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
631 [PersistenceMode (PersistenceMode.InnerProperty)]
633 public ITemplate ItemTemplate {
634 get { return itemTemplate; }
635 set { itemTemplate = value; RequireBinding (); }
638 [BrowsableAttribute (false)]
639 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
640 public int PageCount {
642 if (pageCount != -1) return pageCount;
648 [WebCategoryAttribute ("Paging")]
649 [BindableAttribute (true, BindingDirection.OneWay)]
\r
650 [DefaultValueAttribute (0)]
651 public int PageIndex {
661 [WebCategoryAttribute ("Paging")]
662 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
663 [NotifyParentPropertyAttribute (true)]
664 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
665 public PagerSettings PagerSettings {
667 if (pagerSettings == null) {
668 pagerSettings = new PagerSettings (this);
669 if (IsTrackingViewState)
670 ((IStateManager)pagerSettings).TrackViewState ();
672 return pagerSettings;
676 [WebCategoryAttribute ("Styles")]
677 [PersistenceMode (PersistenceMode.InnerProperty)]
678 [NotifyParentProperty (true)]
679 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
680 public virtual TableItemStyle PagerStyle {
682 if (pagerStyle == null) {
683 pagerStyle = new TableItemStyle ();
684 if (IsTrackingViewState)
685 pagerStyle.TrackViewState();
692 [DefaultValue (null)]
693 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
694 [PersistenceMode (PersistenceMode.InnerProperty)]
696 public ITemplate PagerTemplate {
697 get { return pagerTemplate; }
698 set { pagerTemplate = value; RequireBinding (); }
701 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
702 [BrowsableAttribute (false)]
\r
703 public FormViewRow Row {
710 [WebCategoryAttribute ("Styles")]
711 [PersistenceMode (PersistenceMode.InnerProperty)]
712 [NotifyParentProperty (true)]
713 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
714 [DefaultValue (null)]
715 public virtual TableItemStyle RowStyle {
717 if (rowStyle == null) {
718 rowStyle = new TableItemStyle ();
719 if (IsTrackingViewState)
720 rowStyle.TrackViewState();
726 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
727 [BrowsableAttribute (false)]
728 public virtual object SelectedValue {
729 get { return DataKey.Value; }
732 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
733 [BrowsableAttribute (false)]
734 public virtual FormViewRow TopPagerRow {
741 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
742 [BrowsableAttribute (false)]
743 public object DataItem {
750 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
751 [BrowsableAttribute (false)]
752 public int DataItemCount {
753 get { return PageCount; }
756 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
757 [BrowsableAttribute (false)]
758 public int DataItemIndex {
759 get { return PageIndex; }
762 public virtual bool IsBindableType (Type type)
764 return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
767 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
769 return base.CreateDataSourceSelectArguments ();
772 protected virtual FormViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
774 FormViewRow row = new FormViewRow (rowIndex, rowType, rowState);
775 OnItemCreated (EventArgs.Empty);
779 void RequireBinding ()
782 RequiresDataBinding = true;
787 protected virtual Table CreateTable ()
789 Table table = new Table ();
790 table.Caption = Caption;
791 table.CaptionAlign = CaptionAlign;
792 table.CellPadding = CellPadding;
793 table.CellSpacing = CellSpacing;
794 table.HorizontalAlign = HorizontalAlign;
795 table.BackImageUrl = BackImageUrl;
799 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
801 PagedDataSource dataSource;
804 DataSourceView view = GetData ();
805 dataSource = new PagedDataSource ();
806 dataSource.DataSource = data;
809 dataSource.AllowPaging = true;
810 dataSource.PageSize = 1;
811 dataSource.CurrentPageIndex = PageIndex;
813 dataSource.AllowServerPaging = true;
814 if (view.CanRetrieveTotalRowCount)
815 dataSource.VirtualCount = SelectArguments.TotalRowCount;
817 dataSource.DataSourceView = view;
818 dataSource.DataSourceSelectArguments = SelectArguments;
819 dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
824 pageCount = dataSource.PageCount;
828 dataSource = new PagedDataSource ();
829 dataSource.DataSource = data;
831 dataSource.AllowPaging = true;
832 dataSource.PageSize = 1;
833 dataSource.CurrentPageIndex = PageIndex;
837 bool showPager = AllowPaging && (PageCount > 1);
838 dataSourceCount = dataSource.Count;
841 table = CreateTable ();
842 Controls.Add (table);
844 if (!Page.IsPostBack)
845 currentMode = DefaultMode;
847 // Gets the current data item
849 IEnumerator e = dataSource.GetEnumerator ();
851 dataItem = e.Current;
855 // Main table creation
857 if (HeaderText.Length != 0 || headerTemplate != null) {
858 headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
859 InitializeRow (headerRow);
860 table.Rows.Add (headerRow);
863 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
864 topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
865 InitializePager (topPagerRow, dataSource);
866 table.Rows.Add (topPagerRow);
869 if (dataSourceCount > 0) {
870 DataControlRowState rstate = GetRowState ();
871 itemRow = CreateRow (0, DataControlRowType.DataRow, rstate);
872 InitializeRow (itemRow);
873 table.Rows.Add (itemRow);
876 if (CurrentMode == FormViewMode.Edit)
877 oldEditValues = new DataKey (new OrderedDictionary ());
878 key = new DataKey (new OrderedDictionary (), DataKeyNames);
881 itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
882 table.Rows.Add (itemRow);
883 InitializeRow (itemRow);
886 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
887 bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
888 InitializePager (bottomPagerRow, dataSource);
889 table.Rows.Add (bottomPagerRow);
892 if (FooterText.Length != 0 || footerTemplate != null) {
893 footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
894 InitializeRow (footerRow);
895 table.Rows.Add (footerRow);
898 return dataSource.DataSourceCount;
901 DataControlRowState GetRowState ()
903 DataControlRowState rstate = DataControlRowState.Normal;
904 if (CurrentMode == FormViewMode.Edit) rstate |= DataControlRowState.Edit;
905 else if (CurrentMode == FormViewMode.Insert) rstate |= DataControlRowState.Insert;
909 protected virtual void InitializePager (FormViewRow row, PagedDataSource dataSource)
911 TableCell cell = new TableCell ();
913 if (pagerTemplate != null)
914 pagerTemplate.InstantiateIn (cell);
916 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
918 row.Cells.Add (cell);
921 protected virtual void InitializeRow (FormViewRow row)
923 TableCell cell = new TableCell ();
925 if (row.RowType == DataControlRowType.DataRow)
927 if ((row.RowState & DataControlRowState.Edit) != 0) {
928 if (editItemTemplate != null)
929 editItemTemplate.InstantiateIn (cell);
930 } else if ((row.RowState & DataControlRowState.Insert) != 0) {
931 if (insertItemTemplate != null)
932 insertItemTemplate.InstantiateIn (cell);
933 } else if (itemTemplate != null)
934 itemTemplate.InstantiateIn (cell);
936 else if (row.RowType == DataControlRowType.EmptyDataRow)
938 if (emptyDataTemplate != null)
939 emptyDataTemplate.InstantiateIn (cell);
941 cell.Text = EmptyDataText;
943 else if (row.RowType == DataControlRowType.Footer)
945 if (footerTemplate != null)
946 footerTemplate.InstantiateIn (cell);
948 cell.Text = FooterText;
950 else if (row.RowType == DataControlRowType.Header)
952 if (headerTemplate != null)
953 headerTemplate.InstantiateIn (cell);
955 cell.Text = HeaderText;
957 row.Cells.Add (cell);
960 IOrderedDictionary CreateRowDataKey (object dataItem)
962 if (cachedKeyProperties == null) {
963 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
964 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
965 for (int n=0; n<DataKeyNames.Length; n++) {
966 PropertyDescriptor p = props [DataKeyNames[n]];
968 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
969 cachedKeyProperties [n] = p;
973 OrderedDictionary dic = new OrderedDictionary ();
974 foreach (PropertyDescriptor p in cachedKeyProperties)
975 dic [p.Name] = p.GetValue (dataItem);
979 IOrderedDictionary GetRowValues (bool includePrimaryKey)
981 OrderedDictionary dic = new OrderedDictionary ();
982 ExtractRowValues (dic, includePrimaryKey);
986 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeKeys)
988 DataControlRowState rowState = Row.RowState;
989 IBindableTemplate bt;
991 if ((rowState & DataControlRowState.Insert) != 0)
992 bt = insertItemTemplate as IBindableTemplate;
993 else if ((rowState & DataControlRowState.Edit) != 0)
994 bt = editItemTemplate as IBindableTemplate;
999 IOrderedDictionary values = bt.ExtractValues (Row.Cells [0]);
1000 foreach (DictionaryEntry e in values) {
1001 if (includeKeys || Array.IndexOf (DataKeyNames, e.Key) == -1)
1002 fieldValues [e.Key] = e.Value;
1007 protected override HtmlTextWriterTag TagKey {
1009 return HtmlTextWriterTag.Table;
1013 public sealed override void DataBind ()
1015 DataSourceView view = GetData ();
1016 if (AllowPaging && view.CanPage) {
1017 SelectArguments.StartRowIndex = PageIndex;
1018 SelectArguments.MaximumRows = 1;
1019 if (view.CanRetrieveTotalRowCount)
1020 SelectArguments.RetrieveTotalRowCount = true;
1023 cachedKeyProperties = null;
1026 if (dataSourceCount > 0) {
1027 if (CurrentMode == FormViewMode.Edit)
1028 oldEditValues = new DataKey (GetRowValues (true));
1029 key = new DataKey (CreateRowDataKey (dataItem), DataKeyNames);
1033 protected override void PerformDataBinding (IEnumerable data)
1035 base.PerformDataBinding (data);
1038 protected override void OnInit (EventArgs e)
1040 Page.RegisterRequiresControlState (this);
1044 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1046 base.OnDataSourceViewChanged (sender, e);
1050 protected override bool OnBubbleEvent (object source, EventArgs e)
1052 FormViewCommandEventArgs args = e as FormViewCommandEventArgs;
1054 OnItemCommand (args);
1055 ProcessEvent (args.CommandName, args.CommandArgument as string);
1057 return base.OnBubbleEvent (source, e);
1060 // This is prolly obsolete
1061 protected virtual void RaisePostBackEvent (string eventArgument)
1063 int i = eventArgument.IndexOf ('$');
1065 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1067 ProcessEvent (eventArgument, null);
1070 void ProcessEvent (string eventName, string param)
1081 newIndex = PageCount - 1;
1084 if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1087 if (PageIndex > 0) newIndex = PageIndex - 1;
1090 newIndex = int.Parse (param) - 1;
1093 ShowPage (newIndex);
1101 ShowPage (PageCount - 1);
1105 if (PageIndex < PageCount - 1)
1106 ShowPage (PageIndex + 1);
1111 ShowPage (PageIndex - 1);
1115 ChangeMode (FormViewMode.Edit);
1119 ChangeMode (FormViewMode.Insert);
1123 UpdateItem (param, true);
1140 void ShowPage (int newIndex)
1142 FormViewPageEventArgs args = new FormViewPageEventArgs (newIndex);
1143 OnPageIndexChanging (args);
1146 PageIndex = args.NewPageIndex;
1147 OnPageIndexChanged (EventArgs.Empty);
1151 public void ChangeMode (FormViewMode newMode)
1153 FormViewModeEventArgs args = new FormViewModeEventArgs (newMode, false);
1154 OnModeChanging (args);
1156 currentMode = args.NewMode;
1157 OnModeChanged (EventArgs.Empty);
1164 FormViewModeEventArgs args = new FormViewModeEventArgs (FormViewMode.ReadOnly, true);
1165 OnModeChanging (args);
1171 public virtual void UpdateItem (bool causesValidation)
1173 UpdateItem (null, causesValidation);
1176 void UpdateItem (string param, bool causesValidation)
1178 if (causesValidation)
1181 if (currentMode != FormViewMode.Edit) throw new NotSupportedException ();
1183 currentEditOldValues = oldEditValues.Values;
1184 currentEditRowKeys = DataKey.Values;
1185 currentEditNewValues = GetRowValues (false);
1187 FormViewUpdateEventArgs args = new FormViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1188 OnItemUpdating (args);
1190 DataSourceView view = GetData ();
1191 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1192 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1197 bool UpdateCallback (int recordsAffected, Exception exception)
1199 FormViewUpdatedEventArgs dargs = new FormViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1200 OnItemUpdated (dargs);
1202 if (!dargs.KeepInEditMode)
1205 return dargs.ExceptionHandled;
1208 public virtual void InsertItem (bool causesValidation)
1210 InsertItem (null, causesValidation);
1213 void InsertItem (string param, bool causesValidation)
1215 if (causesValidation)
1218 if (currentMode != FormViewMode.Insert) throw new NotSupportedException ();
1220 currentEditNewValues = GetRowValues (true);
1221 FormViewInsertEventArgs args = new FormViewInsertEventArgs (param, currentEditNewValues);
1222 OnItemInserting (args);
1224 DataSourceView view = GetData ();
1225 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1226 view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
1231 bool InsertCallback (int recordsAffected, Exception exception)
1233 FormViewInsertedEventArgs dargs = new FormViewInsertedEventArgs (recordsAffected, exception, currentEditNewValues);
1234 OnItemInserted (dargs);
1236 if (!dargs.KeepInInsertMode)
1239 return dargs.ExceptionHandled;
1242 public void DeleteItem ()
1244 currentEditRowKeys = DataKey.Values;
1245 currentEditNewValues = GetRowValues (true);
1247 FormViewDeleteEventArgs args = new FormViewDeleteEventArgs (PageIndex, currentEditRowKeys, currentEditNewValues);
1248 OnItemDeleting (args);
1251 if (PageIndex == PageCount - 1)
1256 DataSourceView view = GetData ();
1258 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1260 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1261 OnItemDeleted (dargs);
1266 bool DeleteCallback (int recordsAffected, Exception exception)
1268 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1269 OnItemDeleted (dargs);
1270 return dargs.ExceptionHandled;
1275 ChangeMode (DefaultMode);
1276 oldEditValues = new DataKey (new OrderedDictionary ());
1277 currentEditRowKeys = null;
1278 currentEditOldValues = null;
1279 currentEditNewValues = null;
1283 protected internal override void LoadControlState (object ob)
1285 if (ob == null) return;
1286 object[] state = (object[]) ob;
1287 base.LoadControlState (state[0]);
1288 pageIndex = (int) state[1];
1289 pageCount = (int) state[2];
1290 currentMode = (FormViewMode) state[3];
1293 protected internal override object SaveControlState ()
1295 object bstate = base.SaveControlState ();
1296 return new object[] {
1297 bstate, pageIndex, pageCount, currentMode
1301 protected override void TrackViewState()
1303 base.TrackViewState();
1304 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1305 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1306 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1307 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1308 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1309 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1310 if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
1311 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1312 if (key != null) ((IStateManager)key).TrackViewState();
1315 protected override object SaveViewState()
1317 object[] states = new object [14];
1318 states[0] = base.SaveViewState();
1319 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1320 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1321 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1322 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1323 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1324 states[8] = (insertRowStyle == null ? null : ((IStateManager)insertRowStyle).SaveViewState());
1325 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1326 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1327 states[11] = (key == null ? null : ((IStateManager)key).SaveViewState());
1328 states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1330 for (int i = states.Length - 1; i >= 0; i--) {
1331 if (states [i] != null)
1338 protected override void LoadViewState (object savedState)
1340 if (savedState == null) {
1341 base.LoadViewState (null);
1345 object [] states = (object []) savedState;
1347 base.LoadViewState (states[0]);
1348 EnsureChildControls ();
1350 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1351 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1352 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1353 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1354 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1355 if (states[8] != null) ((IStateManager)InsertRowStyle).LoadViewState (states[8]);
1356 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1357 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1358 if (states[11] != null && DataKey != null) ((IStateManager)DataKey).LoadViewState (states[11]);
1359 if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1362 protected override void Render (HtmlTextWriter writer)
1364 switch (GridLines) {
1365 case GridLines.Horizontal:
1366 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
1367 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1369 case GridLines.Vertical:
1370 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
1371 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1373 case GridLines.Both:
1374 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
1375 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1378 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
1382 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
1383 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
1384 table.RenderBeginTag (writer);
1386 foreach (FormViewRow row in table.Rows)
1388 switch (row.RowType) {
1389 case DataControlRowType.Header:
1390 if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1392 case DataControlRowType.Footer:
1393 if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1395 case DataControlRowType.Pager:
1396 if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1398 case DataControlRowType.EmptyDataRow:
1399 if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1402 if (rowStyle != null) rowStyle.AddAttributesToRender (writer, row);
1406 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1407 editRowStyle.AddAttributesToRender (writer, row);
1408 if ((row.RowState & DataControlRowState.Insert) != 0 && insertRowStyle != null)
1409 insertRowStyle.AddAttributesToRender (writer, row);
1411 row.RenderBeginTag (writer);
1413 for (int n=0; n<row.Cells.Count; n++)
1414 row.Cells[n].Render (writer);
1416 row.RenderEndTag (writer);
1418 table.RenderEndTag (writer);