2010-02-18 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / GridView.cs
index f504d0cb27e7cf93e4eb0c8fefc46c1b404c7bb3..eac5c285e924b1e251f59b73edba373e8fd1e40e 100644 (file)
@@ -46,7 +46,7 @@ namespace System.Web.UI.WebControls
        [DefaultEventAttribute ("SelectedIndexChanged")]
        [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
-       public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IPostBackEventHandler, IPostBackContainer
+       public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IPostBackEventHandler, IPostBackContainer, IPersistedSelector
        {
                Table table;
                GridViewRowCollection rows;
@@ -82,21 +82,21 @@ namespace System.Web.UI.WebControls
                readonly string[] emptyKeys = new string[0];
                IEnumerator _dataEnumerator;
                
-               private static readonly object PageIndexChangedEvent = new object();
-               private static readonly object PageIndexChangingEvent = new object();
-               private static readonly object RowCancelingEditEvent = new object();
-               private static readonly object RowCommandEvent = new object();
-               private static readonly object RowCreatedEvent = new object();
-               private static readonly object RowDataBoundEvent = new object();
-               private static readonly object RowDeletedEvent = new object();
-               private static readonly object RowDeletingEvent = new object();
-               private static readonly object RowEditingEvent = new object();
-               private static readonly object RowUpdatedEvent = new object();
-               private static readonly object RowUpdatingEvent = new object();
-               private static readonly object SelectedIndexChangedEvent = new object();
-               private static readonly object SelectedIndexChangingEvent = new object();
-               private static readonly object SortedEvent = new object();
-               private static readonly object SortingEvent = new object();
+               static readonly object PageIndexChangedEvent = new object();
+               static readonly object PageIndexChangingEvent = new object();
+               static readonly object RowCancelingEditEvent = new object();
+               static readonly object RowCommandEvent = new object();
+               static readonly object RowCreatedEvent = new object();
+               static readonly object RowDataBoundEvent = new object();
+               static readonly object RowDeletedEvent = new object();
+               static readonly object RowDeletingEvent = new object();
+               static readonly object RowEditingEvent = new object();
+               static readonly object RowUpdatedEvent = new object();
+               static readonly object RowUpdatingEvent = new object();
+               static readonly object SelectedIndexChangedEvent = new object();
+               static readonly object SelectedIndexChangingEvent = new object();
+               static readonly object SortedEvent = new object();
+               static readonly object SortingEvent = new object();
                
                // Control state
                int pageIndex;
@@ -543,6 +543,12 @@ namespace System.Web.UI.WebControls
                        }
                }
 
+               [BrowsableAttribute(false)]
+               public IAutoFieldGenerator ColumnsGenerator {
+                       get;
+                       set;
+               }
+
                [DefaultValueAttribute (null)]
                [WebCategoryAttribute ("Data")]
                [TypeConverter (typeof(StringArrayConverter))]
@@ -918,6 +924,18 @@ namespace System.Web.UI.WebControls
                                        return null;
                        }
                }
+
+               [MonoTODO]
+               [BrowsableAttribute(false)]
+               public virtual DataKey SelectedPersistedDataKey {
+                       get; set;
+               }
+
+               [MonoTODO]
+               DataKey IPersistedSelector.DataKey {
+                       get { return SelectedPersistedDataKey; }
+                       set { SelectedPersistedDataKey = value; }
+               }
                
                [BindableAttribute (true)]
                [DefaultValueAttribute (-1)]
