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

Updating data via BatchEditCommand then Saving to DB

9 Answers 187 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeremy Yoder
Top achievements
Rank 1
Jeremy Yoder asked on 18 Mar 2016, 10:17 PM

 

I may be wrong in how to manually save data here, as I'm learning how to work with a Batch grid. If so, please correct me...

I have a RadGrid in Batch mode. When the user has made all changes to the header textboxes at the top of the form, and the detail values in the RadGrid below them, the user needs to click a general Save button to save all the data to the DB. (I'm hiding the "Save changes" button in the grid.)

When the Save button is clicked, I execute this javascript...

function SavePO() {
    var grd = GetControlRAD("grdPODetails");
    grd.get_batchEditingManager().saveChanges(grd.get_masterTableView());
}

 

As I understand it, that does nothing to the underlying data. Instead, it calls the server-side grdPODetails_BatchEditCommand, at which point I manually update the data. In there, I'm updating the dataset that was initially loaded onto the page behind the scenes...

Protected Sub grdPODetails_BatchEditCommand(sender As Object, e As Telerik.Web.UI.GridBatchEditingEventArgs) Handles grdPODetails.BatchEditCommand
    Try
        For Each command As GridBatchEditingCommand In e.Commands
            Select Case command.Type
                ' Code to delete, insert, or update each row of underlying dataset from HashTables
            End Select
        Next
        ' If no errors, also update header data from controls, then save it all to the DB
    Catch ex as Exception
        ' TODO - What do I do here?
    End Try
End Sub

 

As you can see from my TODO, I'm unsure what to do if there's an error. Do I to put "e.Canceled = True", and if so, what does it do? Also, how can I tell the user what the error (ex.Message) is without a postback? (Or does this auto postback?) Assuming it doesn't postback, the best I can determine is to have a control on the form to write the error to in red font, but management doesn't like that idea. They'd rather I pop up an alert, but not sure that's possible.

Any suggestions? And am I going about this the right way to manually save the data?

 

9 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 23 Mar 2016, 11:16 AM
Hi Jeremy,

Regarding the e.Canceled, as documented in our "Server-side API" article, it is applicable with enabled automatic CRUD operations when you need to cancel the operation after failed validation for example.

As for informing the user, you can place a simple Label control on your page and set the error message to the Text property of the Label. You can then clear the message after some time on client-side or clear it on the next postback (within Page's Load event).

Hope this helps.


Regards,
Konstantin Dikov
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 05 Apr 2016, 09:56 PM

 

After I call my javascript SavePO() function above and grdPODetails_BatchEditCommand has saved the data successfully, I want to do a full page postback (with a given key) to re-load the page and data. (This is done in my Page_Load if my key var has a value.)

How can I trigger a postback if grdPODetails_BatchEditCommand is successful, but don't if I had an error caught in the Catch ex as Exception area?

FYI, the button that calls SavePO() has the click event set to "SavePO(); return false;" because if there is an error, I don't want a postback -- only if the save if successful. How can that be done?

Maybe I'm going about it a bit wrong, but it seems there must be a way to use a Batch Grid to manually save the data with a button outside the grid, and only reload the page if it's successful.

 

0
Viktor Tachev
Telerik team
answered on 08 Apr 2016, 10:01 AM
Hello Jeremy,

Please note that in order for the data to be sent to the server there should be a postback. This means that the postback happens when the changes are sent to the server. The validation is performed after that.


Regards,
Viktor Tachev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 12 Apr 2016, 03:10 PM

 

In my grdPODetails_BatchEditCommand proc above, in the Catch portion, is there any way to make ex.Message appear as a popup to the user? I can't see how, as it's server-side, but I'm asking as management wants to know.

 I already tried the page's ClientScript.RegisterStartupScript with a javascript alert contains the message, but I believe because it's a partial postback, that why it doesn't work. Thoughts?

 

0
Viktor Tachev
Telerik team
answered on 13 Apr 2016, 10:21 AM
Hi Jeremy,

if you would like to use the BatchEditCommand event to add code to be executed client-side  you can use the following approach:

protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
{
    RadScriptManager.RegisterStartupScript(Page, Page.GetType(), "1", "Sys.Application.add_load(function(){{alert('success');}}, 0);", true);
}


Still, I would point out that postback will happen when sending the information to the server. However, with adding the logic from above you can alert the user if validation of the data was successful.


Regards,
Viktor Tachev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 13 Apr 2016, 10:00 PM

 

That's pretty slick, and seems to work to show my error message! And there's no possible drawbacks to this? Such as the message appearing a 2nd time during the next postback?

 

0
Viktor Tachev
Telerik team
answered on 14 Apr 2016, 09:53 AM
Hi Jeremy,

The message should be displayed only after the BatchEditCommand event was raised. It should not be shown after other actions with the grid (e.g. paging).

Regards,
Viktor Tachev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 14 Apr 2016, 02:37 PM

 

I implemented the change, but had a user click save a 2nd time, and that time they got 2 error messages -- the 1st message followed by the 2nd. Clicked again and got all 3 error messages, one after the other, etc. So apparently this tacks it on to the existing script each time?

0
Viktor Tachev
Telerik team
answered on 15 Apr 2016, 11:56 AM
Hello Jeremy,

This could be observed if the grid is Ajax-enabled. If this is the case you can try the following code. It is working as expected on my end.


protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
{
    RadScriptManager.RegisterStartupScript(Page, Page.GetType(), "1", "function pageLoad(){alert('success');}", true);
}



Regards,
Viktor Tachev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Grid
Asked by
Jeremy Yoder
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Jeremy Yoder
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or