New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / FormView.cs
index 51c8043031d622297c43d27f801cb1539efba03d..ea63e1d7ea26f52820143b2e5ee28bf50c3d2abb 100644 (file)
@@ -102,10 +102,11 @@ namespace System.Web.UI.WebControls
                // Control state
                int pageIndex;
                FormViewMode currentMode = FormViewMode.ReadOnly; 
-               int pageCount = -1;
+               int pageCount = 0;
                
                public FormView ()
                {
+                       key = new DataKey (new OrderedDictionary ());
                }
                
                public event EventHandler PageIndexChanged {
@@ -344,6 +345,8 @@ namespace System.Web.UI.WebControls
                                return -1;
                        }
                        set {
+                               if (value < -1)
+                                       throw new ArgumentOutOfRangeException ("< -1");
                                ViewState ["CellPadding"] = value;
                                RequireBinding ();
                        }
@@ -359,7 +362,9 @@ namespace System.Web.UI.WebControls
                                return 0;
                        }
                        set {
-                               ViewState ["CellSpacing"] = value;
+                               if (value < -1)
+                                       throw new ArgumentOutOfRangeException ("< -1");
+                               ViewState["CellSpacing"] = value;
                                RequireBinding ();
                        }
                }
@@ -371,21 +376,22 @@ namespace System.Web.UI.WebControls
                                return currentMode;
                        }
                }
-       
+
+               FormViewMode defaultMode;
+
                [DefaultValueAttribute (FormViewMode.ReadOnly)]
                [WebCategoryAttribute ("Behavior")]
                public virtual FormViewMode DefaultMode {
                        get {
-                               object o = ViewState ["DefaultMode"];
-                               if (o != null) return (FormViewMode) o;
-                               return FormViewMode.ReadOnly;
+                               return defaultMode;
                        }
                        set {
-                               ViewState ["DefaultMode"] = value;
+                               defaultMode = value;
                                RequireBinding ();
                        }
                }
-       
+
+               string[] dataKeyNames;
                [DefaultValueAttribute (null)]
                [WebCategoryAttribute ("Data")]
                [TypeConverter (typeof(StringArrayConverter))]
@@ -393,12 +399,12 @@ namespace System.Web.UI.WebControls
                public virtual string[] DataKeyNames
                {
                        get {
-                               object o = ViewState ["DataKeyNames"];
-                               if (o != null) return (string[]) o;
-                               return emptyKeys;
+                               if (dataKeyNames == null)
+                                       return emptyKeys;
+                               return dataKeyNames;
                        }
                        set {
-                               ViewState ["DataKeyNames"] = value;
+                               dataKeyNames = value;
                                RequireBinding ();
                        }
                }
@@ -530,12 +536,10 @@ namespace System.Web.UI.WebControls
                [DefaultValueAttribute (GridLines.None)]
                public virtual GridLines GridLines {
                        get {
-                               object ob = ViewState ["GridLines"];
-                               if (ob != null) return (GridLines) ob;
-                               return GridLines.None;
+                               return ((TableStyle) ControlStyle).GridLines;
                        }
                        set {
-                               ViewState ["GridLines"] = value;
+                               ((TableStyle) ControlStyle).GridLines = value;
                        }
                }
 
@@ -640,7 +644,7 @@ namespace System.Web.UI.WebControls
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
                public virtual int PageCount {
                        get {
-                               if (pageCount != -1) return pageCount;
+                               if (pageCount != 0) return pageCount;
                                EnsureDataBound ();
                                return pageCount;
                        }
@@ -761,7 +765,11 @@ namespace System.Web.UI.WebControls
                [BrowsableAttribute (false)]
                public virtual int DataItemIndex {
                        get { return PageIndex; }
-               }               
+               }
+
+               int IDataItemContainer.DataItemIndex {
+                       get { return DataItemIndex; }
+               }
        
                int IDataItemContainer.DisplayIndex {
                        get { return PageIndex; }
@@ -769,7 +777,7 @@ namespace System.Web.UI.WebControls
        
                public virtual bool IsBindableType (Type type)
                {
-                       return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
+                       return type.IsPrimitive || type == typeof (string) || type == typeof (DateTime) || type == typeof (Guid) || type == typeof (Decimal);
                }
                
                protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
@@ -810,10 +818,11 @@ namespace System.Web.UI.WebControls
                        base.EnsureDataBound ();
                }
        
