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;
86 IOrderedDictionary _keyTable;
88 DataKey oldEditValues;
90 private static readonly object PageIndexChangedEvent = new object();
91 private static readonly object PageIndexChangingEvent = new object();
92 private static readonly object ItemCommandEvent = new object();
93 private static readonly object ItemCreatedEvent = new object();
94 private static readonly object ItemDeletedEvent = new object();
95 private static readonly object ItemDeletingEvent = new object();
96 private static readonly object ItemInsertedEvent = new object();
97 private static readonly object ItemInsertingEvent = new object();
98 private static readonly object ModeChangingEvent = new object();
99 private static readonly object ModeChangedEvent = new object();
100 private static readonly object ItemUpdatedEvent = new object();
101 private static readonly object ItemUpdatingEvent = new object();
105 FormViewMode currentMode = FormViewMode.ReadOnly;
109 public event EventHandler PageIndexChanged {
110 add { Events.AddHandler (PageIndexChangedEvent, value); }
111 remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
114 public event FormViewPageEventHandler PageIndexChanging {
115 add { Events.AddHandler (PageIndexChangingEvent, value); }
116 remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
119 public event FormViewCommandEventHandler ItemCommand {
120 add { Events.AddHandler (ItemCommandEvent, value); }
121 remove { Events.RemoveHandler (ItemCommandEvent, value); }
124 public event EventHandler ItemCreated {
125 add { Events.AddHandler (ItemCreatedEvent, value); }
126 remove { Events.RemoveHandler (ItemCreatedEvent, value); }
129 public event FormViewDeletedEventHandler ItemDeleted {
130 add { Events.AddHandler (ItemDeletedEvent, value); }
131 remove { Events.RemoveHandler (ItemDeletedEvent, value); }
134 public event FormViewDeleteEventHandler ItemDeleting {
135 add { Events.AddHandler (ItemDeletingEvent, value); }
136 remove { Events.RemoveHandler (ItemDeletingEvent, value); }
139 public event FormViewInsertedEventHandler ItemInserted {
140 add { Events.AddHandler (ItemInsertedEvent, value); }
141 remove { Events.RemoveHandler (ItemInsertedEvent, value); }
144 public event FormViewInsertEventHandler ItemInserting {
145 add { Events.AddHandler (ItemInsertingEvent, value); }
146 remove { Events.RemoveHandler (ItemInsertingEvent, value); }
149 public event FormViewModeEventHandler ModeChanging {
150 add { Events.AddHandler (ModeChangingEvent, value); }
151 remove { Events.RemoveHandler (ModeChangingEvent, value); }
154 public event EventHandler ModeChanged {
155 add { Events.AddHandler (ModeChangedEvent, value); }
156 remove { Events.RemoveHandler (ModeChangedEvent, value); }
159 public event FormViewUpdatedEventHandler ItemUpdated {
160 add { Events.AddHandler (ItemUpdatedEvent, value); }
161 remove { Events.RemoveHandler (ItemUpdatedEvent, value); }
164 public event FormViewUpdateEventHandler ItemUpdating {
165 add { Events.AddHandler (ItemUpdatingEvent, value); }
166 remove { Events.RemoveHandler (ItemUpdatingEvent, value); }
169 protected virtual void OnPageIndexChanged (EventArgs e)
171 if (Events != null) {
172 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
173 if (eh != null) eh (this, e);
177 protected virtual void OnPageIndexChanging (FormViewPageEventArgs e)
179 if (Events != null) {
180 FormViewPageEventHandler eh = (FormViewPageEventHandler) Events [PageIndexChangingEvent];
181 if (eh != null) eh (this, e);
185 protected virtual void OnItemCommand (FormViewCommandEventArgs e)
187 if (Events != null) {
188 FormViewCommandEventHandler eh = (FormViewCommandEventHandler) Events [ItemCommandEvent];
189 if (eh != null) eh (this, e);
193 protected virtual void OnItemCreated (EventArgs e)
195 if (Events != null) {
196 EventHandler eh = (EventHandler) Events [ItemCreatedEvent];
197 if (eh != null) eh (this, e);
201 protected virtual void OnItemDeleted (FormViewDeletedEventArgs e)
203 if (Events != null) {
204 FormViewDeletedEventHandler eh = (FormViewDeletedEventHandler) Events [ItemDeletedEvent];
205 if (eh != null) eh (this, e);
209 protected virtual void OnItemInserted (FormViewInsertedEventArgs e)
211 if (Events != null) {
212 FormViewInsertedEventHandler eh = (FormViewInsertedEventHandler) Events [ItemInsertedEvent];
213 if (eh != null) eh (this, e);
217 protected virtual void OnItemInserting (FormViewInsertEventArgs e)
219 if (Events != null) {
220 FormViewInsertEventHandler eh = (FormViewInsertEventHandler) Events [ItemInsertingEvent];
221 if (eh != null) eh (this, e);
225 protected virtual void OnItemDeleting (FormViewDeleteEventArgs e)
227 if (Events != null) {
228 FormViewDeleteEventHandler eh = (FormViewDeleteEventHandler) Events [ItemDeletingEvent];
229 if (eh != null) eh (this, e);
233 protected virtual void OnModeChanged (EventArgs e)
235 if (Events != null) {
236 EventHandler eh = (EventHandler) Events [ModeChangedEvent];
237 if (eh != null) eh (this, e);
241 protected virtual void OnModeChanging (FormViewModeEventArgs e)
243 if (Events != null) {
244 FormViewModeEventHandler eh = (FormViewModeEventHandler) Events [ModeChangingEvent];
245 if (eh != null) eh (this, e);
249 protected virtual void OnItemUpdated (FormViewUpdatedEventArgs e)
251 if (Events != null) {
252 FormViewUpdatedEventHandler eh = (FormViewUpdatedEventHandler) Events [ItemUpdatedEvent];
253 if (eh != null) eh (this, e);
257 protected virtual void OnItemUpdating (FormViewUpdateEventArgs e)
259 if (Events != null) {
260 FormViewUpdateEventHandler eh = (FormViewUpdateEventHandler) Events [ItemUpdatingEvent];
261 if (eh != null) eh (this, e);
266 [WebCategoryAttribute ("Paging")]
267 [DefaultValueAttribute (false)]
268 public virtual bool AllowPaging {
270 object ob = ViewState ["AllowPaging"];
271 if (ob != null) return (bool) ob;
275 ViewState ["AllowPaging"] = value;
280 [UrlPropertyAttribute]
281 [WebCategoryAttribute ("Appearance")]
282 [DefaultValueAttribute ("")]
283 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
284 public virtual string BackImageUrl {
286 if (ControlStyleCreated)
287 return ((TableStyle) ControlStyle).BackImageUrl;
291 ((TableStyle) ControlStyle).BackImageUrl = value;
295 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
296 [BrowsableAttribute (false)]
297 public virtual FormViewRow BottomPagerRow {
299 EnsureChildControls ();
300 return bottomPagerRow;
304 [WebCategoryAttribute ("Accessibility")]
305 [DefaultValueAttribute ("")]
306 [LocalizableAttribute (true)]
307 public virtual string Caption {
309 object ob = ViewState ["Caption"];
310 if (ob != null) return (string) ob;
314 ViewState ["Caption"] = value;
319 [WebCategoryAttribute ("Accessibility")]
320 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
321 public virtual TableCaptionAlign CaptionAlign
324 object o = ViewState ["CaptionAlign"];
325 if(o != null) return (TableCaptionAlign) o;
326 return TableCaptionAlign.NotSet;
329 ViewState ["CaptionAlign"] = value;
334 [WebCategoryAttribute ("Layout")]
335 [DefaultValueAttribute (-1)]
336 public virtual int CellPadding
339 if (ControlStyleCreated)
340 return ((TableStyle) ControlStyle).CellPadding;
344 ((TableStyle) ControlStyle).CellPadding = value;
348 [WebCategoryAttribute ("Layout")]
349 [DefaultValueAttribute (0)]
350 public virtual int CellSpacing
353 if (ControlStyleCreated)
354 return ((TableStyle) ControlStyle).CellSpacing;
358 ((TableStyle) ControlStyle).CellSpacing = value;
362 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
363 [BrowsableAttribute (false)]
364 public FormViewMode CurrentMode {
366 return hasCurrentMode ? currentMode : DefaultMode;
369 hasCurrentMode = true;
374 FormViewMode defaultMode;
376 [DefaultValueAttribute (FormViewMode.ReadOnly)]
377 [WebCategoryAttribute ("Behavior")]
378 public virtual FormViewMode DefaultMode {
388 string[] dataKeyNames;
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 if (dataKeyNames == null)
401 dataKeyNames = value;
406 IOrderedDictionary KeyTable {
408 if (_keyTable == null) {
409 _keyTable = new OrderedDictionary (DataKeyNames.Length);
415 [BrowsableAttribute (false)]
416 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
417 public virtual DataKey DataKey {
420 key= new DataKey (KeyTable);
426 DataKey OldEditValues {
428 if (oldEditValues == null) {
429 oldEditValues = new DataKey (new OrderedDictionary ());
431 return oldEditValues;
435 [DefaultValue (null)]
436 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
437 [PersistenceMode (PersistenceMode.InnerProperty)]
439 public virtual ITemplate EditItemTemplate {
440 get { return editItemTemplate; }
441 set { editItemTemplate = value; RequireBinding (); }
444 [WebCategoryAttribute ("Styles")]
445 [PersistenceMode (PersistenceMode.InnerProperty)]
446 [NotifyParentProperty (true)]
447 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
448 [DefaultValueAttribute (null)]
449 public TableItemStyle EditRowStyle {
451 if (editRowStyle == null) {
452 editRowStyle = new TableItemStyle ();
453 if (IsTrackingViewState)
454 editRowStyle.TrackViewState();
460 [WebCategoryAttribute ("Styles")]
461 [PersistenceMode (PersistenceMode.InnerProperty)]
462 [NotifyParentProperty (true)]
463 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
464 [DefaultValueAttribute (null)]
465 public TableItemStyle EmptyDataRowStyle {
467 if (emptyDataRowStyle == null) {
468 emptyDataRowStyle = new TableItemStyle ();
469 if (IsTrackingViewState)
470 emptyDataRowStyle.TrackViewState();
472 return emptyDataRowStyle;
476 [DefaultValue (null)]
477 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
478 [PersistenceMode (PersistenceMode.InnerProperty)]
480 public virtual ITemplate EmptyDataTemplate {
481 get { return emptyDataTemplate; }
482 set { emptyDataTemplate = value; RequireBinding (); }
485 [LocalizableAttribute (true)]
486 [WebCategoryAttribute ("Appearance")]
487 [DefaultValueAttribute ("")]
488 public virtual string EmptyDataText {
490 object ob = ViewState ["EmptyDataText"];
491 if (ob != null) return (string) ob;
495 ViewState ["EmptyDataText"] = value;
500 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
501 [BrowsableAttribute (false)]
502 public virtual FormViewRow FooterRow {
504 EnsureChildControls ();
509 [DefaultValue (null)]
510 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
511 [PersistenceMode (PersistenceMode.InnerProperty)]
513 public virtual ITemplate FooterTemplate {
514 get { return footerTemplate; }
515 set { footerTemplate = value; RequireBinding (); }
518 [LocalizableAttribute (true)]
519 [WebCategoryAttribute ("Appearance")]
520 [DefaultValueAttribute ("")]
521 public virtual string FooterText {
523 object ob = ViewState ["FooterText"];
524 if (ob != null) return (string) ob;
528 ViewState ["FooterText"] = value;
533 [WebCategoryAttribute ("Styles")]
534 [PersistenceMode (PersistenceMode.InnerProperty)]
535 [NotifyParentProperty (true)]
536 [DefaultValue (null)]
537 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
538 public TableItemStyle FooterStyle {
540 if (footerStyle == null) {
541 footerStyle = new TableItemStyle ();
542 if (IsTrackingViewState)
543 footerStyle.TrackViewState();
549 [WebCategoryAttribute ("Appearance")]
550 [DefaultValueAttribute (GridLines.None)]
551 public virtual GridLines GridLines {
553 if (ControlStyleCreated)
554 return ((TableStyle) ControlStyle).GridLines;
555 return GridLines.None;
558 ((TableStyle) ControlStyle).GridLines = value;
562 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
563 [BrowsableAttribute (false)]
564 public virtual FormViewRow HeaderRow {
566 EnsureChildControls ();
571 [WebCategoryAttribute ("Styles")]
572 [PersistenceMode (PersistenceMode.InnerProperty)]
573 [NotifyParentProperty (true)]
574 [DefaultValue (null)]
575 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
576 public TableItemStyle HeaderStyle {
578 if (headerStyle == null) {
579 headerStyle = new TableItemStyle ();
580 if (IsTrackingViewState)
581 headerStyle.TrackViewState();
587 [DefaultValue (null)]
588 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
589 [PersistenceMode (PersistenceMode.InnerProperty)]
591 public virtual ITemplate HeaderTemplate {
592 get { return headerTemplate; }
593 set { headerTemplate = value; RequireBinding (); }
596 [LocalizableAttribute (true)]
597 [WebCategoryAttribute ("Appearance")]
598 [DefaultValueAttribute ("")]
599 public virtual string HeaderText {
601 object ob = ViewState ["HeaderText"];
602 if (ob != null) return (string) ob;
606 ViewState ["HeaderText"] = value;
611 [Category ("Layout")]
612 [DefaultValueAttribute (HorizontalAlign.NotSet)]
613 public virtual HorizontalAlign HorizontalAlign {
615 if (ControlStyleCreated)
616 return ((TableStyle) ControlStyle).HorizontalAlign;
617 return HorizontalAlign.NotSet;
620 ((TableStyle) ControlStyle).HorizontalAlign = value;
624 [DefaultValue (null)]
625 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
626 [PersistenceMode (PersistenceMode.InnerProperty)]
628 public virtual ITemplate InsertItemTemplate {
629 get { return insertItemTemplate; }
630 set { insertItemTemplate = value; RequireBinding (); }
633 [WebCategoryAttribute ("Styles")]
634 [PersistenceMode (PersistenceMode.InnerProperty)]
635 [NotifyParentProperty (true)]
636 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
637 [DefaultValueAttribute (null)]
638 public TableItemStyle InsertRowStyle {
640 if (insertRowStyle == null) {
641 insertRowStyle = new TableItemStyle ();
642 if (IsTrackingViewState)
643 insertRowStyle.TrackViewState();
645 return insertRowStyle;
649 [DefaultValue (null)]
650 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
651 [PersistenceMode (PersistenceMode.InnerProperty)]
653 public virtual ITemplate ItemTemplate {
654 get { return itemTemplate; }
655 set { itemTemplate = value; RequireBinding (); }
658 [BrowsableAttribute (false)]
659 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
660 public virtual int PageCount {
666 [WebCategoryAttribute ("Paging")]
667 [BindableAttribute (true, BindingDirection.OneWay)]
668 [DefaultValueAttribute (0)]
669 public virtual int PageIndex {
675 throw new ArgumentOutOfRangeException ("PageIndex must be non-negative");
676 if (pageIndex == value)
683 [WebCategoryAttribute ("Paging")]
684 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
685 [NotifyParentPropertyAttribute (true)]
686 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
687 public virtual PagerSettings PagerSettings {
689 if (pagerSettings == null) {
690 pagerSettings = new PagerSettings (this);
691 if (IsTrackingViewState)
692 ((IStateManager)pagerSettings).TrackViewState ();
694 return pagerSettings;
698 [WebCategoryAttribute ("Styles")]
699 [PersistenceMode (PersistenceMode.InnerProperty)]
700 [NotifyParentProperty (true)]
701 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
702 public TableItemStyle PagerStyle {
704 if (pagerStyle == null) {
705 pagerStyle = new TableItemStyle ();
706 if (IsTrackingViewState)
707 pagerStyle.TrackViewState();
714 [DefaultValue (null)]
715 /* DataControlPagerCell isnt specified in the docs */
716 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
717 [PersistenceMode (PersistenceMode.InnerProperty)]
719 public virtual ITemplate PagerTemplate {
720 get { return pagerTemplate; }
721 set { pagerTemplate = value; RequireBinding (); }
724 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
725 [BrowsableAttribute (false)]
726 public virtual FormViewRow Row {
728 EnsureChildControls ();
733 [WebCategoryAttribute ("Styles")]
734 [PersistenceMode (PersistenceMode.InnerProperty)]
735 [NotifyParentProperty (true)]
736 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
737 [DefaultValue (null)]
738 public TableItemStyle RowStyle {
740 if (rowStyle == null) {
741 rowStyle = new TableItemStyle ();
742 if (IsTrackingViewState)
743 rowStyle.TrackViewState();
749 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
750 [BrowsableAttribute (false)]
751 public object SelectedValue {
752 get { return DataKey.Value; }
755 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
756 [BrowsableAttribute (false)]
757 public virtual FormViewRow TopPagerRow {
759 EnsureChildControls ();
764 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
765 [BrowsableAttribute (false)]
766 public virtual object DataItem {
773 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
774 [BrowsableAttribute (false)]
775 public int DataItemCount {
776 get { return PageCount; }
779 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
780 [BrowsableAttribute (false)]
781 public virtual int DataItemIndex {
782 get { return PageIndex; }
785 int IDataItemContainer.DataItemIndex {
786 get { return DataItemIndex; }
789 int IDataItemContainer.DisplayIndex {
790 get { return PageIndex; }
793 public virtual bool IsBindableType (Type type)
795 return type.IsPrimitive || type == typeof (string) || type == typeof (DateTime) || type == typeof (Guid) || type == typeof (Decimal);
798 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
800 DataSourceSelectArguments arg = new DataSourceSelectArguments ();
801 DataSourceView view = GetData ();
802 if (AllowPaging && view.CanPage) {
803 arg.StartRowIndex = PageIndex;
804 if (view.CanRetrieveTotalRowCount) {
805 arg.RetrieveTotalRowCount = true;
809 arg.MaximumRows = -1;
815 protected virtual FormViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
817 FormViewRow row = new FormViewRow (rowIndex, rowType, rowState);
818 OnItemCreated (EventArgs.Empty);
822 void RequireBinding ()
825 RequiresDataBinding = true;
829 protected virtual Table CreateTable ()
831 return new ContainedTable (this);
834 protected override void EnsureDataBound ()
836 base.EnsureDataBound ();
839 protected override Style CreateControlStyle ()
841 TableStyle style = new TableStyle (ViewState);
842 style.CellSpacing = 0;
846 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
848 PagedDataSource dataSource = new PagedDataSource ();
849 dataSource.DataSource = data;
850 dataSource.AllowPaging = AllowPaging;
851 dataSource.PageSize = 1;
852 dataSource.CurrentPageIndex = PageIndex;
854 if (dataBinding && CurrentMode != FormViewMode.Insert) {
855 DataSourceView view = GetData ();
856 if (view != null && view.CanPage) {
857 dataSource.AllowServerPaging = true;
858 if (SelectArguments.RetrieveTotalRowCount)
859 dataSource.VirtualCount = SelectArguments.TotalRowCount;
863 pageCount = dataSource.DataSourceCount;
864 bool showPager = AllowPaging && (pageCount > 1);
867 table = CreateTable ();
868 Controls.Add (table);
870 // Gets the current data item
872 IEnumerator e = dataSource.GetEnumerator ();
877 dataItem = e.Current;
880 for (int page = 0; e.MoveNext (); page++ )
881 if (page == PageIndex)
882 dataItem = e.Current;
884 // Main table creation
886 if (HeaderText.Length != 0 || headerTemplate != null) {
887 headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
888 InitializeRow (headerRow);
889 table.Rows.Add (headerRow);
892 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
893 topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
894 InitializePager (topPagerRow, dataSource);
895 table.Rows.Add (topPagerRow);
899 DataControlRowState rstate = GetRowState ();
900 itemRow = CreateRow (0, DataControlRowType.DataRow, rstate);
901 InitializeRow (itemRow);
902 table.Rows.Add (itemRow);
904 switch (CurrentMode) {
905 case FormViewMode.Edit:
906 itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Edit);
908 case FormViewMode.Insert:
909 itemRow = CreateRow (-1, DataControlRowType.DataRow, DataControlRowState.Insert);
912 itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
915 table.Rows.Add (itemRow);
916 InitializeRow (itemRow);
919 if (FooterText.Length != 0 || footerTemplate != null) {
920 footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
921 InitializeRow (footerRow);
922 table.Rows.Add (footerRow);
925 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
926 bottomPagerRow = CreateRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
927 InitializePager (bottomPagerRow, dataSource);
928 table.Rows.Add (bottomPagerRow);
934 return dataSource.DataSourceCount;
937 DataControlRowState GetRowState ()
939 DataControlRowState rstate = DataControlRowState.Normal;
940 if (CurrentMode == FormViewMode.Edit) rstate |= DataControlRowState.Edit;
941 else if (CurrentMode == FormViewMode.Insert) rstate |= DataControlRowState.Insert;
945 protected virtual void InitializePager (FormViewRow row, PagedDataSource dataSource)
947 TableCell cell = new TableCell ();
950 if (pagerTemplate != null)
951 pagerTemplate.InstantiateIn (cell);
953 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
955 row.Cells.Add (cell);
958 protected virtual void InitializeRow (FormViewRow row)
960 TableCell cell = new TableCell ();
962 if (row.RowType == DataControlRowType.DataRow)
964 if ((row.RowState & DataControlRowState.Edit) != 0) {
965 if (editItemTemplate != null)
966 editItemTemplate.InstantiateIn (cell);
967 } else if ((row.RowState & DataControlRowState.Insert) != 0) {
968 if (insertItemTemplate != null)
969 insertItemTemplate.InstantiateIn (cell);
970 } else if (itemTemplate != null)
971 itemTemplate.InstantiateIn (cell);
973 else if (row.RowType == DataControlRowType.EmptyDataRow)
975 if (emptyDataTemplate != null)
976 emptyDataTemplate.InstantiateIn (cell);
978 cell.Text = EmptyDataText;
980 else if (row.RowType == DataControlRowType.Footer)
982 if (footerTemplate != null)
983 footerTemplate.InstantiateIn (cell);
985 cell.Text = FooterText;
987 else if (row.RowType == DataControlRowType.Header)
989 if (headerTemplate != null)
990 headerTemplate.InstantiateIn (cell);
992 cell.Text = HeaderText;
995 row.Cells.Add (cell);
998 void FillRowDataKey (object dataItem)
1002 if (cachedKeyProperties == null) {
1003 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
1004 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
1005 for (int n=0; n<DataKeyNames.Length; n++) {
1006 PropertyDescriptor p = props [DataKeyNames[n]];
1008 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
1009 cachedKeyProperties [n] = p;
1012 foreach (PropertyDescriptor p in cachedKeyProperties)
1013 KeyTable [p.Name] = p.GetValue (dataItem);
1016 IOrderedDictionary GetRowValues (bool includePrimaryKey)
1018 OrderedDictionary dic = new OrderedDictionary ();
1019 ExtractRowValues (dic, includePrimaryKey);
1023 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeKeys)
1028 DataControlRowState rowState = Row.RowState;
1029 IBindableTemplate bt;
1031 if ((rowState & DataControlRowState.Insert) != 0)
1032 bt = insertItemTemplate as IBindableTemplate;
1033 else if ((rowState & DataControlRowState.Edit) != 0)
1034 bt = editItemTemplate as IBindableTemplate;
1039 IOrderedDictionary values = bt.ExtractValues (Row.Cells [0]);
1040 foreach (DictionaryEntry e in values) {
1041 if (includeKeys || Array.IndexOf (DataKeyNames, e.Key) == -1)
1042 fieldValues [e.Key] = e.Value;
1047 protected override HtmlTextWriterTag TagKey {
1049 return HtmlTextWriterTag.Table;
1053 public sealed override void DataBind ()
1055 if (CurrentMode == FormViewMode.Insert) {
1056 RequiresDataBinding = false;
1057 PerformDataBinding (new object [] { null });
1061 cachedKeyProperties = null;
1064 if (pageCount > 0) {
1065 if (CurrentMode == FormViewMode.Edit)
1066 oldEditValues = new DataKey (GetRowValues (true));
1067 FillRowDataKey (dataItem);
1068 key = new DataKey (KeyTable);
1072 protected internal override void PerformDataBinding (IEnumerable data)
1074 base.PerformDataBinding (data);
1077 protected internal virtual void PrepareControlHierarchy ()
1082 table.Caption = Caption;
1083 table.CaptionAlign = CaptionAlign;
1085 foreach (FormViewRow row in table.Rows) {
1086 switch (row.RowType) {
1087 case DataControlRowType.Header:
1088 if (headerStyle != null && !headerStyle.IsEmpty)
1089 row.ControlStyle.CopyFrom (headerStyle);
1091 case DataControlRowType.Footer:
1092 if (footerStyle != null && !footerStyle.IsEmpty)
1093 row.ControlStyle.CopyFrom (footerStyle);
1095 case DataControlRowType.Pager:
1096 if (pagerStyle != null && !pagerStyle.IsEmpty)
1097 row.ControlStyle.CopyFrom (pagerStyle);
1099 case DataControlRowType.EmptyDataRow:
1100 if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
1101 row.ControlStyle.CopyFrom (emptyDataRowStyle);
1103 case DataControlRowType.DataRow:
1104 if (rowStyle != null && !rowStyle.IsEmpty)
1105 row.ControlStyle.CopyFrom (rowStyle);
1106 if ((row.RowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
1107 row.ControlStyle.CopyFrom (editRowStyle);
1108 if ((row.RowState & DataControlRowState.Insert) != 0 && insertRowStyle != null && !insertRowStyle.IsEmpty)
1109 row.ControlStyle.CopyFrom (insertRowStyle);
1117 protected internal override void OnInit (EventArgs e)
1119 Page.RegisterRequiresControlState (this);
1123 protected override bool OnBubbleEvent (object source, EventArgs e)
1125 FormViewCommandEventArgs args = e as FormViewCommandEventArgs;
1127 OnItemCommand (args);
1128 ProcessEvent (args.CommandName, args.CommandArgument as string);
1131 return base.OnBubbleEvent (source, e);
1134 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
1136 RaisePostBackEvent (eventArgument);
1139 protected virtual void RaisePostBackEvent (string eventArgument)
1141 int i = eventArgument.IndexOf ('$');
1143 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1145 ProcessEvent (eventArgument, null);
1148 void ProcessEvent (string eventName, string param)
1152 case DataControlCommands.PageCommandName:
1155 case DataControlCommands.FirstPageCommandArgument:
1158 case DataControlCommands.LastPageCommandArgument:
1159 newIndex = PageCount - 1;
1161 case DataControlCommands.NextPageCommandArgument:
1162 newIndex = PageIndex + 1;
1164 case DataControlCommands.PreviousPageCommandArgument:
1165 newIndex = PageIndex - 1;
1169 int.TryParse (param, out paramIndex);
1170 newIndex = paramIndex - 1;
1173 ShowPage (newIndex);
1176 case DataControlCommands.FirstPageCommandArgument:
1180 case DataControlCommands.LastPageCommandArgument:
1181 ShowPage (PageCount - 1);
1184 case DataControlCommands.NextPageCommandArgument:
1185 if (PageIndex < PageCount - 1)
1186 ShowPage (PageIndex + 1);
1189 case DataControlCommands.PreviousPageCommandArgument:
1191 ShowPage (PageIndex - 1);
1194 case DataControlCommands.EditCommandName:
1195 ChangeMode (FormViewMode.Edit);
1198 case DataControlCommands.NewCommandName:
1199 ChangeMode (FormViewMode.Insert);
1202 case DataControlCommands.UpdateCommandName:
1203 UpdateItem (param, true);
1206 case DataControlCommands.CancelCommandName:
1210 case DataControlCommands.DeleteCommandName:
1214 case DataControlCommands.InsertCommandName:
1220 void ShowPage (int index)
1222 FormViewPageEventArgs args = new FormViewPageEventArgs (index);
1223 OnPageIndexChanging (args);
1225 int newIndex = args.NewPageIndex;
1226 if (newIndex < 0 || newIndex >= PageCount)
1229 PageIndex = newIndex;
1230 OnPageIndexChanged (EventArgs.Empty);
1234 public void ChangeMode (FormViewMode newMode)
1236 FormViewModeEventArgs args = new FormViewModeEventArgs (newMode, false);
1237 OnModeChanging (args);
1239 CurrentMode = args.NewMode;
1240 OnModeChanged (EventArgs.Empty);
1247 FormViewModeEventArgs args = new FormViewModeEventArgs (FormViewMode.ReadOnly, true);
1248 OnModeChanging (args);
1254 public virtual void UpdateItem (bool causesValidation)
1256 UpdateItem (null, causesValidation);
1259 void UpdateItem (string param, bool causesValidation)
1261 if (causesValidation && Page != null)
1264 if (currentMode != FormViewMode.Edit) throw new HttpException ("Must be in Edit mode");
1266 currentEditOldValues = OldEditValues.Values;
1267 currentEditRowKeys = DataKey.Values;
1268 currentEditNewValues = GetRowValues (false);
1270 FormViewUpdateEventArgs args = new FormViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1271 OnItemUpdating (args);
1273 DataSourceView view = GetData ();
1275 throw new HttpException ("The DataSourceView associated to data bound control was null");
1277 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1283 bool UpdateCallback (int recordsAffected, Exception exception)
1285 FormViewUpdatedEventArgs dargs = new FormViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1286 OnItemUpdated (dargs);
1288 if (!dargs.KeepInEditMode)
1291 return dargs.ExceptionHandled;
1294 public virtual void InsertItem (bool causesValidation)
1296 InsertItem (null, causesValidation);
1299 void InsertItem (string param, bool causesValidation)
1301 if (causesValidation && Page != null)
1304 if (currentMode != FormViewMode.Insert) throw new HttpException ("Must be in Insert mode");
1306 currentEditNewValues = GetRowValues (true);
1307 FormViewInsertEventArgs args = new FormViewInsertEventArgs (param, currentEditNewValues);
1308 OnItemInserting (args);
1310 DataSourceView view = GetData ();
1312 throw new HttpException ("The DataSourceView associated to data bound control was null");
1314 view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
1320 bool InsertCallback (int recordsAffected, Exception exception)
1322 FormViewInsertedEventArgs dargs = new FormViewInsertedEventArgs (recordsAffected, exception, currentEditNewValues);
1323 OnItemInserted (dargs);
1325 if (!dargs.KeepInInsertMode)
1328 return dargs.ExceptionHandled;
1331 public virtual void DeleteItem ()
1333 currentEditRowKeys = DataKey.Values;
1334 currentEditNewValues = GetRowValues (true);
1336 FormViewDeleteEventArgs args = new FormViewDeleteEventArgs (PageIndex, currentEditRowKeys, currentEditNewValues);
1337 OnItemDeleting (args);
1340 if (PageIndex > 0 && PageIndex == PageCount - 1)
1345 DataSourceView view = GetData ();
1346 if (view != null && view.CanDelete)
1347 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1349 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1350 OnItemDeleted (dargs);
1355 bool DeleteCallback (int recordsAffected, Exception exception)
1357 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1358 OnItemDeleted (dargs);
1359 return dargs.ExceptionHandled;
1367 void EndRowEdit (bool switchToDefaultMode)
1369 if (switchToDefaultMode)
1370 ChangeMode (DefaultMode);
1371 oldEditValues = new DataKey (new OrderedDictionary ());
1372 currentEditRowKeys = null;
1373 currentEditOldValues = null;
1374 currentEditNewValues = null;
1378 protected internal override void LoadControlState (object ob)
1380 if (ob == null) return;
1381 object[] state = (object[]) ob;
1382 base.LoadControlState (state[0]);
1383 pageIndex = (int) state[1];
1384 pageCount = (int) state[2];
1385 CurrentMode = (FormViewMode) state[3];
1386 defaultMode = (FormViewMode) state[4];
1387 dataKeyNames = (string[]) state[5];
1388 if (state [6] != null)
1389 ((IStateManager) DataKey).LoadViewState (state [6]);
1390 if (state [7] != null)
1391 ((IStateManager) OldEditValues).LoadViewState (state [7]);
1394 protected internal override object SaveControlState ()
1396 object bstate = base.SaveControlState ();
1397 return new object [] {
1404 (key == null ? null : ((IStateManager)key).SaveViewState()),
1405 (oldEditValues == null ? null : ((IStateManager) oldEditValues).SaveViewState ())
1409 protected override void TrackViewState()
1411 base.TrackViewState();
1412 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1413 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1414 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1415 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1416 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1417 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1418 if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
1419 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1422 protected override object SaveViewState()
1424 object[] states = new object [10];
1425 states[0] = base.SaveViewState();
1426 states[1] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1427 states[2] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1428 states[3] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1429 states[4] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1430 states[5] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1431 states[6] = (insertRowStyle == null ? null : ((IStateManager)insertRowStyle).SaveViewState());
1432 states[7] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1433 states[8] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1435 for (int i = states.Length - 1; i >= 0; i--) {
1436 if (states [i] != null)
1443 protected override void LoadViewState (object savedState)
1445 if (savedState == null) {
1446 base.LoadViewState (null);
1450 object [] states = (object []) savedState;
1452 base.LoadViewState (states[0]);
1454 if (states[1] != null) ((IStateManager)PagerSettings).LoadViewState (states[1]);
1455 if (states[2] != null) ((IStateManager)FooterStyle).LoadViewState (states[2]);
1456 if (states[3] != null) ((IStateManager)HeaderStyle).LoadViewState (states[3]);
1457 if (states[4] != null) ((IStateManager)PagerStyle).LoadViewState (states[4]);
1458 if (states[5] != null) ((IStateManager)RowStyle).LoadViewState (states[5]);
1459 if (states[6] != null) ((IStateManager)InsertRowStyle).LoadViewState (states[6]);
1460 if (states[7] != null) ((IStateManager)EditRowStyle).LoadViewState (states[7]);
1461 if (states[8] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[8]);
1464 protected internal override void Render (HtmlTextWriter writer)
1466 PrepareControlHierarchy ();
1471 table.Render (writer);
1474 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
1476 if (control == null)
1477 throw new ArgumentNullException ("control");
1479 if (control.CausesValidation)
1480 throw new InvalidOperationException ("A button that causes validation in FormView '" + ID + "' is attempting to use the container GridView as the post back target. The button should either turn off validation or use itself as the post back container.");
1482 PostBackOptions options = new PostBackOptions (this);
1483 options.Argument = control.CommandName + "$" + control.CommandArgument;
1484 options.RequiresJavaScriptProtocol = true;