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, " + Consts.AssemblySystem_Design, "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, INamingContainer
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, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
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, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
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 [Category ("Layout")]
592 [DefaultValueAttribute (HorizontalAlign.NotSet)]
593 public virtual HorizontalAlign HorizontalAlign {
595 object ob = ViewState ["HorizontalAlign"];
596 if (ob != null) return (HorizontalAlign) ob;
597 return HorizontalAlign.NotSet;
600 ViewState ["HorizontalAlign"] = value;
605 [DefaultValue (null)]
606 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
607 [PersistenceMode (PersistenceMode.InnerProperty)]
609 public ITemplate InsertItemTemplate {
610 get { return insertItemTemplate; }
611 set { insertItemTemplate = value; RequireBinding (); }
614 [WebCategoryAttribute ("Styles")]
615 [PersistenceMode (PersistenceMode.InnerProperty)]
616 [NotifyParentProperty (true)]
617 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
618 [DefaultValueAttribute (null)]
\r
619 public virtual TableItemStyle InsertRowStyle {
621 if (insertRowStyle == null) {
622 insertRowStyle = new TableItemStyle ();
623 if (IsTrackingViewState)
624 insertRowStyle.TrackViewState();
626 return insertRowStyle;
630 [DefaultValue (null)]
631 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
632 [PersistenceMode (PersistenceMode.InnerProperty)]
634 public ITemplate ItemTemplate {
635 get { return itemTemplate; }
636 set { itemTemplate = value; RequireBinding (); }
639 [BrowsableAttribute (false)]
640 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
641 public int PageCount {
643 if (pageCount != -1) return pageCount;
649 [WebCategoryAttribute ("Paging")]
650 [BindableAttribute (true, BindingDirection.OneWay)]
\r
651 [DefaultValueAttribute (0)]
652 public int PageIndex {
662 [WebCategoryAttribute ("Paging")]
663 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
664 [NotifyParentPropertyAttribute (true)]
665 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
666 public PagerSettings PagerSettings {
668 if (pagerSettings == null) {
669 pagerSettings = new PagerSettings (this);
670 if (IsTrackingViewState)
671 ((IStateManager)pagerSettings).TrackViewState ();
673 return pagerSettings;
677 [WebCategoryAttribute ("Styles")]
678 [PersistenceMode (PersistenceMode.InnerProperty)]
679 [NotifyParentProperty (true)]
680 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
681 public virtual TableItemStyle PagerStyle {
683 if (pagerStyle == null) {
684 pagerStyle = new TableItemStyle ();
685 if (IsTrackingViewState)
686 pagerStyle.TrackViewState();
693 [DefaultValue (null)]
694 /* DataControlPagerCell isnt specified in the docs */
695 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
696 [PersistenceMode (PersistenceMode.InnerProperty)]
698 public ITemplate PagerTemplate {
699 get { return pagerTemplate; }
700 set { pagerTemplate = value; RequireBinding (); }
703 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
704 [BrowsableAttribute (false)]
\r
705 public FormViewRow Row {
712 [WebCategoryAttribute ("Styles")]
713 [PersistenceMode (PersistenceMode.InnerProperty)]
714 [NotifyParentProperty (true)]
715 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
716 [DefaultValue (null)]
717 public virtual TableItemStyle RowStyle {
719 if (rowStyle == null) {
720 rowStyle = new TableItemStyle ();
721 if (IsTrackingViewState)
722 rowStyle.TrackViewState();
728 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
729 [BrowsableAttribute (false)]
730 public virtual object SelectedValue {
731 get { return DataKey.Value; }
734 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
735 [BrowsableAttribute (false)]
736 public virtual FormViewRow TopPagerRow {
743 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
744 [BrowsableAttribute (false)]
745 public object DataItem {
752 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
753 [BrowsableAttribute (false)]
754 public int DataItemCount {
755 get { return PageCount; }
758 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
759 [BrowsableAttribute (false)]
760 public int DataItemIndex {
761 get { return PageIndex; }
764 int IDataItemContainer.DisplayIndex {
765 get { return PageIndex; }
768 public virtual bool IsBindableType (Type type)
770 return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
773 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
775 return base.CreateDataSourceSelectArguments ();
778 protected virtual FormViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
780 FormViewRow row = new FormViewRow (rowIndex, rowType, rowState);
781 OnItemCreated (EventArgs.Empty);
785 void RequireBinding ()
788 RequiresDataBinding = true;
793 protected virtual Table CreateTable ()
795 Table table = new Table ();
796 table.Caption = Caption;
797 table.CaptionAlign = CaptionAlign;
798 table.CellPadding = CellPadding;
799 table.CellSpacing = CellSpacing;
800 table.HorizontalAlign = HorizontalAlign;
801 table.BackImageUrl = BackImageUrl;
806 protected override void EnsureDataBound ()
808 throw new NotImplementedException ();
812 protected override Style CreateControlStyle ()
814 throw new NotImplementedException ();
817 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
819 PagedDataSource dataSource;
822 DataSourceView view = GetData ();
823 dataSource = new PagedDataSource ();
824 dataSource.DataSource = data;
827 dataSource.AllowPaging = true;
828 dataSource.PageSize = 1;
829 dataSource.CurrentPageIndex = PageIndex;
831 dataSource.AllowServerPaging = true;
832 if (view.CanRetrieveTotalRowCount)
833 dataSource.VirtualCount = SelectArguments.TotalRowCount;
835 dataSource.DataSourceView = view;
836 dataSource.DataSourceSelectArguments = SelectArguments;
837 dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
842 pageCount = dataSource.PageCount;
846 dataSource = new PagedDataSource ();
847 dataSource.DataSource = data;
849 dataSource.AllowPaging = true;
850 dataSource.PageSize = 1;
851 dataSource.CurrentPageIndex = PageIndex;
855 bool showPager = AllowPaging && (PageCount > 1);
856 dataSourceCount = dataSource.Count;
859 table = CreateTable ();
860 Controls.Add (table);
862 if (!Page.IsPostBack)
863 currentMode = DefaultMode;
865 // Gets the current data item
867 IEnumerator e = dataSource.GetEnumerator ();
869 dataItem = e.Current;
873 // Main table creation
875 if (HeaderText.Length != 0 || headerTemplate != null) {
876 headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
877 InitializeRow (headerRow);
878 table.Rows.Add (headerRow);
881 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
882 topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
883 InitializePager (topPagerRow, dataSource);
884 table.Rows.Add (topPagerRow);
887 if (dataSourceCount > 0) {
888 DataControlRowState rstate = GetRowState ();
889 itemRow = CreateRow (0, DataControlRowType.DataRow, rstate);
890 InitializeRow (itemRow);
891 table.Rows.Add (itemRow);
894 if (CurrentMode == FormViewMode.Edit)
895 oldEditValues = new DataKey (new OrderedDictionary ());
896 key = new DataKey (new OrderedDictionary (), DataKeyNames);
899 itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
900 table.Rows.Add (itemRow);
901 InitializeRow (itemRow);
904 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
905 bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
906 InitializePager (bottomPagerRow, dataSource);
907 table.Rows.Add (bottomPagerRow);
910 if (FooterText.Length != 0 || footerTemplate != null) {
911 footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
912 InitializeRow (footerRow);
913 table.Rows.Add (footerRow);
919 return dataSource.DataSourceCount;
922 DataControlRowState GetRowState ()
924 DataControlRowState rstate = DataControlRowState.Normal;
925 if (CurrentMode == FormViewMode.Edit) rstate |= DataControlRowState.Edit;
926 else if (CurrentMode == FormViewMode.Insert) rstate |= DataControlRowState.Insert;
930 protected virtual void InitializePager (FormViewRow row, PagedDataSource dataSource)
932 TableCell cell = new TableCell ();
934 if (pagerTemplate != null)
935 pagerTemplate.InstantiateIn (cell);
937 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
939 row.Cells.Add (cell);
942 protected virtual void InitializeRow (FormViewRow row)
944 TableCell cell = new TableCell ();
946 if (row.RowType == DataControlRowType.DataRow)
948 if ((row.RowState & DataControlRowState.Edit) != 0) {
949 if (editItemTemplate != null)
950 editItemTemplate.InstantiateIn (cell);
951 } else if ((row.RowState & DataControlRowState.Insert) != 0) {
952 if (insertItemTemplate != null)
953 insertItemTemplate.InstantiateIn (cell);
954 } else if (itemTemplate != null)
955 itemTemplate.InstantiateIn (cell);
957 else if (row.RowType == DataControlRowType.EmptyDataRow)
959 if (emptyDataTemplate != null)
960 emptyDataTemplate.InstantiateIn (cell);
962 cell.Text = EmptyDataText;
964 else if (row.RowType == DataControlRowType.Footer)
966 if (footerTemplate != null)
967 footerTemplate.InstantiateIn (cell);
969 cell.Text = FooterText;
971 else if (row.RowType == DataControlRowType.Header)
973 if (headerTemplate != null)
974 headerTemplate.InstantiateIn (cell);
976 cell.Text = HeaderText;
978 row.Cells.Add (cell);
981 IOrderedDictionary CreateRowDataKey (object dataItem)
983 if (cachedKeyProperties == null) {
984 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
985 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
986 for (int n=0; n<DataKeyNames.Length; n++) {
987 PropertyDescriptor p = props [DataKeyNames[n]];
989 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
990 cachedKeyProperties [n] = p;
994 OrderedDictionary dic = new OrderedDictionary ();
995 foreach (PropertyDescriptor p in cachedKeyProperties)
996 dic [p.Name] = p.GetValue (dataItem);
1000 IOrderedDictionary GetRowValues (bool includePrimaryKey)
1002 OrderedDictionary dic = new OrderedDictionary ();
1003 ExtractRowValues (dic, includePrimaryKey);
1007 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeKeys)
1009 DataControlRowState rowState = Row.RowState;
1010 IBindableTemplate bt;
1012 if ((rowState & DataControlRowState.Insert) != 0)
1013 bt = insertItemTemplate as IBindableTemplate;
1014 else if ((rowState & DataControlRowState.Edit) != 0)
1015 bt = editItemTemplate as IBindableTemplate;
1020 IOrderedDictionary values = bt.ExtractValues (Row.Cells [0]);
1021 foreach (DictionaryEntry e in values) {
1022 if (includeKeys || Array.IndexOf (DataKeyNames, e.Key) == -1)
1023 fieldValues [e.Key] = e.Value;
1028 protected override HtmlTextWriterTag TagKey {
1030 return HtmlTextWriterTag.Table;
1034 public sealed override void DataBind ()
1036 DataSourceView view = GetData ();
1037 if (AllowPaging && view.CanPage) {
1038 SelectArguments.StartRowIndex = PageIndex;
1039 SelectArguments.MaximumRows = 1;
1040 if (view.CanRetrieveTotalRowCount)
1041 SelectArguments.RetrieveTotalRowCount = true;
1044 cachedKeyProperties = null;
1047 if (dataSourceCount > 0) {
1048 if (CurrentMode == FormViewMode.Edit)
1049 oldEditValues = new DataKey (GetRowValues (true));
1050 key = new DataKey (CreateRowDataKey (dataItem), DataKeyNames);
1054 protected internal override void PerformDataBinding (IEnumerable data)
1056 base.PerformDataBinding (data);
1060 protected internal virtual void PrepareControlHierarchy ()
1062 throw new NotImplementedException ();
1065 protected internal override void OnInit (EventArgs e)
1067 Page.RegisterRequiresControlState (this);
1071 protected override void OnDataSourceViewChanged (object sender, EventArgs e)
1073 base.OnDataSourceViewChanged (sender, e);
1077 protected override bool OnBubbleEvent (object source, EventArgs e)
1079 FormViewCommandEventArgs args = e as FormViewCommandEventArgs;
1081 OnItemCommand (args);
1082 ProcessEvent (args.CommandName, args.CommandArgument as string);
1084 return base.OnBubbleEvent (source, e);
1087 // This is prolly obsolete
1088 protected virtual void RaisePostBackEvent (string eventArgument)
1090 int i = eventArgument.IndexOf ('$');
1092 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1094 ProcessEvent (eventArgument, null);
1097 void ProcessEvent (string eventName, string param)
1101 case DataControlCommands.PageCommandName:
1104 case DataControlCommands.FirstPageCommandArgument:
1107 case DataControlCommands.LastPageCommandArgument:
1108 newIndex = PageCount - 1;
1110 case DataControlCommands.NextPageCommandArgument:
1111 if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1113 case DataControlCommands.PreviousPageCommandArgument:
1114 if (PageIndex > 0) newIndex = PageIndex - 1;
1117 newIndex = int.Parse (param) - 1;
1120 ShowPage (newIndex);
1123 case DataControlCommands.FirstPageCommandArgument:
1127 case DataControlCommands.LastPageCommandArgument:
1128 ShowPage (PageCount - 1);
1131 case DataControlCommands.NextPageCommandArgument:
1132 if (PageIndex < PageCount - 1)
1133 ShowPage (PageIndex + 1);
1136 case DataControlCommands.PreviousPageCommandArgument:
1138 ShowPage (PageIndex - 1);
1141 case DataControlCommands.EditCommandName:
1142 ChangeMode (FormViewMode.Edit);
1145 case DataControlCommands.NewCommandName:
1146 ChangeMode (FormViewMode.Insert);
1149 case DataControlCommands.UpdateCommandName:
1150 UpdateItem (param, true);
1153 case DataControlCommands.CancelCommandName:
1157 case DataControlCommands.DeleteCommandName:
1161 case DataControlCommands.InsertCommandName:
1167 void ShowPage (int newIndex)
1169 FormViewPageEventArgs args = new FormViewPageEventArgs (newIndex);
1170 OnPageIndexChanging (args);
1173 PageIndex = args.NewPageIndex;
1174 OnPageIndexChanged (EventArgs.Empty);
1178 public void ChangeMode (FormViewMode newMode)
1180 FormViewModeEventArgs args = new FormViewModeEventArgs (newMode, false);
1181 OnModeChanging (args);
1183 currentMode = args.NewMode;
1184 OnModeChanged (EventArgs.Empty);
1191 FormViewModeEventArgs args = new FormViewModeEventArgs (FormViewMode.ReadOnly, true);
1192 OnModeChanging (args);
1198 public virtual void UpdateItem (bool causesValidation)
1200 UpdateItem (null, causesValidation);
1203 void UpdateItem (string param, bool causesValidation)
1205 if (causesValidation)
1208 if (currentMode != FormViewMode.Edit) throw new NotSupportedException ();
1210 currentEditOldValues = oldEditValues.Values;
1211 currentEditRowKeys = DataKey.Values;
1212 currentEditNewValues = GetRowValues (false);
1214 FormViewUpdateEventArgs args = new FormViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1215 OnItemUpdating (args);
1217 DataSourceView view = GetData ();
1218 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1219 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1224 bool UpdateCallback (int recordsAffected, Exception exception)
1226 FormViewUpdatedEventArgs dargs = new FormViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1227 OnItemUpdated (dargs);
1229 if (!dargs.KeepInEditMode)
1232 return dargs.ExceptionHandled;
1235 public virtual void InsertItem (bool causesValidation)
1237 InsertItem (null, causesValidation);
1240 void InsertItem (string param, bool causesValidation)
1242 if (causesValidation)
1245 if (currentMode != FormViewMode.Insert) throw new NotSupportedException ();
1247 currentEditNewValues = GetRowValues (true);
1248 FormViewInsertEventArgs args = new FormViewInsertEventArgs (param, currentEditNewValues);
1249 OnItemInserting (args);
1251 DataSourceView view = GetData ();
1252 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1253 view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
1258 bool InsertCallback (int recordsAffected, Exception exception)
1260 FormViewInsertedEventArgs dargs = new FormViewInsertedEventArgs (recordsAffected, exception, currentEditNewValues);
1261 OnItemInserted (dargs);
1263 if (!dargs.KeepInInsertMode)
1266 return dargs.ExceptionHandled;
1269 public void DeleteItem ()
1271 currentEditRowKeys = DataKey.Values;
1272 currentEditNewValues = GetRowValues (true);
1274 FormViewDeleteEventArgs args = new FormViewDeleteEventArgs (PageIndex, currentEditRowKeys, currentEditNewValues);
1275 OnItemDeleting (args);
1278 if (PageIndex == PageCount - 1)
1283 DataSourceView view = GetData ();
1285 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1287 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1288 OnItemDeleted (dargs);
1293 bool DeleteCallback (int recordsAffected, Exception exception)
1295 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1296 OnItemDeleted (dargs);
1297 return dargs.ExceptionHandled;
1302 ChangeMode (DefaultMode);
1303 oldEditValues = new DataKey (new OrderedDictionary ());
1304 currentEditRowKeys = null;
1305 currentEditOldValues = null;
1306 currentEditNewValues = null;
1310 protected internal override void LoadControlState (object ob)
1312 if (ob == null) return;
1313 object[] state = (object[]) ob;
1314 base.LoadControlState (state[0]);
1315 pageIndex = (int) state[1];
1316 pageCount = (int) state[2];
1317 currentMode = (FormViewMode) state[3];
1320 protected internal override object SaveControlState ()
1322 object bstate = base.SaveControlState ();
1323 return new object[] {
1324 bstate, pageIndex, pageCount, currentMode
1328 protected override void TrackViewState()
1330 base.TrackViewState();
1331 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1332 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1333 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1334 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1335 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1336 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1337 if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
1338 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1339 if (key != null) ((IStateManager)key).TrackViewState();
1342 protected override object SaveViewState()
1344 object[] states = new object [14];
1345 states[0] = base.SaveViewState();
1346 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1347 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1348 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1349 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1350 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1351 states[8] = (insertRowStyle == null ? null : ((IStateManager)insertRowStyle).SaveViewState());
1352 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1353 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1354 states[11] = (key == null ? null : ((IStateManager)key).SaveViewState());
1355 states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1357 for (int i = states.Length - 1; i >= 0; i--) {
1358 if (states [i] != null)
1365 protected override void LoadViewState (object savedState)
1367 if (savedState == null) {
1368 base.LoadViewState (null);
1372 object [] states = (object []) savedState;
1374 base.LoadViewState (states[0]);
1375 EnsureChildControls ();
1377 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1378 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1379 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1380 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1381 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1382 if (states[8] != null) ((IStateManager)InsertRowStyle).LoadViewState (states[8]);
1383 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1384 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1385 if (states[11] != null && DataKey != null) ((IStateManager)DataKey).LoadViewState (states[11]);
1386 if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1389 protected internal override void Render (HtmlTextWriter writer)
1391 switch (GridLines) {
1392 case GridLines.Horizontal:
1393 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
1394 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1396 case GridLines.Vertical:
1397 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
1398 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1400 case GridLines.Both:
1401 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
1402 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1405 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
1409 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
1410 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
1411 table.RenderBeginTag (writer);
1413 foreach (FormViewRow row in table.Rows)
1415 switch (row.RowType) {
1416 case DataControlRowType.Header:
1417 if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1419 case DataControlRowType.Footer:
1420 if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1422 case DataControlRowType.Pager:
1423 if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1425 case DataControlRowType.EmptyDataRow:
1426 if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1429 if (rowStyle != null) rowStyle.AddAttributesToRender (writer, row);
1433 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1434 editRowStyle.AddAttributesToRender (writer, row);
1435 if ((row.RowState & DataControlRowState.Insert) != 0 && insertRowStyle != null)
1436 insertRowStyle.AddAttributesToRender (writer, row);
1438 row.RenderBeginTag (writer);
1440 for (int n=0; n<row.Cells.Count; n++)
1441 row.Cells[n].Render (writer);
1443 row.RenderEndTag (writer);
1445 table.RenderEndTag (writer);