-               [MonoTODO]
                protected override Style CreateControlStyle ()
                {
-                       return base.CreateControlStyle ();
+                       TableStyle style = new TableStyle (ViewState);
+                       style.CellSpacing = 0;
+                       return style;
                }
                
                protected override int CreateChildControls (IEnumerable data, bool dataBinding)
@@ -826,20 +835,15 @@ namespace System.Web.UI.WebControls
 
                        if (dataBinding) {
                                DataSourceView view = GetData ();
-                               if (view.CanPage) {
+                               if (view != null && view.CanPage) {
                                        dataSource.AllowServerPaging = true;
                                        if (view.CanRetrieveTotalRowCount)
                                                dataSource.VirtualCount = SelectArguments.TotalRowCount;
-                                       else {
-                                               dataSource.DataSourceView = view;
-                                               dataSource.DataSourceSelectArguments = SelectArguments;
-                                               dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
-                                       }
                                }
                        }
 
                        pageCount = dataSource.DataSourceCount;
-                       bool showPager = AllowPaging && (dataSource.Count > 1);
+                       bool showPager = AllowPaging && (pageCount > 1);
                        
                        Controls.Clear ();
                        table = CreateTable ();
@@ -888,13 +892,23 @@ namespace System.Web.UI.WebControls
                                        key = new DataKey (new OrderedDictionary (), DataKeyNames);
                                }
                        } else {
-                               itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
+                               switch (CurrentMode) {
+                               case FormViewMode.Edit:
+                                       itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Edit);
+                                       break;
+                               case FormViewMode.Insert:
+                                       itemRow = CreateRow (-1, DataControlRowType.DataRow, DataControlRowState.Insert);
+                                       break;
+                               default:
+                                       itemRow = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
+                                       break;
+                               }
                                table.Rows.Add (itemRow);
                                InitializeRow (itemRow);
                        }
                                
                        if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
-                               bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
+                               bottomPagerRow = CreateRow (0, DataControlRowType.Pager, DataControlRowState.Normal);
                                InitializePager (bottomPagerRow, dataSource);
                                table.Rows.Add (bottomPagerRow);
                        }
@@ -922,11 +936,12 @@ namespace System.Web.UI.WebControls
                protected virtual void InitializePager (FormViewRow row, PagedDataSource dataSource)
                {
                        TableCell cell = new TableCell ();
-                       
+                       cell.ColumnSpan = 2;
+
                        if (pagerTemplate != null)
                                pagerTemplate.InstantiateIn (cell);
                        else
-                               cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
+                               cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount, pagerStyle));
                        
                        row.Cells.Add (cell);
                }
@@ -967,6 +982,7 @@ namespace System.Web.UI.WebControls
                                else
                                        cell.Text = HeaderText;
                        }
+                       cell.ColumnSpan = 2;
                        row.Cells.Add (cell);
                }
                
@@ -998,6 +1014,9 @@ namespace System.Web.UI.WebControls
                
                protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeKeys)
                {
+                       if (Row == null)
+                               return;
+
                        DataControlRowState rowState = Row.RowState;
                        IBindableTemplate bt;
                        
@@ -1025,6 +1044,12 @@ namespace System.Web.UI.WebControls
                
                public sealed override void DataBind ()
                {
+                       if (CurrentMode == FormViewMode.Insert) {
+                               RequiresDataBinding = false;
+                               PerformDataBinding (new object [] { null });
+                               return;
+                       }
+                       
                        DataSourceView view = GetData ();
                        if (AllowPaging && view.CanPage) {
                                SelectArguments.StartRowIndex = PageIndex;
@@ -1039,6 +1064,8 @@ namespace System.Web.UI.WebControls
                        if (pageCount > 0) {
                                if (CurrentMode == FormViewMode.Edit)
                                        oldEditValues = new DataKey (GetRowValues (true));
+                               else
+                                       oldEditValues = new DataKey (new OrderedDictionary ());
                                key = new DataKey (CreateRowDataKey (dataItem), DataKeyNames);
                        }
                }
@@ -1098,13 +1125,15 @@ namespace System.Web.UI.WebControls
                                        newIndex = PageCount - 1;
                                        break;
                                case DataControlCommands.NextPageCommandArgument:
-                                       if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
+                                       newIndex = PageIndex + 1;
                                        break;
                                case DataControlCommands.PreviousPageCommandArgument:
-                                       if (PageIndex > 0) newIndex = PageIndex - 1;
+                                       newIndex = PageIndex - 1;
                                        break;
                                default:
-                                       newIndex = int.Parse (param) - 1;
+                                       int paramIndex = 0;
+                                       int.TryParse (param, out paramIndex);
+                                       newIndex = paramIndex - 1;
                                        break;
                                }
                                ShowPage (newIndex);
@@ -1159,8 +1188,11 @@ namespace System.Web.UI.WebControls
                        FormViewPageEventArgs args = new FormViewPageEventArgs (newIndex);
                        OnPageIndexChanging (args);
                        if (!args.Cancel) {
-                               EndRowEdit ();
-                               PageIndex = args.NewPageIndex;
+                               newIndex = args.NewPageIndex;
+                               if (newIndex < 0 || newIndex >= PageCount)
+                                       return;
+                               EndRowEdit (false);
+                               PageIndex = newIndex;
                                OnPageIndexChanged (EventArgs.Empty);
                        }
                }
