Save radgrid inside RadDataForm before generating a report (PDF)

2 Answers 100 Views
Ajax Grid
Christian
Top achievements
Rank 1
Iron
Christian asked on 15 Mar 2023, 09:08 AM | edited on 15 Mar 2023, 09:14 AM

Hello

I have a ASP.NET AJAX site which lets you generate bills. These have a header and lines.

It does have a button to generate a PDF report of this bill.

The form is a RadDataForm, always in edit mode, and inside of it, besides header fields, it's a RadGrid for the bill lines .

Until now, you have to first click the built in save button in the RadDataForm, then you can click on the generate PDF, otherwise the PDF is outdated (it doesn't save the modified header and lines). Now it's required that only clicking on the generate PDF button first save everything in the radDataForm, then it generates the PDF with updated values. I managed to save the header data before executing the PDF code but I can't figure out how to save the lines (RadGrid), because if I trigger it, then stops executing and the PDF report isn't generated at all.

The Save button has a client side Javascript event attached to save the RadGrid status.

 var grid1 = $telerik.findControl(document.documentElement, "RadGrid1");
var batchManager1 = grid1.get_batchEditingManager();
var hasChanges = batchManager1.hasChanges(grid1.get_masterTableView());
if (hasChanges) {
  batchManager1.saveTableChanges([grid1.get_masterTableView()]);
  ajaxPanel.ajaxRequest("saveChanges");

} else {
  var ajaxPanel = $telerik.findControl(document.documentElement, "RadAjaxPanel1");
  ajaxPanel.ajaxRequest("saveChanges");
}

If I add that same client side function in the generate PDF, it saves the RadGrid but it doesn't execute the generate PDF code (possibly due to the Postback preventing it to reach the server side function that generates the report and PDF)

 

I use the BatchEditCommand to manually get the updated, inserted or deleted rows and execute the SQL queries to do that. But I can't get the list of commands (the GridBatchEditingEventArgs e) without this event

Is there a way to save the radDataForm first (including the RadGrid) then generating the PDF file (it's a server side function)

Many thanks!

2 Answers, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 20 Mar 2023, 07:37 AM

Hi Christian,

The Batch editing mode is an entirely client-side editing mode. Processing its changes to the server (saving the currently changed values) is only possible with a dedicated postback. Usually, that is done with the built-in Save Changes button or with the Batch Editing API - as in the current case by calling the saveTableChanges() method.

In case another postback is executed prior to saving the grid, the current changes in the RadGrid will be lost. Due to the way ASP.NET AJAX framework works multiple postbacks cannot be instantiated simultaneously.

With this in mind, it seems you have been on the right track with the current scenario.

As generating the PDF report is a server-side process you can possibly leverage the Postback triggered for saving the batch changes to also excute the PDF generation logic within the same request.

You can use the BatchEditCommand event to execute the database queries and afterward call the PDF generation method. If this event is too early for the PDF generation for some reason, you can use a boolean flag to keep track of if batch changes are being saved and trigger the PDF generation in a later event when that is needed.

bool hasBatchEditCommandFired = false;

protected void RadGrid1_BatchEditCommand(object sender, Telerik.Web.UI.GridBatchEditingEventArgs e)
{
    //execude CRUD commands and update the database

    hasBatchEditCommandFired = true;
}

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (hasBatchEditCommandFired)
    {
        //rebind the Grid if changes should be applied prior to generating the PDF report
        RadGrid1.Rebind();
            
        //execute the PDF generation here
    }
}

Kind regards,
Doncho
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Christian
Top achievements
Rank 1
Iron
answered on 20 Mar 2023, 08:42 AM | edited on 20 Mar 2023, 08:45 AM

Hi Doncho

Many thanks for your reply!

I have added the save PDF function into the BatchEditCommand but there is a issue I maybe overlooked in the OP:

When you write some column values in the RadGrid, it automatically calculates the price of the line and saves the entire RadGrid via the JavaScript function.

Same when simply saving the radDataForm, If I click save, it saves the header plus lines, the lines via the same JavaScript function.

If I add the save PDF function there (without any other condition) then it generates the PDF after finalizing editing the line

I want to save header plus lines, then after that, generate the PDF with the saved values (from the database)

Maybe a solution would be some session to identify if the BatchEditCommand  has been called from the Print PDF button or elsewhere, then making a conditional call in the BatchEditCommand, but I can't get to save some session value or hidden field from the JavaScript

For what I read, it isn't possible to modify session data from JavaScript and when I try to change the value of an asp:hidden field, the script execution stops just after that (then it doesn't save the RadGrid status)


Many thanks for any assistance!

Regards

Doncho
Telerik team
commented on 22 Mar 2023, 12:49 PM

Hi Christian,

Using a HiddenField seems like a completely valid approach to me.

Normally, setting the value of a HiddenField before triggering the postback for saving batch changes should not prevent the further execution of the JavaScript code.

I would suggest you double-check for eventual JavaScript errors that might be leading to the unexpected behavior:

Tags
Ajax Grid
Asked by
Christian
Top achievements
Rank 1
Iron
Answers by
Doncho
Telerik team
Christian
Top achievements
Rank 1
Iron
Share this question
or