Updating data via BatchEditCommand then Saving to DB

10 posts, 0 answers
  1. Jeremy Yoder
    Jeremy Yoder avatar
    131 posts
    Member since:
    Dec 2009

    Posted 18 Mar Link to this post

     

    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?

     

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 23 Mar Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jeremy Yoder
    Jeremy Yoder avatar
    131 posts
    Member since:
    Dec 2009

    Posted 05 Apr in reply to Konstantin Dikov Link to this post

     

    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.

     

  5. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 08 Apr Link to this post

    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.
  6. Jeremy Yoder
    Jeremy Yoder avatar
    131 posts
    Member since:
    Dec 2009

    Posted 12 Apr in reply to Viktor Tachev Link to this post

     

    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?

     

  7. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 13 Apr Link to this post

    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.
  8. Jeremy Yoder
    Jeremy Yoder avatar
    131 posts
    Member since:
    Dec 2009

    Posted 13 Apr in reply to Viktor Tachev Link to this post

     

    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?

     

  9. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 14 Apr Link to this post

    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.
  10. Jeremy Yoder
    Jeremy Yoder avatar
    131 posts
    Member since:
    Dec 2009

    Posted 14 Apr in reply to Viktor Tachev Link to this post

     

    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?

  11. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 15 Apr Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017