This is a migrated thread and some comments may be shown as answers.

Save batch edits on page change or sort

6 Answers 409 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 27 Aug 2015, 04:03 PM

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());         
        }       
    }     
}

6 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 01 Sep 2015, 06:23 AM
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
Franz
Top achievements
Rank 1
Iron
Iron
commented on 22 Jun 2021, 10:24 PM

how can i do this approach? how can i know in server side if its page or sort at the end of BatchEditCommand?
0
Marcus
Top achievements
Rank 1
answered on 18 Jul 2018, 07:58 AM

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

0
Eyup
Telerik team
answered on 23 Jul 2018, 04:57 AM
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.
0
Marcus
Top achievements
Rank 1
answered on 23 Jul 2018, 06:27 AM

Hi ,

 

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

0
Eyup
Telerik team
answered on 26 Jul 2018, 04:55 AM
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.
0
Marcus
Top achievements
Rank 1
answered on 27 Jul 2018, 07:16 AM

Hi Eyup,

    Thank you so much! Works like a charm.

Thank you & Regards,

Marcus Goh

Tags
Grid
Asked by
Stephen
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Marcus
Top achievements
Rank 1
Share this question
or