Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DetailsView.cs
index d4b7438d777b507d9e7d942821e0700ad0a728db..af981d0994395f7fb944f5e24164f85185df9482 100644 (file)
@@ -4,7 +4,7 @@
 // Authors:
 //     Lluis Sanchez Gual (lluis@novell.com)
 //
-// (C) 2005 Novell, Inc (http://www.novell.com)
+// (C) 2005-2010 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -26,8 +26,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using System;
 using System.Collections;
 using System.Collections.Specialized;
@@ -45,12 +43,11 @@ namespace System.Web.UI.WebControls
        [DesignerAttribute ("System.Web.UI.Design.WebControls.DetailsViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
        [ControlValuePropertyAttribute ("SelectedValue")]
        [DefaultEventAttribute ("PageIndexChanging")]
+       [DataKeyProperty ("DataKey")]
        [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        public class DetailsView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IDataItemContainer, INamingContainer, IPostBackEventHandler, IPostBackContainer
-#if NET_4_0
-               , IDataBoundItemControl
-#endif
+               , IDataBoundItemControl, IDataBoundControl, IFieldControl
        {
                object dataItem;
                
@@ -299,8 +296,6 @@ namespace System.Web.UI.WebControls
                                throw new HttpException (String.Format (unhandledEventExceptionMessage, ID, "ItemUpdating"));
                }
                
-#if NET_4_0
-               
                DataBoundControlMode IDataBoundItemControl.Mode {
                        get {
                                switch (CurrentMode) {
@@ -322,14 +317,23 @@ namespace System.Web.UI.WebControls
                IDataSource IDataBoundControl.DataSourceObject {
                        get { return base.DataSourceObject; }
                }
-#endif
-               
+
+               IAutoFieldGenerator IFieldControl.FieldsGenerator {
+                       get {
+                               throw new NotImplementedException ();
+                       }
+                       
+                       set {
+                               throw new NotImplementedException ();
+                       }
+               }
                [WebCategoryAttribute ("Paging")]
                [DefaultValueAttribute (false)]
                public virtual bool AllowPaging {
                        get {
                                object ob = ViewState ["AllowPaging"];
-                               if (ob != null) return (bool) ob;
+                               if (ob != null)
+                                       return (bool) ob;
                                return false;
                        }
                        set {
@@ -359,7 +363,8 @@ namespace System.Web.UI.WebControls
                public virtual bool AutoGenerateEditButton {
                        get {
                                object ob = ViewState ["AutoGenerateEditButton"];
-                               if (ob != null) return (bool) ob;
+                               if (ob != null)
+                                       return (bool) ob;
                                return false;
                        }
                        set {
@@ -373,7 +378,8 @@ namespace System.Web.UI.WebControls
                public virtual bool AutoGenerateDeleteButton {
                        get {
                                object ob = ViewState ["AutoGenerateDeleteButton"];
-                               if (ob != null) return (bool) ob;
+                               if (ob != null)
+                                       return (bool) ob;
                                return false;
                        }
                        set {
@@ -387,7 +393,8 @@ namespace System.Web.UI.WebControls
                public virtual bool AutoGenerateInsertButton {
                        get {
                                object ob = ViewState ["AutoGenerateInsertButton"];
-                               if (ob != null) return (bool) ob;
+                               if (ob != null)
+                                       return (bool) ob;
                                return false;
                        }
                        set {
@@ -401,7 +408,8 @@ namespace System.Web.UI.WebControls
                public virtual bool AutoGenerateRows {
                        get {
                                object ob = ViewState ["AutoGenerateRows"];
-                               if (ob != null) return (bool) ob;
+                               if (ob != null)
+                                       return (bool) ob;
                                return true;
                        }
                        set {
@@ -440,8 +448,9 @@ namespace System.Web.UI.WebControls
                public virtual string Caption {
                        get {
                                object ob = ViewState ["Caption"];
-                               if (ob != null) return (string) ob;
-                               return string.Empty;
+                               if (ob != null)
+                                       return (string) ob;
+                               return String.Empty;
                        }
                        set {
                                ViewState ["Caption"] = value;
@@ -451,11 +460,11 @@ namespace System.Web.UI.WebControls
                
                [WebCategoryAttribute ("Accessibility")]
                [DefaultValueAttribute (TableCaptionAlign.NotSet)]
-               public virtual TableCaptionAlign CaptionAlign
-               {
+               public virtual TableCaptionAlign CaptionAlign {
                        get {
                                object o = ViewState ["CaptionAlign"];
-                               if(o != null) return (TableCaptionAlign) o;
+                               if(o != null)
+                                       return (TableCaptionAlign) o;
                                return TableCaptionAlign.NotSet;
                        }
                        set {
@@ -466,30 +475,24 @@ namespace System.Web.UI.WebControls
 
                [WebCategoryAttribute ("Layout")]
                [DefaultValueAttribute (-1)]
-               public virtual int CellPadding
-               {
+               public virtual int CellPadding {
                        get {
                                if (ControlStyleCreated)
                                        return ((TableStyle) ControlStyle).CellPadding;
                                return -1;
                        }
-                       set {
-                               ((TableStyle) ControlStyle).CellPadding = value;
-                       }
+                       set { ((TableStyle) ControlStyle).CellPadding = value; }
                }
 
                [WebCategoryAttribute ("Layout")]
                [DefaultValueAttribute (0)]
-               public virtual int CellSpacing
-               {
+               public virtual int CellSpacing {
                        get {
                                if (ControlStyleCreated)
                                        return ((TableStyle) ControlStyle).CellSpacing;
                                return 0;
                        }
-                       set {
-                               ((TableStyle) ControlStyle).CellSpacing = value;
-                       }
+                       set { ((TableStyle) ControlStyle).CellSpacing = value; }
                }
                
                [DefaultValueAttribute (null)]
@@ -511,9 +514,8 @@ namespace System.Web.UI.WebControls
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
                [BrowsableAttribute (false)]
                public DetailsViewMode CurrentMode {
-                       get {
-                               return hasCurrentMode ? currentMode : DefaultMode;
-                       }
+                       get { return hasCurrentMode ? currentMode : DefaultMode; }
+
                        private set {
                                hasCurrentMode = true;
                                currentMode = value;
@@ -524,9 +526,7 @@ namespace System.Web.UI.WebControls
                [DefaultValueAttribute (DetailsViewMode.ReadOnly)]
                [WebCategoryAttribute ("Behavior")]
                public virtual DetailsViewMode DefaultMode {
-                       get {
-                               return defaultMode;
-                       }
+                       get { return defaultMode; }
                        set {
                                defaultMode = value;
                                RequireBinding ();
@@ -556,8 +556,7 @@ namespace System.Web.UI.WebControls
                [WebCategoryAttribute ("Data")]
                [TypeConverter (typeof(StringArrayConverter))]
                [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
-               public virtual string[] DataKeyNames
-               {
+               public virtual string[] DataKeyNames {
                        get {
                                if (dataKeyNames == null)
                                        return emptyKeys;
@@ -572,9 +571,8 @@ namespace System.Web.UI.WebControls
                
                IOrderedDictionary KeyTable {
                        get {
-                               if (_keyTable == null) {
+                               if (_keyTable == null)
                                        _keyTable = new OrderedDictionary (DataKeyNames.Length);
-                               }
                                return _keyTable;
                        }
                }
@@ -583,18 +581,16 @@ namespace System.Web.UI.WebControls
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
                public virtual DataKey DataKey {
                        get {
-                               if (key == null) {
+                               if (key == null)
                                        key = new DataKey (KeyTable);
-                               }
                                return key;
                        }
                }
 
                DataKey OldEditValues {
                        get {
-                               if (oldEditValues == null) {
+                               if (oldEditValues == null)
                                        oldEditValues = new DataKey (new OrderedDictionary ());
-                               }
                                return oldEditValues;
                        }
                }
@@ -646,8 +642,9 @@ namespace System.Web.UI.WebControls
                public virtual string EmptyDataText {
                        get {
                                object ob = ViewState ["EmptyDataText"];
-                               if (ob != null) return (string) ob;
-                               return string.Empty;
+                               if (ob != null)
+                                       return (string) ob;
+                               return String.Empty;
                        }
                        set {
                                ViewState ["EmptyDataText"] = value;
@@ -660,7 +657,8 @@ namespace System.Web.UI.WebControls
                public virtual bool EnablePagingCallbacks {
                        get {
                                object ob = ViewState ["EnablePagingCallbacks"];
-                               if (ob != null) return (bool) ob;
+                               if (ob != null)
+                                       return (bool) ob;
                                return false;
                        }
                        set {
@@ -709,8 +707,9 @@ namespace System.Web.UI.WebControls
                public virtual string FooterText {
                        get {
                                object ob = ViewState ["FooterText"];
-                               if (ob != null) return (string) ob;
-                               return string.Empty;
+                               if (ob != null)
+                                       return (string) ob;
+                               return String.Empty;
                        }
                        set {
                                ViewState ["FooterText"] = value;
@@ -742,9 +741,7 @@ namespace System.Web.UI.WebControls
                                        return ((TableStyle) ControlStyle).GridLines;
                                return GridLines.Both;
                        }
-                       set {
-                               ((TableStyle) ControlStyle).GridLines = value;
-                       }
+                       set { ((TableStyle) ControlStyle).GridLines = value; }
                }
 
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
@@ -787,8 +784,9 @@ namespace System.Web.UI.WebControls
                public virtual string HeaderText {
                        get {
                                object ob = ViewState ["HeaderText"];
-                               if (ob != null) return (string) ob;
-                               return string.Empty;
+                               if (ob != null)
+                                       return (string) ob;
+                               return String.Empty;
                        }
                        set {
                                ViewState ["HeaderText"] = value;
@@ -804,9 +802,7 @@ namespace System.Web.UI.WebControls
                                        return ((TableStyle) ControlStyle).HorizontalAlign;
                                return HorizontalAlign.NotSet;
                        }
-                       set {
-                               ((TableStyle) ControlStyle).HorizontalAlign = value;
-                       }
+                       set { ((TableStyle) ControlStyle).HorizontalAlign = value; }
                }
 
                [WebCategoryAttribute ("Styles")]
@@ -828,12 +824,8 @@ namespace System.Web.UI.WebControls
                [BrowsableAttribute (false)]
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
                public virtual int PageCount {
-                       get {
-                               return pageCount;
-                       }
-                       private set {
-                               pageCount = value;
-                       }
+                       get { return pageCount; }
+                       private set { pageCount = value; }
                }
 
                [WebCategoryAttribute ("Paging")]
@@ -905,6 +897,7 @@ namespace System.Web.UI.WebControls
                }
 
                [BrowsableAttribute(false)]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public IAutoFieldGenerator RowsGenerator {
                        get;
                        set;
@@ -968,7 +961,14 @@ namespace System.Web.UI.WebControls
                int IDataItemContainer.DataItemIndex {
                        get { return DataItemIndex; }
                }
-       
+
+               [MonoTODO ("Make use of it in the code")]
+               [DefaultValue (true)]
+               public virtual bool EnableModelValidation {
+                       get;
+                       set;
+               }
+
                public virtual bool IsBindableType (Type type)
                {
                        return type.IsPrimitive || type == typeof (string) || type == typeof (DateTime) || type == typeof (Guid) || type == typeof (Decimal);
@@ -983,10 +983,8 @@ namespace System.Web.UI.WebControls
                                if (view.CanRetrieveTotalRowCount) {
                                        arg.RetrieveTotalRowCount = true;
                                        arg.MaximumRows = 1;
-                               }
-                               else {
+                               } else
                                        arg.MaximumRows = -1;
-                               }
                        }
                        return arg;
                }
@@ -1047,8 +1045,7 @@ namespace System.Web.UI.WebControls
                
                AutoGeneratedFieldProperties[] CreateAutoFieldProperties (object dataItem)
                {
-                       if (IsBindableType (dataItem.GetType ()))
-                       {
+                       if (IsBindableType (dataItem.GetType ())) {
                                AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
                                ((IStateManager) field).TrackViewState ();
                                field.Name = "Item";
@@ -1095,9 +1092,8 @@ namespace System.Web.UI.WebControls
                
                void RequireBinding ()
                {
-                       if (Initialized) {
+                       if (Initialized)
                                RequiresDataBinding = true;
-                       }
                }
                
                protected virtual Table CreateTable ()
@@ -1113,24 +1109,24 @@ namespace System.Web.UI.WebControls
                        return style;
                }
                
-               protected override int CreateChildControls (IEnumerable data, bool dataBinding)
+               protected override int CreateChildControls (IEnumerable dataSource, bool dataBinding)
                {
-                       PagedDataSource dataSource = new PagedDataSource ();
-                       dataSource.DataSource = CurrentMode != DetailsViewMode.Insert ? data : null;
-                       dataSource.AllowPaging = AllowPaging;
-                       dataSource.PageSize = 1;
-                       dataSource.CurrentPageIndex = PageIndex;
+                       PagedDataSource pagedDataSource = new PagedDataSource ();
+                       pagedDataSource.DataSource = CurrentMode != DetailsViewMode.Insert ? dataSource : null;
+                       pagedDataSource.AllowPaging = AllowPaging;
+                       pagedDataSource.PageSize = 1;
+                       pagedDataSource.CurrentPageIndex = PageIndex;
 
                        if (dataBinding && CurrentMode != DetailsViewMode.Insert) {
                                DataSourceView view = GetData ();
                                if (view != null && view.CanPage) {
-                                       dataSource.AllowServerPaging = true;
+                                       pagedDataSource.AllowServerPaging = true;
                                        if (SelectArguments.RetrieveTotalRowCount)
-                                               dataSource.VirtualCount = SelectArguments.TotalRowCount;
+                                               pagedDataSource.VirtualCount = SelectArguments.TotalRowCount;
                                }
                        }
 
-                       bool showPager = AllowPaging && (dataSource.PageCount > 1);
+                       bool showPager = AllowPaging && (pagedDataSource.PageCount > 1);
 
                        Controls.Clear ();
                        table = CreateTable ();
@@ -1144,21 +1140,19 @@ namespace System.Web.UI.WebControls
                        // Gets the current data item
 
                        if (AllowPaging) {
-                               PageCount = dataSource.DataSourceCount;
-                               if (PageIndex >= PageCount && PageCount > 0) {
-                                       pageIndex = dataSource.CurrentPageIndex = PageCount - 1;
-                               }
-                               if (dataSource.DataSource != null) {
-                                       IEnumerator e = dataSource.GetEnumerator ();
+                               PageCount = pagedDataSource.DataSourceCount;
+                               if (PageIndex >= PageCount && PageCount > 0)
+                                       pageIndex = pagedDataSource.CurrentPageIndex = PageCount - 1;
+                               if (pagedDataSource.DataSource != null) {
+                                       IEnumerator e = pagedDataSource.GetEnumerator ();
                                        if (e.MoveNext ())
                                                dataItem = e.Current;
                                }
-                       }
-                       else {
+                       } else {
                                int page = 0;
                                object lastItem = null;
-                               if (dataSource.DataSource != null) {
-                                       IEnumerator e = dataSource.GetEnumerator ();
+                               if (pagedDataSource.DataSource != null) {
+                                       IEnumerator e = pagedDataSource.GetEnumerator ();
                                        for (; e.MoveNext (); page++) {
                                                lastItem = e.Current;
                                                if (page == PageIndex)
@@ -1178,9 +1172,7 @@ namespace System.Web.UI.WebControls
                                        table.Rows.Add (row);
                                        list.Add (row);
                                }
-                       }
-                       else {
-
+                       } else {
                                // Creates the set of fields to show
 
                                ICollection fieldCollection = CreateFieldSet (dataItem, dataBinding && dataItem != null);
@@ -1210,7 +1202,7 @@ namespace System.Web.UI.WebControls
                                if (showPager && PagerSettings.Position == PagerPosition.Top ||
                                                PagerSettings.Position == PagerPosition.TopAndBottom) {
                                        topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
-                                       InitializePager (topPagerRow, dataSource);
+                                       InitializePager (topPagerRow, pagedDataSource);
                                        table.Rows.Add (topPagerRow);
                                }
 
@@ -1237,7 +1229,7 @@ namespace System.Web.UI.WebControls
                                if (showPager && PagerSettings.Position == PagerPosition.Bottom ||
                                                PagerSettings.Position == PagerPosition.TopAndBottom) {
                                        bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
-                                       InitializePager (bottomPagerRow, dataSource);
+                                       InitializePager (bottomPagerRow, pagedDataSource);
                                        table.Rows.Add (bottomPagerRow);
                                }
                        }
@@ -1262,20 +1254,21 @@ namespace System.Web.UI.WebControls
                                        MarkAsDataBound ();
                                        OnDataBound (EventArgs.Empty);
                                }
-                       }
-                       else
+                       } else
                                base.EnsureDataBound ();
                }
                
                DataControlRowState GetRowState (int index)
                {
                        DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
-                       if (CurrentMode == DetailsViewMode.Edit) rstate |= DataControlRowState.Edit;
-                       else if (CurrentMode == DetailsViewMode.Insert) rstate |= DataControlRowState.Insert;
+                       if (CurrentMode == DetailsViewMode.Edit)
+                               rstate |= DataControlRowState.Edit;
+                       else if (CurrentMode == DetailsViewMode.Insert)
+                               rstate |= DataControlRowState.Insert;
                        return rstate;
                }
                
-               protected virtual void InitializePager (DetailsViewRow row, PagedDataSource dataSource)
+               protected virtual void InitializePager (DetailsViewRow row, PagedDataSource pagedDataSource)
                {
                        TableCell cell = new TableCell ();
                        cell.ColumnSpan = 2;
@@ -1283,7 +1276,7 @@ namespace System.Web.UI.WebControls
                        if (pagerTemplate != null)
                                pagerTemplate.InstantiateIn (cell);
                        else
-                               cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
+                               cell.Controls.Add (PagerSettings.CreatePagerControl (pagedDataSource.CurrentPageIndex, pagedDataSource.PageCount));
                        
                        row.Cells.Add (cell);
                }
@@ -1356,15 +1349,16 @@ namespace System.Web.UI.WebControls
                        return dic;
                }
                
-               protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeReadOnlyFields, bool includePrimaryKey)
+               protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeReadOnlyFields, bool includeKeys)
                {
                        foreach (DetailsViewRow row in Rows) {
-                               if (row.Cells.Count < 1) continue;
+                               if (row.Cells.Count < 1)
+                                       continue;
                                DataControlFieldCell c = row.Cells[row.Cells.Count-1] as DataControlFieldCell;
                                if (c != null)
                                        c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
                        }
-                       if (!includePrimaryKey && DataKeyNames != null)
+                       if (!includeKeys && DataKeyNames != null)
                                foreach (string key in DataKeyNames)
                                        fieldValues.Remove (key);
                }
@@ -1406,37 +1400,36 @@ namespace System.Web.UI.WebControls
 
                        foreach (DetailsViewRow row in table.Rows) {
                                switch (row.RowType) {
-                               case DataControlRowType.Header:
-                                       if (headerStyle != null && !headerStyle.IsEmpty)
-                                               row.ControlStyle.CopyFrom (headerStyle);
-                                       break;
-                               case DataControlRowType.Footer:
-                                       if (footerStyle != null && !footerStyle.IsEmpty)
-                                               row.ControlStyle.CopyFrom (footerStyle);
-                                       break;
-                               case DataControlRowType.Pager:
-                                       if (pagerStyle != null && !pagerStyle.IsEmpty)
-                                               row.ControlStyle.CopyFrom (pagerStyle);
-                                       break;
-                               case DataControlRowType.EmptyDataRow:
-                                       if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
-                                               row.ControlStyle.CopyFrom (emptyDataRowStyle);
-                                       break;
-                               case DataControlRowType.DataRow:
-                                       if (rowStyle != null && !rowStyle.IsEmpty)
-                                               row.ControlStyle.CopyFrom (rowStyle);
-                                       if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
-                                               row.ControlStyle.CopyFrom (alternatingRowStyle);
-                                       break;
-                               default:
-                                       break;
+                                       case DataControlRowType.Header:
+                                               if (headerStyle != null && !headerStyle.IsEmpty)
+                                                       row.ControlStyle.CopyFrom (headerStyle);
+                                               break;
+                                       case DataControlRowType.Footer:
+                                               if (footerStyle != null && !footerStyle.IsEmpty)
+                                                       row.ControlStyle.CopyFrom (footerStyle);
+                                               break;
+                                       case DataControlRowType.Pager:
+                                               if (pagerStyle != null && !pagerStyle.IsEmpty)
+                                                       row.ControlStyle.CopyFrom (pagerStyle);
+                                               break;
+                                       case DataControlRowType.EmptyDataRow:
+                                               if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
+                                                       row.ControlStyle.CopyFrom (emptyDataRowStyle);
+                                               break;
+                                       case DataControlRowType.DataRow:
+                                               if (rowStyle != null && !rowStyle.IsEmpty)
+                                                       row.ControlStyle.CopyFrom (rowStyle);
+                                               if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
+                                                       row.ControlStyle.CopyFrom (alternatingRowStyle);
+                                               break;
+                                       default:
+                                               break;
                                }
 
                                if (row.ContainingField is CommandField) {
                                        if (commandRowStyle != null && !commandRowStyle.IsEmpty)
                                                row.ControlStyle.CopyFrom (commandRowStyle);
-                               }
-                               else {
+                               } else {
                                        if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
                                                row.ControlStyle.CopyFrom (editRowStyle);
                                        if ((row.RowState & DataControlRowState.Insert) != 0) {
@@ -1456,14 +1449,15 @@ namespace System.Web.UI.WebControls
                                                                fcell.ControlStyle.CopyFrom (fieldHeaderStyle);
                                                        if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
                                                                fcell.ControlStyle.CopyFrom (field.HeaderStyle);
-                                               }
-                                               else {
-                                                       if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty)
+                                               } else {
+                                                       if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty) {
                                                                foreach (Control c in fcell.Controls) {
                                                                        WebControl wc = c as WebControl;
                                                                        if (wc != null)
                                                                                wc.ControlStyle.MergeWith (field.ControlStyle);
                                                                }
+                                                       }
+                                                       
                                                        if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
                                                                fcell.ControlStyle.CopyFrom (field.ItemStyle);
                                                }
@@ -1474,7 +1468,9 @@ namespace System.Web.UI.WebControls
                
                protected internal override void OnInit (EventArgs e)
                {
-                       Page.RegisterRequiresControlState (this);
+                       Page page = Page;
+                       if (page != null)
+                               page.RegisterRequiresControlState (this);
                        base.OnInit (e);
                }
                
@@ -1530,79 +1526,78 @@ namespace System.Web.UI.WebControls
 
                void ProcessEvent (string eventName, string param, bool causesValidation)
                {
-                       switch (eventName)
-                       {
-                       case DataControlCommands.PageCommandName:
-                               int newIndex = -1;
-                               switch (param) {
+                       switch (eventName) {
+                               case DataControlCommands.PageCommandName:
+                                       int newIndex = -1;
+                                       switch (param) {
+                                               case DataControlCommands.FirstPageCommandArgument:
+                                                       newIndex = 0;
+                                                       break;
+                                               case DataControlCommands.LastPageCommandArgument:
+                                                       newIndex = PageCount - 1;
+                                                       break;
+                                               case DataControlCommands.NextPageCommandArgument:
+                                                       newIndex = PageIndex + 1;
+                                                       break;
+                                               case DataControlCommands.PreviousPageCommandArgument:
+                                                       newIndex = PageIndex - 1;
+                                                       break;
+                                               default:
+                                                       int paramIndex = 0;
+                                                       int.TryParse (param, out paramIndex);
+                                                       newIndex = paramIndex - 1;
+                                                       break;
+                                       }
+                                       SetPageIndex (newIndex);
+                                       break;
+                                       
                                case DataControlCommands.FirstPageCommandArgument:
-                                       newIndex = 0;
+                                       SetPageIndex (0);
                                        break;
+
                                case DataControlCommands.LastPageCommandArgument:
-                                       newIndex = PageCount - 1;
+                                       SetPageIndex (PageCount - 1);
                                        break;
+                                       
                                case DataControlCommands.NextPageCommandArgument:
-                                       newIndex = PageIndex + 1;
+                                       if (PageIndex < PageCount - 1)
+                                               SetPageIndex (PageIndex + 1);
                                        break;
+
                                case DataControlCommands.PreviousPageCommandArgument:
-                                       newIndex = PageIndex - 1;
-                                       break;
-                               default:
-                                       int paramIndex = 0;
-                                       int.TryParse (param, out paramIndex);
-                                       newIndex = paramIndex - 1;
+                                       if (PageIndex > 0)
+                                               SetPageIndex (PageIndex - 1);
                                        break;
-                               }
-                               ShowPage (newIndex);
-                               break;
-                                       
-                       case DataControlCommands.FirstPageCommandArgument:
-                               ShowPage (0);
-                               break;
-
-                       case DataControlCommands.LastPageCommandArgument:
-                               ShowPage (PageCount - 1);
-                               break;
                                        
-                       case DataControlCommands.NextPageCommandArgument:
-                               if (PageIndex < PageCount - 1)
-                                       ShowPage (PageIndex + 1);
-                               break;
-
-                       case DataControlCommands.PreviousPageCommandArgument:
-                               if (PageIndex > 0)
-                                       ShowPage (PageIndex - 1);
-                               break;
-                                       
-                       case DataControlCommands.EditCommandName:
-                               ProcessChangeMode (DetailsViewMode.Edit);
-                               break;
+                               case DataControlCommands.EditCommandName:
+                                       ProcessChangeMode (DetailsViewMode.Edit);
+                                       break;
                                        
-                       case DataControlCommands.NewCommandName:
-                               ProcessChangeMode (DetailsViewMode.Insert);
-                               break;
+                               case DataControlCommands.NewCommandName:
+                                       ProcessChangeMode (DetailsViewMode.Insert);
+                                       break;
                                        
-                       case DataControlCommands.UpdateCommandName:
-                               UpdateItem (param, causesValidation);
-                               break;
+                               case DataControlCommands.UpdateCommandName:
+                                       UpdateItem (param, causesValidation);
+                                       break;
                                        
-                       case DataControlCommands.CancelCommandName:
-                               CancelEdit ();
-                               break;
+                               case DataControlCommands.CancelCommandName:
+                                       CancelEdit ();
+                                       break;
                                        
-                       case DataControlCommands.DeleteCommandName:
-                               DeleteItem ();
-                               break;
+                               case DataControlCommands.DeleteCommandName:
+                                       DeleteItem ();
+                                       break;
                                        
-                       case DataControlCommands.InsertCommandName:
-                               InsertItem (causesValidation);
-                               break;
+                               case DataControlCommands.InsertCommandName:
+                                       InsertItem (causesValidation);
+                                       break;
                        }
                }
-               
-               void ShowPage (int newIndex)
+               public
+               void SetPageIndex (int index)
                {
-                       DetailsViewPageEventArgs args = new DetailsViewPageEventArgs (newIndex);
+                       DetailsViewPageEventArgs args = new DetailsViewPageEventArgs (index);
                        OnPageIndexChanging (args);
 
                        if (args.Cancel || !IsBoundUsingDataSourceID)
@@ -1655,7 +1650,8 @@ namespace System.Web.UI.WebControls
                        if (causesValidation && Page != null && !Page.IsValid)
                                return;
                        
-                       if (CurrentMode != DetailsViewMode.Edit) throw new HttpException ();
+                       if (CurrentMode != DetailsViewMode.Edit)
+                               throw new HttpException ();
 
                        currentEditOldValues = OldEditValues.Values;
 
@@ -1695,7 +1691,8 @@ namespace System.Web.UI.WebControls
                        if (causesValidation && Page != null && !Page.IsValid)
                                return;
                        
-                       if (CurrentMode != DetailsViewMode.Insert) throw new HttpException ();
+                       if (CurrentMode != DetailsViewMode.Insert)
+                               throw new HttpException ();
                        
                        currentEditNewValues = GetRowValues (false, true);
                        DetailsViewInsertEventArgs args = new DetailsViewInsertEventArgs (param, currentEditNewValues);
@@ -1756,7 +1753,8 @@ namespace System.Web.UI.WebControls
                        EndRowEdit (true);
                }
 
-               void EndRowEdit (bool switchToDefaultMode) {
+               void EndRowEdit (bool switchToDefaultMode)
+               {
                        if (switchToDefaultMode)
                                ChangeMode (DefaultMode);
                        oldEditValues = new DataKey (new OrderedDictionary ());
@@ -1766,10 +1764,11 @@ namespace System.Web.UI.WebControls
                        RequireBinding ();
                }
 
-               protected internal override void LoadControlState (object ob)
+               protected internal override void LoadControlState (object savedState)
                {
-                       if (ob == null) return;
-                       object[] state = (object[]) ob;
+                       if (savedState == null)
+                               return;
+                       object[] state = (object[]) savedState;
                        base.LoadControlState (state[0]);
                        pageIndex = (int) state[1];
                        pageCount = (int) state[2];
@@ -1794,23 +1793,34 @@ namespace System.Web.UI.WebControls
                                defaultMode,
                                (key == null ? null : ((IStateManager)key).SaveViewState()),
                                (oldEditValues == null ? null : ((IStateManager) oldEditValues).SaveViewState ())
-                                       };
+                       };
                }
                
                protected override void TrackViewState()
                {
                        base.TrackViewState();
-                       if (columns != null) ((IStateManager)columns).TrackViewState();
-                       if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
-                       if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
-                       if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
-                       if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
-                       if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
-                       if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
-                       if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
-                       if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
-                       if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
-                       if (key != null) ((IStateManager)key).TrackViewState();
+                       if (columns != null)
+                               ((IStateManager)columns).TrackViewState();
+                       if (pagerSettings != null)
+                               ((IStateManager)pagerSettings).TrackViewState();
+                       if (alternatingRowStyle != null)
+                               ((IStateManager)alternatingRowStyle).TrackViewState();
+                       if (footerStyle != null)
+                               ((IStateManager)footerStyle).TrackViewState();
+                       if (headerStyle != null)
+                               ((IStateManager)headerStyle).TrackViewState();
+                       if (pagerStyle != null)
+                               ((IStateManager)pagerStyle).TrackViewState();
+                       if (rowStyle != null)
+                               ((IStateManager)rowStyle).TrackViewState();
+                       if (editRowStyle != null)
+                               ((IStateManager)editRowStyle).TrackViewState();
+                       if (insertRowStyle != null)
+                               ((IStateManager)insertRowStyle).TrackViewState();
+                       if (emptyDataRowStyle != null)
+                               ((IStateManager)emptyDataRowStyle).TrackViewState();
+                       if (key != null)
+                               ((IStateManager)key).TrackViewState();
                        if (autoFieldProperties != null) {
                                foreach (IStateManager sm in autoFieldProperties)
                                        sm.TrackViewState ();
@@ -1890,14 +1900,14 @@ namespace System.Web.UI.WebControls
                                ControlStyle.LoadViewState (states [12]);
                }
                
-               void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
+               void ICallbackEventHandler.RaiseCallbackEvent (string eventArgument)
                {
-                       RaiseCallbackEvent (eventArgs);
+                       RaiseCallbackEvent (eventArgument);
                }
                
-               protected virtual void RaiseCallbackEvent (string eventArgs)
+               protected virtual void RaiseCallbackEvent (string eventArgument)
                {
-                       string[] clientData = eventArgs.Split ('|');
+                       string[] clientData = eventArgument.Split ('|');
                        PageIndex = int.Parse (clientData[0]);
                        
                        RaisePostBackEvent (clientData[1]);
@@ -1953,31 +1963,31 @@ namespace System.Web.UI.WebControls
 {0}.uid = {2};
 {0}.form = {3};
 ";
-
                protected internal override void OnPreRender (EventArgs e)
                {
                        base.OnPreRender (e);
 
-                       if (EnablePagingCallbacks)
-                       {
-                               if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(DetailsView), "DetailsView.js")) {
-                                       string url = Page.ClientScript.GetWebResourceUrl (typeof(DetailsView), "DetailsView.js");
-                                       Page.ClientScript.RegisterClientScriptInclude (typeof(DetailsView), "DetailsView.js", url);
+                       Page page = Page;
+                       if (EnablePagingCallbacks && page != null) {
+                               ClientScriptManager scriptManager = page.ClientScript;
+                               if (!scriptManager.IsClientScriptIncludeRegistered (typeof(DetailsView), "DetailsView.js")) {
+                                       string url = scriptManager.GetWebResourceUrl (typeof(DetailsView), "DetailsView.js");
+                                       scriptManager.RegisterClientScriptInclude (typeof(DetailsView), "DetailsView.js", url);
                                }
-                               Page.ClientScript.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
+                               scriptManager.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
                                
                                string cgrid = ClientID + "_data";
                                string script = String.Format (onPreRenderScript,
                                                               cgrid,
                                                               ClientScriptManager.GetScriptLiteral (PageIndex),
                                                               ClientScriptManager.GetScriptLiteral (UniqueID),
-                                                              Page.theForm);
+                                                              page.theForm);
                                
-                               Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
+                               scriptManager.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
                                
                                // Make sure the basic script infrastructure is rendered
-                               Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
-                               Page.ClientScript.GetPostBackClientHyperlink (this, String.Empty, true);
+                               scriptManager.GetCallbackEventReference (this, "null", String.Empty, "null");
+                               scriptManager.GetPostBackClientHyperlink (this, String.Empty, true);
                        }
                }
                
@@ -2018,5 +2028,3 @@ namespace System.Web.UI.WebControls
                }
        }
 }
-
-#endif