Save batch edits on page change or sort

7 posts, 0 answers
  1. Stephen
    Stephen avatar
    2 posts
    Member since:
    Aug 2015

    Posted 27 Aug 2015 Link to this post

    I have been banging my head on this for a couple days. I have a simple radgrid in which I am doing batch editing. The problem I have is that when I change the page or sort a column any changes made are lost. I want the changes to automatically be saved via the BatchEditCommand server-side event before the page flip or sort occurs.  I have subscribed to the RadGrid's OnCommand client side event and the function is below. 

    The javascript function is being executed properly when I page or sort and everything seems fine at each point when debugging. The server side function--BatchEditcommand--functions properly, but it is simply not being executed from this point.

    I know the grid.get_batchEditingManager().saveChanges(grid.get_masterTableView()); is being executed because this function runs again immediately and the args.get_commandName() returns "BatchEdit" as it should.  

    The only way I can get the BatchEditCommand server side event to fire is if i put a args.set_cancel(true); after the grid.saveChanges in the javascript, but then that cancels the paging or sorting action which I want to still happen after saving the changed data. I feel like this should work since the paging action and the saving action both cause postback anyway.

     Any ideas?

    Thanks.

    function UserAction(sender, args) {       
        var grid = $find("<%= grdWaterUses.ClientID %>");     if (args.get_commandName() == "Sort" || args.get_commandName() == "Page") {         if (grid.get_batchEditingManager().hasChanges(grid.get_masterTableView())) {           
              document.getElementById("<%= hiddenPostbackCommand.ClientID %>").value = "ClientSave";           grid.get_batchEditingManager().saveChanges(grid.get_masterTableView());         
            }       
        }     
    }

  2. Eyup
    Admin
    Eyup avatar
    3986 posts

    Posted 01 Sep 2015 Link to this post

    Hello Stephen,

    The problem here is that the two actions, both Batch save command and Sort command, cause postback. Therefore, you need to execute only one command on the client and re-initiate the second command on the server. Alternatively, you can display a warning message to your users, similar to this implementation:
    http://demos.telerik.com/aspnet-ajax/grid/examples/data-binding/client-side/client-data-source-binding/defaultcs.aspx?product=clientdatasource&show-source=true

    The first approach can be achieved as follows:
    1. Determine on client-side UserAction event handler whether the initiated command is Sort or Paging and cancel the command using args.set_cancel(true).
    2. Then fire your saveChanges() method.
    3. After saving the changes to your database on the server, at the end of the BatchEditCommand event handler you can call a method to initiate a paging or sorting action:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/control-lifecycle/how-to-fire-command-events

    Hope this helps.

    Regards,
    Eyup
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Marcus
    Marcus avatar
    3 posts
    Member since:
    Jul 2018

    Posted 18 Jul 2018 in reply to Eyup Link to this post

    Hello Eyup, 

     

    How do I use this code if I have PagerTemplate as well?

    I tried using your code without PagerTemplate and it works, but am I able to use PagerTemplate and make it work at the same time?

     

    Regards,

    Marcus

  4. Eyup
    Admin
    Eyup avatar
    3986 posts

    Posted 22 Jul 2018 Link to this post

    Hello Marcus,

    Since you are using custom template, you have greater control over this behavior. Just add a confirmation logic in the client-side event handlers of the buttons before calling the saveChanges, addNewRecord, etc. batch API methods:
    https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/edit-mode/batch-editing/client-side-api

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  5. Marcus
    Marcus avatar
    3 posts
    Member since:
    Jul 2018

    Posted 23 Jul 2018 in reply to Eyup Link to this post

    Hi Eyrup,

     

    Sorry, I will try to be more specific with my questioning. 

    What I am trying to achieve is to prompt the user that changes will not be saved if the next page is clicked, however, the code as shown below doesn't work with PagerTemplate tag.

    function UserAction(sender, args) {
                if (sender.get_batchEditingManager().hasChanges(sender.get_masterTableView()) &&
                    !confirm("Any changes will be cleared. Are you sure you want to perform this action?")) {
                    args.set_cancel(true);

    }

    }

    Hope you could shed some light onto this issue as it seems with the PagerTemplate, ClientEvents are unable to call OnUserAction.

     

    Thanks & Regards,

    Marcus

  6. Eyup
    Admin
    Eyup avatar
    3986 posts

    Posted 25 Jul 2018 Link to this post

    Hello Marcus,

    You can achieve this requirement using the following approach. Let's say you have this button in the PagerTemplate:
    <asp:Button ID="btnPageNext" runat="server" Text="Next"
        CommandName="Page" CommandArgument="Next"
        OnClientClick="return confirmChanges();"></asp:Button>

    Than, you can use this script logic to prevent or allow the PostBack action of the button:
    function confirmChanges() {
        var grid = $find('<%= RadGrid1.ClientID %>');
        return !grid.get_batchEditingManager().hasChanges(grid.get_masterTableView()) ||
        confirm("Any changes will be cleared. Are you sure you want to perform this action?");
    }

    That should do the trick.

    Regards,
    Eyup
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  7. Marcus
    Marcus avatar
    3 posts
    Member since:
    Jul 2018

    Posted 27 Jul 2018 in reply to Eyup Link to this post

    Hi Eyup,

        Thank you so much! Works like a charm.

    Thank you & Regards,

    Marcus Goh

Back to Top