@@ -1205,9 +1237,12 @@ namespace System.Web.UI.WebControls
                        OnItemUpdating (args);
                        if (!args.Cancel) {
                                DataSourceView view = GetData ();
-                               if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
-                               view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
-                       } else
+                               if (view == null)
+                                       throw new HttpException ("The DataSourceView associated to data bound control was null");
+                               if (view.CanUpdate)
+                                       view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
+                       }
+                       else
                                EndRowEdit ();
                }
 
@@ -1239,9 +1274,12 @@ namespace System.Web.UI.WebControls
                        OnItemInserting (args);
                        if (!args.Cancel) {
                                DataSourceView view = GetData ();
-                               if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
-                               view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
-                       } else
+                               if (view == null)
+                                       throw new HttpException ("The DataSourceView associated to data bound control was null");
+                               if (view.CanInsert)
+                                       view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
+                       }
+                       else
                                EndRowEdit ();
                }
                
@@ -1271,7 +1309,7 @@ namespace System.Web.UI.WebControls
                                RequireBinding ();
                                        
                                DataSourceView view = GetData ();
-                               if (view != null)
+                               if (view != null && view.CanDelete)
                                        view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
                                else {
                                        FormViewDeletedEventArgs dargs = new FormViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
@@ -1289,7 +1327,13 @@ namespace System.Web.UI.WebControls
                
                void EndRowEdit ()
                {
-                       ChangeMode (DefaultMode);
+                       EndRowEdit (true);
+               }
+
+               void EndRowEdit (bool switchToDefaultMode) 
+               {
+                       if (switchToDefaultMode)
+                               ChangeMode (DefaultMode);
                        oldEditValues = new DataKey (new OrderedDictionary ());
                        currentEditRowKeys = null;
                        currentEditOldValues = null;
@@ -1305,14 +1349,16 @@ namespace System.Web.UI.WebControls
                        pageIndex = (int) state[1];
                        pageCount = (int) state[2];
                        currentMode = (FormViewMode) state[3];
+                       defaultMode = (FormViewMode) state[4];
+                       dataKeyNames = (string[]) state[5];
                }
                
                protected internal override object SaveControlState ()
                {
                        object bstate = base.SaveControlState ();
                        return new object[] {
-                               bstate, pageIndex, pageCount, currentMode
-                                       };
+                               bstate, pageIndex, pageCount, currentMode, defaultMode, dataKeyNames
+                       };
                }
                
                protected override void TrackViewState()
@@ -1397,9 +1443,10 @@ namespace System.Web.UI.WebControls
                        }
                        
                        writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
-                       writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
                        if (!string.IsNullOrEmpty (ControlStyle.CssClass))
                                writer.AddAttribute (HtmlTextWriterAttribute.Class, ControlStyle.CssClass);
+
+                       table.ControlStyle.MergeWith (ControlStyle);
                        table.RenderBeginTag (writer);
                        
                        foreach (FormViewRow row in table.Rows)
@@ -1437,10 +1484,10 @@ namespace System.Web.UI.WebControls
                        table.RenderEndTag (writer);
                }
 
-               [MonoTODO]
                PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
                {
-                       throw new NotImplementedException ();
+                       Control ctrl = control as Control;
+                       return new PostBackOptions(ctrl);
                }
 
        }