The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

EsDataSource with AutoSort=true Prevents updates for Serialized Collection Saved in ViewState

Last post 03-28-2008, 8:17 AM by jeffdev. 2 replies.
Sort Posts: Previous Next
  •  03-27-2008, 9:13 PM 8598

    EsDataSource with AutoSort=true Prevents updates for Serialized Collection Saved in ViewState

    I have a grid displaying a collection.  When you click on one of the items in the grid, you can edit the item and store it back in the grid.

    I line up all of these changes and store them all back to the data store in one shot when the submit button is pressed.  To do this, I persist the collection across postbacks using an entity spaces proxy class and serializing them into the viewstate.

    In effect, my code works like this.  The first time the page is loaded, I load the collection and store it in a variable (this is the variable that gets persisted as described above) and this is the source for e.collection of my EsDataSource.  On postbacks, I retrieve the collection from the viewstate rather than loading it again from the data store.  This means that the collection EsDataSource is the one with the user's changes.

    All this works fine, but only when I have AutoSorting="false".  If I set it to "true", my collection seems to revert back to its old values (before changes were made) on each and every post back.

    When AutoSorting="true" are you storing the collection in the viewstate too?  Does this in some way wind up overwriting my changes?  Is there some way to prevent that?

     

    Code:
    protected override object SaveControlState()
    {
                //Persist data across postbacks
                object[] state = new object[1];
    
                if (_itemHistory != null)
                {
                    ItemHistoryCollectionProxyStub itemHistoryProxy = new ItemHistoryCollectionProxyStub(_itemHistory);
                    XmlSerializer itemSerializer = new XmlSerializer(itemHistoryProxy.GetType());
                    StringWriter itemSW = new StringWriter();
                    itemSerializer.Serialize(itemSW, itemHistoryProxy);
                    state[0] = itemSW.ToString();
                }
    
                return state;
    }
    protected override void LoadControlState(object savedState)
    {
                //Retrieve data across postbacks
                object[] state = (object[])savedState;
    
                if (state[0] != null)
                {
                    XmlSerializer itemSerializer = new XmlSerializer(typeof(ItemHistoryCollectionProxyStub));
                    StringReader itemSR = new StringReader(state[0].ToString());
                    ItemHistoryCollectionProxyStub itemHistoryProxy = (ItemHistoryCollectionProxyStub)itemSerializer.Deserialize(itemSR);
                    _itemHistory = itemHistoryProxy.GetCollection(); //Convert it back to the collection.
                }
     }
    
    protected void EsDataSource_ItemHistory_esSelect(object sender, esDataSourceSelectEventArgs e)
    {
            if (_itemHistory != null)
            {
                    e.Collection = _itemHistory;
            }
            else
            {
                    _itemHistory = new ItemHistoryCollection().LoadAll();
            }
    }
  •  03-28-2008, 4:50 AM 8599 in reply to 8598

    Re: EsDataSource with AutoSort=true Prevents updates for Serialized Collection Saved in ViewState

    When either AutoSorting or AutoPaging are true esDataSource will call Load() on your collection/query, that is because it sets the OrderBy and the es.PageSize and es.PageNumber properties, in order for "Auto" anythings to work these things have to be set before Load() is called. In order for you to do the serializing and such you will have to turn AutoLoad off. If it's any help I have done 5 years of ASP.NET and have never stored objects in the Session or ViewState and always refetch them. However, what you are doing should be fine it's just that AutoPaging and AutoSorting tell the esDataSource to handle these "things" by setting up the query appropriately and then calling Load().

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  03-28-2008, 8:17 AM 8608 in reply to 8599

    Re: EsDataSource with AutoSort=true Prevents updates for Serialized Collection Saved in ViewState

    Hi Mike,

    I understand.  I decided to bypass the esDataSource and bind to the collection directly.  I then handled the sort event of my grid and set the Sort property on the collection directly.  Seems to work.

    I agree with you, most times refetching the objects is the way to go.  In this one instance though I didn't want to save any of the changes to the collection until the user saved everything else on the page.

View as RSS news feed in XML