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 [SupportsEventValidation]
44 [DesignerAttribute ("System.Web.UI.Design.WebControls.FormViewDesigner, " + Consts.AssemblySystem_Design, "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 FormView: CompositeDataBoundControl, IDataItemContainer, INamingContainer, IPostBackEventHandler, IPostBackContainer
54 FormViewRow headerRow;
55 FormViewRow footerRow;
56 FormViewRow bottomPagerRow;
57 FormViewRow topPagerRow;
60 IOrderedDictionary currentEditRowKeys;
61 IOrderedDictionary currentEditNewValues;
62 IOrderedDictionary currentEditOldValues;
64 ITemplate pagerTemplate;
65 ITemplate emptyDataTemplate;
66 ITemplate headerTemplate;
67 ITemplate footerTemplate;
68 ITemplate editItemTemplate;
69 ITemplate insertItemTemplate;
70 ITemplate itemTemplate;
72 PropertyDescriptor[] cachedKeyProperties;
73 readonly string[] emptyKeys = new string[0];
76 PagerSettings pagerSettings;
78 TableItemStyle editRowStyle;
79 TableItemStyle insertRowStyle;
80 TableItemStyle emptyDataRowStyle;
81 TableItemStyle footerStyle;
82 TableItemStyle headerStyle;
83 TableItemStyle pagerStyle;
84 TableItemStyle rowStyle;
87 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 virtual 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 virtual 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)]
368 [BrowsableAttribute (false)]
369 public FormViewMode CurrentMode {
375 [DefaultValueAttribute (FormViewMode.ReadOnly)]
376 [WebCategoryAttribute ("Behavior")]
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 virtual 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)]
429 public 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)]
445 public 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 virtual 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 virtual ITemplate FooterTemplate {
494 get { return footerTemplate; }
495 set { footerTemplate = value; RequireBinding (); }
498 [LocalizableAttribute (true)]
499 [WebCategoryAttribute ("Appearance")]
500 [DefaultValueAttribute ("")]
501 public virtual 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 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 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 virtual ITemplate HeaderTemplate {
572 get { return headerTemplate; }
573 set { headerTemplate = value; RequireBinding (); }
576 [LocalizableAttribute (true)]
577 [WebCategoryAttribute ("Appearance")]
578 [DefaultValueAttribute ("")]
579 public virtual 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 virtual 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)]
619 public 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 virtual ITemplate ItemTemplate {
635 get { return itemTemplate; }
636 set { itemTemplate = value; RequireBinding (); }
639 [BrowsableAttribute (false)]
640 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
641 public virtual int PageCount {
643 if (pageCount != -1) return pageCount;
649 [WebCategoryAttribute ("Paging")]
650 [BindableAttribute (true, BindingDirection.OneWay)]
651 [DefaultValueAttribute (0)]
652 public virtual int PageIndex {
657 if (pageIndex == value)
664 [WebCategoryAttribute ("Paging")]
665 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
666 [NotifyParentPropertyAttribute (true)]
667 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
668 public virtual PagerSettings PagerSettings {
670 if (pagerSettings == null) {
671 pagerSettings = new PagerSettings (this);
672 if (IsTrackingViewState)
673 ((IStateManager)pagerSettings).TrackViewState ();
675 return pagerSettings;
679 [WebCategoryAttribute ("Styles")]
680 [PersistenceMode (PersistenceMode.InnerProperty)]
681 [NotifyParentProperty (true)]
682 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
683 public TableItemStyle PagerStyle {
685 if (pagerStyle == null) {
686 pagerStyle = new TableItemStyle ();
687 if (IsTrackingViewState)
688 pagerStyle.TrackViewState();
695 [DefaultValue (null)]
696 /* DataControlPagerCell isnt specified in the docs */
697 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
698 [PersistenceMode (PersistenceMode.InnerProperty)]
700 public virtual ITemplate PagerTemplate {
701 get { return pagerTemplate; }
702 set { pagerTemplate = value; RequireBinding (); }
705 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
706 [BrowsableAttribute (false)]
707 public virtual FormViewRow Row {
714 [WebCategoryAttribute ("Styles")]
715 [PersistenceMode (PersistenceMode.InnerProperty)]
716 [NotifyParentProperty (true)]
717 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
718 [DefaultValue (null)]
719 public TableItemStyle RowStyle {
721 if (rowStyle == null) {
722 rowStyle = new TableItemStyle ();
723 if (IsTrackingViewState)
724 rowStyle.TrackViewState();
730 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
731 [BrowsableAttribute (false)]
732 public object SelectedValue {
733 get { return DataKey.Value; }
736 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
737 [BrowsableAttribute (false)]
738 public virtual FormViewRow TopPagerRow {
745 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
746 [BrowsableAttribute (false)]
747 public virtual object DataItem {
754 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
755 [BrowsableAttribute (false)]
756 public int DataItemCount {
757 get { return PageCount; }
760 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
761 [BrowsableAttribute (false)]
762 public virtual int DataItemIndex {
763 get { return PageIndex; }
766 int IDataItemContainer.DisplayIndex {
767 get { return PageIndex; }
770 public virtual bool IsBindableType (Type type)
772 return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
775 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
777 return base.CreateDataSourceSelectArguments ();
780 protected virtual FormViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
782 FormViewRow row = new FormViewRow (rowIndex, rowType, rowState);
783 OnItemCreated (EventArgs.Empty);
787 void RequireBinding ()
791 RequiresDataBinding = true;
795 protected virtual Table CreateTable ()
797 Table table = new Table ();
798 table.Caption = Caption;
799 table.CaptionAlign = CaptionAlign;
800 table.CellPadding = CellPadding;
801 table.CellSpacing = CellSpacing;
802 table.HorizontalAlign = HorizontalAlign;
803 table.BackImageUrl = BackImageUrl;
808 protected override void EnsureDataBound ()
810 base.EnsureDataBound ();
814 protected override Style CreateControlStyle ()
816 return base.CreateControlStyle ();
819 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
821 PagedDataSource dataSource = new PagedDataSource ();
822 dataSource.DataSource = data;
823 dataSource.AllowPaging = AllowPaging;
824 dataSource.PageSize = 1;
825 dataSource.CurrentPageIndex = PageIndex;
828 DataSourceView view = GetData ();
830 dataSource.AllowServerPaging = true;
831 if (view.CanRetrieveTotalRowCount)
832 dataSource.VirtualCount = SelectArguments.TotalRowCount;
834 dataSource.DataSourceView = view;
835 dataSource.DataSourceSelectArguments = SelectArguments;
836 dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
841 pageCount = dataSource.DataSourceCount;
842 bool showPager = AllowPaging && (dataSource.Count > 1);
845 table = CreateTable ();
846 Controls.Add (table);
848 if (!Page.IsPostBack)
849 currentMode = DefaultMode;
851 // Gets the current data item
853 IEnumerator e = dataSource.GetEnumerator ();
858 dataItem = e.Current;
861 for (int page = 0; e.MoveNext (); page++ )
862 if (page == PageIndex)
863 dataItem = e.Current;
865 // Main table creation
867 if (HeaderText.Length != 0 || headerTemplate != null) {
868 headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
869 InitializeRow (headerRow);
870 table.Rows.Add (headerRow);
873 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
874 topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
875 InitializePager (topPagerRow, dataSource);
876 table.Rows.Add (topPagerRow);
880 DataControlRowState rstate = GetRowState ();
881 itemRow = CreateRow (0, DataControlRowType.DataRow, rstate);
882 InitializeRow (itemRow);
883 table.Rows.Add (itemRow);
886 if (CurrentMode == FormViewMode.Edit)
887 oldEditValues = new DataKey (new OrderedDictionary ());
888 key = new DataKey (new OrderedDictionary (), DataKeyNames);
891 switch (CurrentMode) {
892 case FormViewMode.Edit:
893 itemRow = CreateRow (-1, DataControlRowType.DataRow, DataControlRowState.Edit);
895 case FormViewMode.Insert:
896 itemRow = CreateRow (-1, DataControlRowType.DataRow, DataControlRowState.Insert);
899 itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
902 table.Rows.Add (itemRow);
903 InitializeRow (itemRow);
906 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
907 bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
908 InitializePager (bottomPagerRow, dataSource);
909 table.Rows.Add (bottomPagerRow);
912 if (FooterText.Length != 0 || footerTemplate != null) {
913 footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
914 InitializeRow (footerRow);
915 table.Rows.Add (footerRow);
921 return dataSource.DataSourceCount;
924 DataControlRowState GetRowState ()
926 DataControlRowState rstate = DataControlRowState.Normal;
927 if (CurrentMode == FormViewMode.Edit) rstate |= DataControlRowState.Edit;
928 else if (CurrentMode == FormViewMode.Insert) rstate |= DataControlRowState.Insert;
932 protected virtual void InitializePager (FormViewRow row, PagedDataSource dataSource)
934 TableCell cell = new TableCell ();
936 if (pagerTemplate != null)
937 pagerTemplate.InstantiateIn (cell);
939 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
941 row.Cells.Add (cell);
944 protected virtual void InitializeRow (FormViewRow row)
946 TableCell cell = new TableCell ();
948 if (row.RowType == DataControlRowType.DataRow)
950 if ((row.RowState & DataControlRowState.Edit) != 0) {
951 if (editItemTemplate != null)
952 editItemTemplate.InstantiateIn (cell);
953 } else if ((row.RowState & DataControlRowState.Insert) != 0) {
954 if (insertItemTemplate != null)
955 insertItemTemplate.InstantiateIn (cell);
956 } else if (itemTemplate != null)
957 itemTemplate.InstantiateIn (cell);
959 else if (row.RowType == DataControlRowType.EmptyDataRow)
961 if (emptyDataTemplate != null)
962 emptyDataTemplate.InstantiateIn (cell);
964 cell.Text = EmptyDataText;
966 else if (row.RowType == DataControlRowType.Footer)
968 if (footerTemplate != null)
969 footerTemplate.InstantiateIn (cell);
971 cell.Text = FooterText;
973 else if (row.RowType == DataControlRowType.Header)
975 if (headerTemplate != null)
976 headerTemplate.InstantiateIn (cell);
978 cell.Text = HeaderText;
980 row.Cells.Add (cell);
983 IOrderedDictionary CreateRowDataKey (object dataItem)
985 if (cachedKeyProperties == null) {
986 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
987 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
988 for (int n=0; n<DataKeyNames.Length; n++) {
989 PropertyDescriptor p = props [DataKeyNames[n]];
991 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
992 cachedKeyProperties [n] = p;
996 OrderedDictionary dic = new OrderedDictionary ();
997 foreach (PropertyDescriptor p in cachedKeyProperties)
998 dic [p.Name] = p.GetValue (dataItem);
1002 IOrderedDictionary GetRowValues (bool includePrimaryKey)
1004 OrderedDictionary dic = new OrderedDictionary ();
1005 ExtractRowValues (dic, includePrimaryKey);
1009 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeKeys)
1011 DataControlRowState rowState = Row.RowState;
1012 IBindableTemplate bt;
1014 if ((rowState & DataControlRowState.Insert) != 0)
1015 bt = insertItemTemplate as IBindableTemplate;
1016 else if ((rowState & DataControlRowState.Edit) != 0)
1017 bt = editItemTemplate as IBindableTemplate;
1022 IOrderedDictionary values = bt.ExtractValues (Row.Cells [0]);
1023 foreach (DictionaryEntry e in values) {
1024 if (includeKeys || Array.IndexOf (DataKeyNames, e.Key) == -1)
1025 fieldValues [e.Key] = e.Value;
1030 protected override HtmlTextWriterTag TagKey {
1032 return HtmlTextWriterTag.Table;
1036 public sealed override void DataBind ()
1038 DataSourceView view = GetData ();
1039 if (AllowPaging && view.CanPage) {
1040 SelectArguments.StartRowIndex = PageIndex;
1041 SelectArguments.MaximumRows = 1;
1042 if (view.CanRetrieveTotalRowCount)
1043 SelectArguments.RetrieveTotalRowCount = true;
1046 cachedKeyProperties = null;
1049 if (pageCount > 0) {
1050 if (CurrentMode == FormViewMode.Edit)
1051 oldEditValues = new DataKey (GetRowValues (true));
1052 key = new DataKey (CreateRowDataKey (dataItem), DataKeyNames);
1056 protected internal override void PerformDataBinding (IEnumerable data)
1058 base.PerformDataBinding (data);
1062 protected internal virtual void PrepareControlHierarchy ()
1064 throw new NotImplementedException ();
1067 protected internal override void OnInit (EventArgs e)
1069 Page.RegisterRequiresControlState (this);
1073 protected override bool OnBubbleEvent (object source, EventArgs e)
1075 FormViewCommandEventArgs args = e as FormViewCommandEventArgs;
1077 OnItemCommand (args);
1078 ProcessEvent (args.CommandName, args.CommandArgument as string);
1080 return base.OnBubbleEvent (source, e);
1083 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
1085 RaisePostBackEvent (eventArgument);
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);
1172 newIndex = args.NewPageIndex;
1173 if (newIndex < 0 || newIndex >= PageCount)
1176 PageIndex = newIndex;
1177 OnPageIndexChanged (EventArgs.Empty);
1181 public void ChangeMode (FormViewMode newMode)
1183 FormViewModeEventArgs args = new FormViewModeEventArgs (newMode, false);
1184 OnModeChanging (args);
1186 currentMode = args.NewMode;
1187 OnModeChanged (EventArgs.Empty);
1194 FormViewModeEventArgs args = new FormViewModeEventArgs (FormViewMode.ReadOnly, true);
1195 OnModeChanging (args);
1201 public virtual void UpdateItem (bool causesValidation)
1203 UpdateItem (null, causesValidation);
1206 void UpdateItem (string param, bool causesValidation)
1208 if (causesValidation)
1211 if (currentMode != FormViewMode.Edit) throw new NotSupportedException ();
1213 currentEditOldValues = oldEditValues.Values;
1214 currentEditRowKeys = DataKey.Values;
1215 currentEditNewValues = GetRowValues (false);
1217 FormViewUpdateEventArgs args = new FormViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1218 OnItemUpdating (args);
1220 DataSourceView view = GetData ();
1221 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1222 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1227 bool UpdateCallback (int recordsAffected, Exception exception)
1229 FormViewUpdatedEventArgs dargs = new FormViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1230 OnItemUpdated (dargs);
1232 if (!dargs.KeepInEditMode)
1235 return dargs.ExceptionHandled;
1238 public virtual void InsertItem (bool causesValidation)
1240 InsertItem (null, causesValidation);
1243 void InsertItem (string param, bool causesValidation)
1245 if (causesValidation)
1248 if (currentMode != FormViewMode.Insert) throw new NotSupportedException ();
1250 currentEditNewValues = GetRowValues (true);
1251 FormViewInsertEventArgs args = new FormViewInsertEventArgs (param, currentEditNewValues);
1252 OnItemInserting (args);
1254 DataSourceView view = GetData ();
1255 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1256 view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
1261 bool InsertCallback (int recordsAffected, Exception exception)
1263 FormViewInsertedEventArgs dargs = new FormViewInsertedEventArgs (recordsAffected, exception, currentEditNewValues);
1264 OnItemInserted (dargs);
1266 if (!dargs.KeepInInsertMode)
1269 return dargs.ExceptionHandled;
1272 public virtual void DeleteItem ()
1274 currentEditRowKeys = DataKey.Values;
1275 currentEditNewValues = GetRowValues (true);
1277 FormViewDeleteEventArgs args = new FormViewDeleteEventArgs (PageIndex, currentEditRowKeys, currentEditNewValues);
1278 OnItemDeleting (args);
1281 if (PageIndex == PageCount - 1)
1286 DataSourceView view = GetData ();
1288 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1290 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1291 OnItemDeleted (dargs);
1296 bool DeleteCallback (int recordsAffected, Exception exception)
1298 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1299 OnItemDeleted (dargs);
1300 return dargs.ExceptionHandled;
1305 ChangeMode (DefaultMode);
1306 oldEditValues = new DataKey (new OrderedDictionary ());
1307 currentEditRowKeys = null;
1308 currentEditOldValues = null;
1309 currentEditNewValues = null;
1313 protected internal override void LoadControlState (object ob)
1315 if (ob == null) return;
1316 object[] state = (object[]) ob;
1317 base.LoadControlState (state[0]);
1318 pageIndex = (int) state[1];
1319 pageCount = (int) state[2];
1320 currentMode = (FormViewMode) state[3];
1323 protected internal override object SaveControlState ()
1325 object bstate = base.SaveControlState ();
1326 return new object[] {
1327 bstate, pageIndex, pageCount, currentMode
1331 protected override void TrackViewState()
1333 base.TrackViewState();
1334 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1335 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1336 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1337 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1338 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1339 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1340 if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
1341 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1342 if (key != null) ((IStateManager)key).TrackViewState();
1345 protected override object SaveViewState()
1347 object[] states = new object [14];
1348 states[0] = base.SaveViewState();
1349 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1350 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1351 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1352 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1353 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1354 states[8] = (insertRowStyle == null ? null : ((IStateManager)insertRowStyle).SaveViewState());
1355 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1356 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1357 states[11] = (key == null ? null : ((IStateManager)key).SaveViewState());
1358 states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1360 for (int i = states.Length - 1; i >= 0; i--) {
1361 if (states [i] != null)
1368 protected override void LoadViewState (object savedState)
1370 if (savedState == null) {
1371 base.LoadViewState (null);
1375 object [] states = (object []) savedState;
1377 base.LoadViewState (states[0]);
1378 EnsureChildControls ();
1380 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1381 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1382 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1383 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1384 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1385 if (states[8] != null) ((IStateManager)InsertRowStyle).LoadViewState (states[8]);
1386 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1387 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1388 if (states[11] != null && DataKey != null) ((IStateManager)DataKey).LoadViewState (states[11]);
1389 if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1392 protected internal override void Render (HtmlTextWriter writer)
1394 switch (GridLines) {
1395 case GridLines.Horizontal:
1396 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
1397 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1399 case GridLines.Vertical:
1400 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
1401 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1403 case GridLines.Both:
1404 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
1405 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1408 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
1412 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
1413 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
1414 if (!string.IsNullOrEmpty (ControlStyle.CssClass))
1415 writer.AddAttribute (HtmlTextWriterAttribute.Class, ControlStyle.CssClass);
1416 table.RenderBeginTag (writer);
1418 foreach (FormViewRow row in table.Rows)
1420 switch (row.RowType) {
1421 case DataControlRowType.Header:
1422 if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1424 case DataControlRowType.Footer:
1425 if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1427 case DataControlRowType.Pager:
1428 if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1430 case DataControlRowType.EmptyDataRow:
1431 if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1434 if (rowStyle != null) rowStyle.AddAttributesToRender (writer, row);
1438 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1439 editRowStyle.AddAttributesToRender (writer, row);
1440 if ((row.RowState & DataControlRowState.Insert) != 0 && insertRowStyle != null)
1441 insertRowStyle.AddAttributesToRender (writer, row);
1443 row.RenderBeginTag (writer);
1445 for (int n=0; n<row.Cells.Count; n++)
1446 row.Cells[n].Render (writer);
1448 row.RenderEndTag (writer);
1450 table.RenderEndTag (writer);
1454 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
1456 throw new NotImplementedException ();