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;
109 key = new DataKey (new OrderedDictionary ());
\r
112 public event EventHandler PageIndexChanged {
113 add { Events.AddHandler (PageIndexChangedEvent, value); }
114 remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
117 public event FormViewPageEventHandler PageIndexChanging {
118 add { Events.AddHandler (PageIndexChangingEvent, value); }
119 remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
122 public event FormViewCommandEventHandler ItemCommand {
123 add { Events.AddHandler (ItemCommandEvent, value); }
124 remove { Events.RemoveHandler (ItemCommandEvent, value); }
127 public event EventHandler ItemCreated {
128 add { Events.AddHandler (ItemCreatedEvent, value); }
129 remove { Events.RemoveHandler (ItemCreatedEvent, value); }
132 public event FormViewDeletedEventHandler ItemDeleted {
133 add { Events.AddHandler (ItemDeletedEvent, value); }
134 remove { Events.RemoveHandler (ItemDeletedEvent, value); }
137 public event FormViewDeleteEventHandler ItemDeleting {
138 add { Events.AddHandler (ItemDeletingEvent, value); }
139 remove { Events.RemoveHandler (ItemDeletingEvent, value); }
142 public event FormViewInsertedEventHandler ItemInserted {
143 add { Events.AddHandler (ItemInsertedEvent, value); }
144 remove { Events.RemoveHandler (ItemInsertedEvent, value); }
147 public event FormViewInsertEventHandler ItemInserting {
148 add { Events.AddHandler (ItemInsertingEvent, value); }
149 remove { Events.RemoveHandler (ItemInsertingEvent, value); }
152 public event FormViewModeEventHandler ModeChanging {
153 add { Events.AddHandler (ModeChangingEvent, value); }
154 remove { Events.RemoveHandler (ModeChangingEvent, value); }
157 public event EventHandler ModeChanged {
158 add { Events.AddHandler (ModeChangedEvent, value); }
159 remove { Events.RemoveHandler (ModeChangedEvent, value); }
162 public event FormViewUpdatedEventHandler ItemUpdated {
163 add { Events.AddHandler (ItemUpdatedEvent, value); }
164 remove { Events.RemoveHandler (ItemUpdatedEvent, value); }
167 public event FormViewUpdateEventHandler ItemUpdating {
168 add { Events.AddHandler (ItemUpdatingEvent, value); }
169 remove { Events.RemoveHandler (ItemUpdatingEvent, value); }
172 protected virtual void OnPageIndexChanged (EventArgs e)
174 if (Events != null) {
175 EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
176 if (eh != null) eh (this, e);
180 protected virtual void OnPageIndexChanging (FormViewPageEventArgs e)
182 if (Events != null) {
183 FormViewPageEventHandler eh = (FormViewPageEventHandler) Events [PageIndexChangingEvent];
184 if (eh != null) eh (this, e);
188 protected virtual void OnItemCommand (FormViewCommandEventArgs e)
190 if (Events != null) {
191 FormViewCommandEventHandler eh = (FormViewCommandEventHandler) Events [ItemCommandEvent];
192 if (eh != null) eh (this, e);
196 protected virtual void OnItemCreated (EventArgs e)
198 if (Events != null) {
199 EventHandler eh = (EventHandler) Events [ItemCreatedEvent];
200 if (eh != null) eh (this, e);
204 protected virtual void OnItemDeleted (FormViewDeletedEventArgs e)
206 if (Events != null) {
207 FormViewDeletedEventHandler eh = (FormViewDeletedEventHandler) Events [ItemDeletedEvent];
208 if (eh != null) eh (this, e);
212 protected virtual void OnItemInserted (FormViewInsertedEventArgs e)
214 if (Events != null) {
215 FormViewInsertedEventHandler eh = (FormViewInsertedEventHandler) Events [ItemInsertedEvent];
216 if (eh != null) eh (this, e);
220 protected virtual void OnItemInserting (FormViewInsertEventArgs e)
222 if (Events != null) {
223 FormViewInsertEventHandler eh = (FormViewInsertEventHandler) Events [ItemInsertingEvent];
224 if (eh != null) eh (this, e);
228 protected virtual void OnItemDeleting (FormViewDeleteEventArgs e)
230 if (Events != null) {
231 FormViewDeleteEventHandler eh = (FormViewDeleteEventHandler) Events [ItemDeletingEvent];
232 if (eh != null) eh (this, e);
236 protected virtual void OnModeChanged (EventArgs e)
238 if (Events != null) {
239 EventHandler eh = (EventHandler) Events [ModeChangedEvent];
240 if (eh != null) eh (this, e);
244 protected virtual void OnModeChanging (FormViewModeEventArgs e)
246 if (Events != null) {
247 FormViewModeEventHandler eh = (FormViewModeEventHandler) Events [ModeChangingEvent];
248 if (eh != null) eh (this, e);
252 protected virtual void OnItemUpdated (FormViewUpdatedEventArgs e)
254 if (Events != null) {
255 FormViewUpdatedEventHandler eh = (FormViewUpdatedEventHandler) Events [ItemUpdatedEvent];
256 if (eh != null) eh (this, e);
260 protected virtual void OnItemUpdating (FormViewUpdateEventArgs e)
262 if (Events != null) {
263 FormViewUpdateEventHandler eh = (FormViewUpdateEventHandler) Events [ItemUpdatingEvent];
264 if (eh != null) eh (this, e);
269 [WebCategoryAttribute ("Paging")]
270 [DefaultValueAttribute (false)]
271 public virtual bool AllowPaging {
273 object ob = ViewState ["AllowPaging"];
274 if (ob != null) return (bool) ob;
278 ViewState ["AllowPaging"] = value;
283 [UrlPropertyAttribute]
284 [WebCategoryAttribute ("Appearance")]
285 [DefaultValueAttribute ("")]
286 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
287 public virtual string BackImageUrl {
289 object ob = ViewState ["BackImageUrl"];
290 if (ob != null) return (string) ob;
294 ViewState ["BackImageUrl"] = value;
299 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
300 [BrowsableAttribute (false)]
301 public virtual FormViewRow BottomPagerRow {
304 return bottomPagerRow;
308 [WebCategoryAttribute ("Accessibility")]
309 [DefaultValueAttribute ("")]
310 [LocalizableAttribute (true)]
311 public virtual string Caption {
313 object ob = ViewState ["Caption"];
314 if (ob != null) return (string) ob;
318 ViewState ["Caption"] = value;
323 [WebCategoryAttribute ("Accessibility")]
324 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
325 public virtual TableCaptionAlign CaptionAlign
328 object o = ViewState ["CaptionAlign"];
329 if(o != null) return (TableCaptionAlign) o;
330 return TableCaptionAlign.NotSet;
333 ViewState ["CaptionAlign"] = value;
338 [WebCategoryAttribute ("Layout")]
339 [DefaultValueAttribute (-1)]
340 public virtual int CellPadding
343 object o = ViewState ["CellPadding"];
344 if (o != null) return (int) o;
349 throw new ArgumentOutOfRangeException ("< -1");
\r
350 ViewState ["CellPadding"] = value;
355 [WebCategoryAttribute ("Layout")]
356 [DefaultValueAttribute (0)]
357 public virtual int CellSpacing
360 object o = ViewState ["CellSpacing"];
361 if (o != null) return (int) o;
366 throw new ArgumentOutOfRangeException ("< -1");
\r
367 ViewState["CellSpacing"] = value;
372 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
373 [BrowsableAttribute (false)]
374 public FormViewMode CurrentMode {
380 FormViewMode defaultMode;
382 [DefaultValueAttribute (FormViewMode.ReadOnly)]
383 [WebCategoryAttribute ("Behavior")]
384 public virtual FormViewMode DefaultMode {
394 string[] dataKeyNames;
395 [DefaultValueAttribute (null)]
396 [WebCategoryAttribute ("Data")]
397 [TypeConverter (typeof(StringArrayConverter))]
398 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
399 public virtual string[] DataKeyNames
402 if (dataKeyNames == null)
407 dataKeyNames = value;
412 [BrowsableAttribute (false)]
413 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
414 public virtual DataKey DataKey {
421 [DefaultValue (null)]
422 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
423 [PersistenceMode (PersistenceMode.InnerProperty)]
425 public virtual ITemplate EditItemTemplate {
426 get { return editItemTemplate; }
427 set { editItemTemplate = value; RequireBinding (); }
430 [WebCategoryAttribute ("Styles")]
431 [PersistenceMode (PersistenceMode.InnerProperty)]
432 [NotifyParentProperty (true)]
433 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
434 [DefaultValueAttribute (null)]
435 public TableItemStyle EditRowStyle {
437 if (editRowStyle == null) {
438 editRowStyle = new TableItemStyle ();
439 if (IsTrackingViewState)
440 editRowStyle.TrackViewState();
446 [WebCategoryAttribute ("Styles")]
447 [PersistenceMode (PersistenceMode.InnerProperty)]
448 [NotifyParentProperty (true)]
449 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
450 [DefaultValueAttribute (null)]
451 public TableItemStyle EmptyDataRowStyle {
453 if (emptyDataRowStyle == null) {
454 emptyDataRowStyle = new TableItemStyle ();
455 if (IsTrackingViewState)
456 emptyDataRowStyle.TrackViewState();
458 return emptyDataRowStyle;
462 [DefaultValue (null)]
463 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
464 [PersistenceMode (PersistenceMode.InnerProperty)]
466 public virtual ITemplate EmptyDataTemplate {
467 get { return emptyDataTemplate; }
468 set { emptyDataTemplate = value; RequireBinding (); }
471 [LocalizableAttribute (true)]
472 [WebCategoryAttribute ("Appearance")]
473 [DefaultValueAttribute ("")]
474 public virtual string EmptyDataText {
476 object ob = ViewState ["EmptyDataText"];
477 if (ob != null) return (string) ob;
481 ViewState ["EmptyDataText"] = value;
486 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
487 [BrowsableAttribute (false)]
488 public virtual FormViewRow FooterRow {
490 EnsureChildControls ();
495 [DefaultValue (null)]
496 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
497 [PersistenceMode (PersistenceMode.InnerProperty)]
499 public virtual ITemplate FooterTemplate {
500 get { return footerTemplate; }
501 set { footerTemplate = value; RequireBinding (); }
504 [LocalizableAttribute (true)]
505 [WebCategoryAttribute ("Appearance")]
506 [DefaultValueAttribute ("")]
507 public virtual string FooterText {
509 object ob = ViewState ["FooterText"];
510 if (ob != null) return (string) ob;
514 ViewState ["FooterText"] = value;
519 [WebCategoryAttribute ("Styles")]
520 [PersistenceMode (PersistenceMode.InnerProperty)]
521 [NotifyParentProperty (true)]
522 [DefaultValue (null)]
523 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
524 public TableItemStyle FooterStyle {
526 if (footerStyle == null) {
527 footerStyle = new TableItemStyle ();
528 if (IsTrackingViewState)
529 footerStyle.TrackViewState();
535 [WebCategoryAttribute ("Appearance")]
536 [DefaultValueAttribute (GridLines.None)]
537 public virtual GridLines GridLines {
539 return ((TableStyle) ControlStyle).GridLines;
\r
542 ((TableStyle) ControlStyle).GridLines = value;
\r
546 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
547 [BrowsableAttribute (false)]
548 public virtual FormViewRow HeaderRow {
550 EnsureChildControls ();
555 [WebCategoryAttribute ("Styles")]
556 [PersistenceMode (PersistenceMode.InnerProperty)]
557 [NotifyParentProperty (true)]
558 [DefaultValue (null)]
559 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
560 public TableItemStyle HeaderStyle {
562 if (headerStyle == null) {
563 headerStyle = new TableItemStyle ();
564 if (IsTrackingViewState)
565 headerStyle.TrackViewState();
571 [DefaultValue (null)]
572 [TemplateContainer (typeof(FormView), BindingDirection.OneWay)]
573 [PersistenceMode (PersistenceMode.InnerProperty)]
575 public virtual ITemplate HeaderTemplate {
576 get { return headerTemplate; }
577 set { headerTemplate = value; RequireBinding (); }
580 [LocalizableAttribute (true)]
581 [WebCategoryAttribute ("Appearance")]
582 [DefaultValueAttribute ("")]
583 public virtual string HeaderText {
585 object ob = ViewState ["HeaderText"];
586 if (ob != null) return (string) ob;
590 ViewState ["HeaderText"] = value;
595 [Category ("Layout")]
596 [DefaultValueAttribute (HorizontalAlign.NotSet)]
597 public virtual HorizontalAlign HorizontalAlign {
599 object ob = ViewState ["HorizontalAlign"];
600 if (ob != null) return (HorizontalAlign) ob;
601 return HorizontalAlign.NotSet;
604 ViewState ["HorizontalAlign"] = value;
609 [DefaultValue (null)]
610 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
611 [PersistenceMode (PersistenceMode.InnerProperty)]
613 public virtual ITemplate InsertItemTemplate {
614 get { return insertItemTemplate; }
615 set { insertItemTemplate = value; RequireBinding (); }
618 [WebCategoryAttribute ("Styles")]
619 [PersistenceMode (PersistenceMode.InnerProperty)]
620 [NotifyParentProperty (true)]
621 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
622 [DefaultValueAttribute (null)]
623 public TableItemStyle InsertRowStyle {
625 if (insertRowStyle == null) {
626 insertRowStyle = new TableItemStyle ();
627 if (IsTrackingViewState)
628 insertRowStyle.TrackViewState();
630 return insertRowStyle;
634 [DefaultValue (null)]
635 [TemplateContainer (typeof(FormView), BindingDirection.TwoWay)]
636 [PersistenceMode (PersistenceMode.InnerProperty)]
638 public virtual ITemplate ItemTemplate {
639 get { return itemTemplate; }
640 set { itemTemplate = value; RequireBinding (); }
643 [BrowsableAttribute (false)]
644 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
645 public virtual int PageCount {
647 if (pageCount != 0) return pageCount;
653 [WebCategoryAttribute ("Paging")]
654 [BindableAttribute (true, BindingDirection.OneWay)]
655 [DefaultValueAttribute (0)]
656 public virtual int PageIndex {
661 if (pageIndex == value)
668 [WebCategoryAttribute ("Paging")]
669 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
670 [NotifyParentPropertyAttribute (true)]
671 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
672 public virtual PagerSettings PagerSettings {
674 if (pagerSettings == null) {
675 pagerSettings = new PagerSettings (this);
676 if (IsTrackingViewState)
677 ((IStateManager)pagerSettings).TrackViewState ();
679 return pagerSettings;
683 [WebCategoryAttribute ("Styles")]
684 [PersistenceMode (PersistenceMode.InnerProperty)]
685 [NotifyParentProperty (true)]
686 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
687 public TableItemStyle PagerStyle {
689 if (pagerStyle == null) {
690 pagerStyle = new TableItemStyle ();
691 if (IsTrackingViewState)
692 pagerStyle.TrackViewState();
699 [DefaultValue (null)]
700 /* DataControlPagerCell isnt specified in the docs */
701 //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
702 [PersistenceMode (PersistenceMode.InnerProperty)]
704 public virtual ITemplate PagerTemplate {
705 get { return pagerTemplate; }
706 set { pagerTemplate = value; RequireBinding (); }
709 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
710 [BrowsableAttribute (false)]
711 public virtual FormViewRow Row {
718 [WebCategoryAttribute ("Styles")]
719 [PersistenceMode (PersistenceMode.InnerProperty)]
720 [NotifyParentProperty (true)]
721 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
722 [DefaultValue (null)]
723 public TableItemStyle RowStyle {
725 if (rowStyle == null) {
726 rowStyle = new TableItemStyle ();
727 if (IsTrackingViewState)
728 rowStyle.TrackViewState();
734 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
735 [BrowsableAttribute (false)]
736 public object SelectedValue {
737 get { return DataKey.Value; }
740 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
741 [BrowsableAttribute (false)]
742 public virtual FormViewRow TopPagerRow {
749 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
750 [BrowsableAttribute (false)]
751 public virtual object DataItem {
758 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
759 [BrowsableAttribute (false)]
760 public int DataItemCount {
761 get { return PageCount; }
764 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
765 [BrowsableAttribute (false)]
766 public virtual int DataItemIndex {
767 get { return PageIndex; }
770 int IDataItemContainer.DataItemIndex {
\r
771 get { return DataItemIndex; }
\r
774 int IDataItemContainer.DisplayIndex {
775 get { return PageIndex; }
778 public virtual bool IsBindableType (Type type)
780 return type.IsPrimitive || type == typeof (string) || type == typeof (DateTime) || type == typeof (Guid) || type == typeof (Decimal);
\r
783 protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
785 return base.CreateDataSourceSelectArguments ();
788 protected virtual FormViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
790 FormViewRow row = new FormViewRow (rowIndex, rowType, rowState);
791 OnItemCreated (EventArgs.Empty);
795 void RequireBinding ()
799 RequiresDataBinding = true;
803 protected virtual Table CreateTable ()
805 Table table = new Table ();
806 table.Caption = Caption;
807 table.CaptionAlign = CaptionAlign;
808 table.CellPadding = CellPadding;
809 table.CellSpacing = CellSpacing;
810 table.HorizontalAlign = HorizontalAlign;
811 table.BackImageUrl = BackImageUrl;
816 protected override void EnsureDataBound ()
818 base.EnsureDataBound ();
821 protected override Style CreateControlStyle ()
823 TableStyle style = new TableStyle (ViewState);
\r
824 style.CellSpacing = 0;
\r
828 protected override int CreateChildControls (IEnumerable data, bool dataBinding)
830 PagedDataSource dataSource = new PagedDataSource ();
831 dataSource.DataSource = data;
832 dataSource.AllowPaging = AllowPaging;
833 dataSource.PageSize = 1;
834 dataSource.CurrentPageIndex = PageIndex;
837 DataSourceView view = GetData ();
838 if (view != null && view.CanPage) {
839 dataSource.AllowServerPaging = true;
840 if (view.CanRetrieveTotalRowCount)
841 dataSource.VirtualCount = SelectArguments.TotalRowCount;
845 pageCount = dataSource.DataSourceCount;
846 bool showPager = AllowPaging && (dataSource.Count > 0);
849 table = CreateTable ();
850 Controls.Add (table);
852 if (!Page.IsPostBack)
853 currentMode = DefaultMode;
855 // Gets the current data item
857 IEnumerator e = dataSource.GetEnumerator ();
862 dataItem = e.Current;
865 for (int page = 0; e.MoveNext (); page++ )
866 if (page == PageIndex)
867 dataItem = e.Current;
869 // Main table creation
871 if (HeaderText.Length != 0 || headerTemplate != null) {
872 headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
873 InitializeRow (headerRow);
874 table.Rows.Add (headerRow);
877 if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
878 topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
879 InitializePager (topPagerRow, dataSource);
880 table.Rows.Add (topPagerRow);
884 DataControlRowState rstate = GetRowState ();
885 itemRow = CreateRow (0, DataControlRowType.DataRow, rstate);
886 InitializeRow (itemRow);
887 table.Rows.Add (itemRow);
890 if (CurrentMode == FormViewMode.Edit)
891 oldEditValues = new DataKey (new OrderedDictionary ());
892 key = new DataKey (new OrderedDictionary (), DataKeyNames);
895 switch (CurrentMode) {
896 case FormViewMode.Edit:
897 itemRow = CreateRow (-1, DataControlRowType.DataRow, DataControlRowState.Edit);
899 case FormViewMode.Insert:
900 itemRow = CreateRow (-1, DataControlRowType.DataRow, DataControlRowState.Insert);
903 itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
906 table.Rows.Add (itemRow);
907 InitializeRow (itemRow);
910 if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
911 bottomPagerRow = CreateRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
912 InitializePager (bottomPagerRow, dataSource);
913 table.Rows.Add (bottomPagerRow);
916 if (FooterText.Length != 0 || footerTemplate != null) {
917 footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
918 InitializeRow (footerRow);
919 table.Rows.Add (footerRow);
925 return dataSource.DataSourceCount;
928 DataControlRowState GetRowState ()
930 DataControlRowState rstate = DataControlRowState.Normal;
931 if (CurrentMode == FormViewMode.Edit) rstate |= DataControlRowState.Edit;
932 else if (CurrentMode == FormViewMode.Insert) rstate |= DataControlRowState.Insert;
936 protected virtual void InitializePager (FormViewRow row, PagedDataSource dataSource)
938 TableCell cell = new TableCell ();
941 if (pagerTemplate != null)
942 pagerTemplate.InstantiateIn (cell);
944 cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
946 row.Cells.Add (cell);
949 protected virtual void InitializeRow (FormViewRow row)
951 TableCell cell = new TableCell ();
953 if (row.RowType == DataControlRowType.DataRow)
955 if ((row.RowState & DataControlRowState.Edit) != 0) {
956 if (editItemTemplate != null)
957 editItemTemplate.InstantiateIn (cell);
958 } else if ((row.RowState & DataControlRowState.Insert) != 0) {
959 if (insertItemTemplate != null)
960 insertItemTemplate.InstantiateIn (cell);
961 } else if (itemTemplate != null)
962 itemTemplate.InstantiateIn (cell);
964 else if (row.RowType == DataControlRowType.EmptyDataRow)
966 if (emptyDataTemplate != null)
967 emptyDataTemplate.InstantiateIn (cell);
969 cell.Text = EmptyDataText;
971 else if (row.RowType == DataControlRowType.Footer)
973 if (footerTemplate != null)
974 footerTemplate.InstantiateIn (cell);
976 cell.Text = FooterText;
978 else if (row.RowType == DataControlRowType.Header)
980 if (headerTemplate != null)
981 headerTemplate.InstantiateIn (cell);
983 cell.Text = HeaderText;
986 row.Cells.Add (cell);
989 IOrderedDictionary CreateRowDataKey (object dataItem)
991 if (cachedKeyProperties == null) {
992 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
993 cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
994 for (int n=0; n<DataKeyNames.Length; n++) {
995 PropertyDescriptor p = props [DataKeyNames[n]];
997 new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
998 cachedKeyProperties [n] = p;
1002 OrderedDictionary dic = new OrderedDictionary ();
1003 foreach (PropertyDescriptor p in cachedKeyProperties)
1004 dic [p.Name] = p.GetValue (dataItem);
1008 IOrderedDictionary GetRowValues (bool includePrimaryKey)
1010 OrderedDictionary dic = new OrderedDictionary ();
1011 ExtractRowValues (dic, includePrimaryKey);
1015 protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeKeys)
1020 DataControlRowState rowState = Row.RowState;
1021 IBindableTemplate bt;
1023 if ((rowState & DataControlRowState.Insert) != 0)
1024 bt = insertItemTemplate as IBindableTemplate;
1025 else if ((rowState & DataControlRowState.Edit) != 0)
1026 bt = editItemTemplate as IBindableTemplate;
1031 IOrderedDictionary values = bt.ExtractValues (Row.Cells [0]);
1032 foreach (DictionaryEntry e in values) {
1033 if (includeKeys || Array.IndexOf (DataKeyNames, e.Key) == -1)
1034 fieldValues [e.Key] = e.Value;
1039 protected override HtmlTextWriterTag TagKey {
1041 return HtmlTextWriterTag.Table;
1045 public sealed override void DataBind ()
1047 DataSourceView view = GetData ();
1048 if (AllowPaging && view.CanPage) {
1049 SelectArguments.StartRowIndex = PageIndex;
1050 SelectArguments.MaximumRows = 1;
1051 if (view.CanRetrieveTotalRowCount)
1052 SelectArguments.RetrieveTotalRowCount = true;
1055 cachedKeyProperties = null;
1058 if (pageCount > 0) {
1059 if (CurrentMode == FormViewMode.Edit)
1060 oldEditValues = new DataKey (GetRowValues (true));
1061 key = new DataKey (CreateRowDataKey (dataItem), DataKeyNames);
1065 protected internal override void PerformDataBinding (IEnumerable data)
1067 base.PerformDataBinding (data);
1071 protected internal virtual void PrepareControlHierarchy ()
1073 throw new NotImplementedException ();
1076 protected internal override void OnInit (EventArgs e)
1078 Page.RegisterRequiresControlState (this);
1082 protected override bool OnBubbleEvent (object source, EventArgs e)
1084 FormViewCommandEventArgs args = e as FormViewCommandEventArgs;
1086 OnItemCommand (args);
1087 ProcessEvent (args.CommandName, args.CommandArgument as string);
1089 return base.OnBubbleEvent (source, e);
1092 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
1094 RaisePostBackEvent (eventArgument);
1097 protected virtual void RaisePostBackEvent (string eventArgument)
1099 int i = eventArgument.IndexOf ('$');
1101 ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
1103 ProcessEvent (eventArgument, null);
1106 void ProcessEvent (string eventName, string param)
1110 case DataControlCommands.PageCommandName:
1113 case DataControlCommands.FirstPageCommandArgument:
1116 case DataControlCommands.LastPageCommandArgument:
1117 newIndex = PageCount - 1;
1119 case DataControlCommands.NextPageCommandArgument:
1120 if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
1122 case DataControlCommands.PreviousPageCommandArgument:
1123 if (PageIndex > 0) newIndex = PageIndex - 1;
1126 newIndex = int.Parse (param) - 1;
1129 ShowPage (newIndex);
1132 case DataControlCommands.FirstPageCommandArgument:
1136 case DataControlCommands.LastPageCommandArgument:
1137 ShowPage (PageCount - 1);
1140 case DataControlCommands.NextPageCommandArgument:
1141 if (PageIndex < PageCount - 1)
1142 ShowPage (PageIndex + 1);
1145 case DataControlCommands.PreviousPageCommandArgument:
1147 ShowPage (PageIndex - 1);
1150 case DataControlCommands.EditCommandName:
1151 ChangeMode (FormViewMode.Edit);
1154 case DataControlCommands.NewCommandName:
1155 ChangeMode (FormViewMode.Insert);
1158 case DataControlCommands.UpdateCommandName:
1159 UpdateItem (param, true);
1162 case DataControlCommands.CancelCommandName:
1166 case DataControlCommands.DeleteCommandName:
1170 case DataControlCommands.InsertCommandName:
1176 void ShowPage (int newIndex)
1178 FormViewPageEventArgs args = new FormViewPageEventArgs (newIndex);
1179 OnPageIndexChanging (args);
1181 newIndex = args.NewPageIndex;
1182 if (newIndex < 0 || newIndex >= PageCount)
1185 PageIndex = newIndex;
1186 OnPageIndexChanged (EventArgs.Empty);
1190 public void ChangeMode (FormViewMode newMode)
1192 FormViewModeEventArgs args = new FormViewModeEventArgs (newMode, false);
1193 OnModeChanging (args);
1195 currentMode = args.NewMode;
1196 OnModeChanged (EventArgs.Empty);
1203 FormViewModeEventArgs args = new FormViewModeEventArgs (FormViewMode.ReadOnly, true);
1204 OnModeChanging (args);
1210 public virtual void UpdateItem (bool causesValidation)
1212 UpdateItem (null, causesValidation);
1215 void UpdateItem (string param, bool causesValidation)
1217 if (causesValidation)
1220 if (currentMode != FormViewMode.Edit) throw new NotSupportedException ();
1222 currentEditOldValues = oldEditValues.Values;
1223 currentEditRowKeys = DataKey.Values;
1224 currentEditNewValues = GetRowValues (false);
1226 FormViewUpdateEventArgs args = new FormViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1227 OnItemUpdating (args);
1229 DataSourceView view = GetData ();
1230 if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
1231 view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
1236 bool UpdateCallback (int recordsAffected, Exception exception)
1238 FormViewUpdatedEventArgs dargs = new FormViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
1239 OnItemUpdated (dargs);
1241 if (!dargs.KeepInEditMode)
1244 return dargs.ExceptionHandled;
1247 public virtual void InsertItem (bool causesValidation)
1249 InsertItem (null, causesValidation);
1252 void InsertItem (string param, bool causesValidation)
1254 if (causesValidation)
1257 if (currentMode != FormViewMode.Insert) throw new NotSupportedException ();
1259 currentEditNewValues = GetRowValues (true);
1260 FormViewInsertEventArgs args = new FormViewInsertEventArgs (param, currentEditNewValues);
1261 OnItemInserting (args);
1263 DataSourceView view = GetData ();
1266 view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
1271 bool InsertCallback (int recordsAffected, Exception exception)
1273 FormViewInsertedEventArgs dargs = new FormViewInsertedEventArgs (recordsAffected, exception, currentEditNewValues);
1274 OnItemInserted (dargs);
1276 if (!dargs.KeepInInsertMode)
1279 return dargs.ExceptionHandled;
1282 public virtual void DeleteItem ()
1284 currentEditRowKeys = DataKey.Values;
1285 currentEditNewValues = GetRowValues (true);
1287 FormViewDeleteEventArgs args = new FormViewDeleteEventArgs (PageIndex, currentEditRowKeys, currentEditNewValues);
1288 OnItemDeleting (args);
1291 if (PageIndex == PageCount - 1)
1296 DataSourceView view = GetData ();
1298 view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
1300 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
1301 OnItemDeleted (dargs);
1306 bool DeleteCallback (int recordsAffected, Exception exception)
1308 FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
1309 OnItemDeleted (dargs);
1310 return dargs.ExceptionHandled;
1315 ChangeMode (DefaultMode);
1316 oldEditValues = new DataKey (new OrderedDictionary ());
1317 currentEditRowKeys = null;
1318 currentEditOldValues = null;
1319 currentEditNewValues = null;
1323 protected internal override void LoadControlState (object ob)
1325 if (ob == null) return;
1326 object[] state = (object[]) ob;
1327 base.LoadControlState (state[0]);
1328 pageIndex = (int) state[1];
1329 pageCount = (int) state[2];
1330 currentMode = (FormViewMode) state[3];
\r
1331 defaultMode = (FormViewMode) state[4];
\r
1332 dataKeyNames = (string[]) state[5];
1335 protected internal override object SaveControlState ()
1337 object bstate = base.SaveControlState ();
\r
1338 return new object[] {
1339 bstate, pageIndex, pageCount, currentMode, defaultMode, dataKeyNames
1343 protected override void TrackViewState()
1345 base.TrackViewState();
1346 if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
1347 if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
1348 if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
1349 if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
1350 if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
1351 if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
1352 if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
1353 if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
1354 if (key != null) ((IStateManager)key).TrackViewState();
1357 protected override object SaveViewState()
1359 object[] states = new object [14];
1360 states[0] = base.SaveViewState();
1361 states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
1362 states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
1363 states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
1364 states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
1365 states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
1366 states[8] = (insertRowStyle == null ? null : ((IStateManager)insertRowStyle).SaveViewState());
1367 states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
1368 states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
1369 states[11] = (key == null ? null : ((IStateManager)key).SaveViewState());
1370 states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
1372 for (int i = states.Length - 1; i >= 0; i--) {
1373 if (states [i] != null)
1380 protected override void LoadViewState (object savedState)
1382 if (savedState == null) {
1383 base.LoadViewState (null);
1387 object [] states = (object []) savedState;
1389 base.LoadViewState (states[0]);
1390 EnsureChildControls ();
1392 if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
1393 if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
1394 if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
1395 if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
1396 if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
1397 if (states[8] != null) ((IStateManager)InsertRowStyle).LoadViewState (states[8]);
1398 if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
1399 if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
1400 if (states[11] != null && DataKey != null) ((IStateManager)DataKey).LoadViewState (states[11]);
1401 if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
1404 protected internal override void Render (HtmlTextWriter writer)
1406 switch (GridLines) {
1407 case GridLines.Horizontal:
1408 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
1409 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1411 case GridLines.Vertical:
1412 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
1413 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1415 case GridLines.Both:
1416 writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
1417 writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
1420 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
1424 writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
1425 if (!string.IsNullOrEmpty (ControlStyle.CssClass))
1426 writer.AddAttribute (HtmlTextWriterAttribute.Class, ControlStyle.CssClass);
\r
1428 table.ControlStyle.MergeWith (ControlStyle);
\r
1429 table.RenderBeginTag (writer);
1431 foreach (FormViewRow row in table.Rows)
1433 switch (row.RowType) {
1434 case DataControlRowType.Header:
1435 if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
1437 case DataControlRowType.Footer:
1438 if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
1440 case DataControlRowType.Pager:
1441 if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
1443 case DataControlRowType.EmptyDataRow:
1444 if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
1447 if (rowStyle != null) rowStyle.AddAttributesToRender (writer, row);
1451 if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
1452 editRowStyle.AddAttributesToRender (writer, row);
1453 if ((row.RowState & DataControlRowState.Insert) != 0 && insertRowStyle != null)
1454 insertRowStyle.AddAttributesToRender (writer, row);
1456 row.RenderBeginTag (writer);
1458 for (int n=0; n<row.Cells.Count; n++)
1459 row.Cells[n].Render (writer);
1461 row.RenderEndTag (writer);
1463 table.RenderEndTag (writer);
1466 PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
1468 Control ctrl = control as Control;
1469 return new PostBackOptions(ctrl);