@@ -1097,6 +1115,14 @@ namespace System.Web.UI.WebControls
                
                protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
                {
+                       bool autoGenerate = AutoGenerateColumns;
+
+                       if (autoGenerate) {
+                               IAutoFieldGenerator fieldGenerator = ColumnsGenerator;
+                               if (fieldGenerator != null)
+                                       return fieldGenerator.GenerateFields (this);
+                       }
+                       
                        ArrayList fields = new ArrayList ();
                        
                        if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
@@ -1109,7 +1135,7 @@ namespace System.Web.UI.WebControls
 
                        fields.AddRange (Columns);
                        
-                       if (AutoGenerateColumns) {
+                       if (autoGenerate) {
                                if (useDataSource)
                                        autoFieldProperties = CreateAutoFieldProperties (dataSource);
        
@@ -1271,13 +1297,16 @@ namespace System.Web.UI.WebControls
 
                        _dataEnumerator = null;
                        ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
-                       DataControlField[] fields = new DataControlField [fieldCollection.Count];
+                       int fieldCount = fieldCollection.Count;
+                       DataControlField dcf;
+                       DataControlField[] fields = new DataControlField [fieldCount];
                        fieldCollection.CopyTo (fields, 0);
-
-                       foreach (DataControlField field in fields) {
-                               field.Initialize (AllowSorting, this);
+                       
+                       for (int i = 0; i < fieldCount; i++) {
+                               dcf = fields [i];
+                               dcf.Initialize (AllowSorting, this);
                                if (EnableSortingAndPagingCallbacks)
-                                       field.ValidateSupportsCallback ();
+                                       dcf.ValidateSupportsCallback ();
                        }
 
                        bool skip_first = false;
@@ -1298,7 +1327,7 @@ namespace System.Web.UI.WebControls
                                
                                if (list.Count == 0) {
                                        if (createPager && (PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom)) {
-                                               topPagerRow = CreatePagerRow (fields.Length, dataSource);
+                                               topPagerRow = CreatePagerRow (fieldCount, dataSource);
                                                OnRowCreated (new GridViewRowEventArgs (topPagerRow));
                                                ContainedTable.Rows.Add (topPagerRow);
                                                if (dataBinding) {
@@ -1327,16 +1356,16 @@ namespace System.Web.UI.WebControls
                                OnRowCreated (new GridViewRowEventArgs (row));
                                ContainedTable.Rows.Add (row);
                                if (dataBinding) {
-                                       row.DataBind ();
-                                       OnRowDataBound (new GridViewRowEventArgs (row));
+                                       row.DataBind ();                                        
                                        if (EditIndex == row.RowIndex)
                                                oldEditValues = new DataKey (GetRowValues (row, true, true));
                                        DataKeyArrayList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
+                                       OnRowDataBound (new GridViewRowEventArgs (row));
                                } 
                        }
 
                        if (list.Count == 0) {
-                               GridViewRow emptyRow = CreateEmptyrRow (fields.Length);
+                               GridViewRow emptyRow = CreateEmptyrRow (fieldCount);
                                if (emptyRow != null) {
                                        OnRowCreated (new GridViewRowEventArgs (emptyRow));
                                        ContainedTable.Rows.Add (emptyRow);
@@ -1358,7 +1387,7 @@ namespace System.Web.UI.WebControls
                                }
 
                                if (createPager && (PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom)) {
-                                       bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
+                                       bottomPagerRow = CreatePagerRow (fieldCount, dataSource);
                                        OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
                                        ContainedTable.Rows.Add (bottomPagerRow);
                                        if (dataBinding) {
@@ -1672,6 +1701,7 @@ namespace System.Web.UI.WebControls
                
                void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
                {
+                       ValidateEvent (UniqueID, eventArgument);
                        RaisePostBackEvent (eventArgument);
                }
 
@@ -1847,9 +1877,8 @@ namespace System.Web.UI.WebControls
                        if (causesValidation && Page != null && !Page.IsValid)
                                return;
 
-                       currentEditOldValues = OldEditValues.Values;
-
-                       currentEditRowKeys = DataKeys [rowIndex].Values;
+                       currentEditOldValues = CopyOrderedDictionary (OldEditValues.Values);
+                       currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
                        currentEditNewValues = GetRowValues (row, false, false);
                        
                        GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (rowIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
@@ -1864,6 +1893,14 @@ namespace System.Web.UI.WebControls
                        view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
                }
 
+               static IOrderedDictionary CopyOrderedDictionary (IOrderedDictionary sourceDic) {
+                       OrderedDictionary copyDic = new OrderedDictionary ();
+                       foreach (object key in sourceDic.Keys) {
+                               copyDic.Add (key, sourceDic [key]);
+                       }
+                       return copyDic;
+               }
+
                bool UpdateCallback (int recordsAffected, Exception exception)
                {
                        GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
@@ -1878,7 +1915,7 @@ namespace System.Web.UI.WebControls
                public virtual void DeleteRow (int rowIndex)
                {
                        GridViewRow row = Rows [rowIndex];
-                       currentEditRowKeys = DataKeys [rowIndex].Values;
+                       currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
                        currentEditNewValues = GetRowValues (row, true, true);
                        
                        GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
@@ -2101,9 +2138,12 @@ namespace System.Web.UI.WebControls
                
                protected virtual string GetCallbackScript (IButtonControl control, string argument)
                {
-                       if (EnableSortingAndPagingCallbacks)
+                       if (EnableSortingAndPagingCallbacks) {
+                               Page page = Page;
+                               if (page != null)
+                                       page.ClientScript.RegisterForEventValidation (UniqueID, argument);
                                return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
-                       else
+                       else
                                return null;
                }