Save Radgrid in Batcheditmode from External Button

8 posts, 0 answers
  1. Karlheinz
    Karlheinz avatar
    14 posts
    Member since:
    Jul 2014

    Posted 07 Apr 2015 Link to this post

    I have a ASP-Button that is supposed to do two things when clicked:

    1. Call onClientClick to save radgrid content (batcheditmode) clientside, because radgrid doesnt provide serverside methode :-(
    2. Fire Click  (postback) to save other content

    Approach 1:

            btnOk.OnClientClick="SaveChangesToGrid();";
            btnOk.Click += new EventHandler(btnOk_Click);

    - Click is fired

    - javascript function "SaveChangesToGrid();" is called, but the grid doesnt save.

    Approach 2:
            btnOk.OnClientClick="SaveChangesToGrid();return false;";
            btnOk.Click += new EventHandler(btnOk_Click);

    - Click is not !! fired
    - javascript function "SaveChangesToGrid();" is called and saves then grid.

     

    Using the built-in save buttun works.

    Is there any possiblity to save a RadGrid AND other page-content with only clicking one button ??

     I have searched this forum for more than a day, but nothing works for me.

    Thanks for any help

     

     

     




    onClientItemClicked / onClientItemClicking prevents that itemClick is fired.

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 09 Apr 2015 Link to this post

    Hello Karlheinz,

    When you need to initiate the saving for RadGrid in Batch edit mode, you need to ensure that the used button will not perform postback on its own (no server-side OnClick event of the button). That is why your second approach is working correctly. 

    Nevertheless, you can take a look at the following example, which demonstrates how to save RadGrid changes and retrieve changes from other controls at the same time:
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function saveAll() {
                var grid1 = $find("<%=RadGrid1.ClientID%>");
                var batchManager1 = grid1.get_batchEditingManager();
                var hasChanges = batchManager1.hasChanges(grid1.get_masterTableView());                
                if (hasChanges) {
                    batchManager1.saveTableChanges([grid1.get_masterTableView()]);
                } else {
                    var ajaxPanel = $find("<%=RadAjaxPanel1.ClientID%>");
                    ajaxPanel.ajaxRequest("saveChanges");
                }
            }
        </script>
    </telerik:RadCodeBlock>
     
    <telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1" OnAjaxRequest="RadAjaxPanel1_AjaxRequest">
        <telerik:RadButton runat="server" ID="RadButton1" AutoPostBack="false" Text="Save all"
            OnClientClicked="saveAll"></telerik:RadButton>
        <telerik:RadTextBox runat="server" ID="RadTextBox1"></telerik:RadTextBox>
     
        <telerik:RadGrid runat="server" ID="RadGrid1" Width="200px" OnNeedDataSource="RadGrid1_NeedDataSource"
            OnBatchEditCommand="RadGrid1_BatchEditCommand">
            <MasterTableView EditMode="Batch">
            </MasterTableView>
        </telerik:RadGrid>
    </telerik:RadAjaxPanel>

    And the code-behind:
    private bool saveChanges = false;
     
    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        for (int i = 0; i < 3; i++)
        {
            table.Rows.Add(i);
        }
     
        (sender as RadGrid).DataSource = table;
    }
     
    protected void RadAjaxPanel1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        if (e.Argument == "saveChanges")
        {
            saveChanges = true;
        }
    }
     
    protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
    {
        saveChanges = true;
        //Handle the grid updates
    }
     
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (saveChanges)
        {
            //Save the values from the other controls
        }
    }

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Karlheinz
    Karlheinz avatar
    14 posts
    Member since:
    Jul 2014

    Posted 13 Apr 2015 in reply to Konstantin Dikov Link to this post

    Hello Konstantin,

     thanks for your help.

    I think, in your example, button and RadGrid must reside on the same ajax-panel.

    This is not possible in our page-layout.

    Is there any other approach to save a RadGrid in batch-edit-mode from external button with postback ??

    Thanks in advance for any help. 

     

  5. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 14 Apr 2015 Link to this post

    Hi Karlheinz,

    If you do not want to use the RadAjaxPanel approach, you can use a HiddenField control and set some value to it when you click on your external "Save" button. Then, on the server-side PreRender event you can retrieve that value and determine whether or not to save the changes from the other controls.

    You should keep in mind that you need to reset the value in the HiddenField controls after you retrieve it.

    Hope this helps.


    Best Regards,
    Konstantin Dikov
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  6. Karlheinz
    Karlheinz avatar
    14 posts
    Member since:
    Jul 2014

    Posted 14 Apr 2015 Link to this post

    Hi Konstantin,

    thanks for your answer

     

    Hello Telerik,

    Realy great control design

    Providing a Grid with batch-edit-mode that can be saved only with time-consuming workarounds is really a great idea :-(

     

  7. Emad
    Emad avatar
    16 posts
    Member since:
    Dec 2015

    Posted 05 Mar Link to this post

    Hi,

    I'm using external button to save two grids in batch edit mode, it's working fine.

    But I'm facing issue when required validation fires code still working where it should stop ?!!

    Please advice..

     

    function saveTowGrids() {
                try{
                    var grid1 = $find("<%= RadGrid1.ClientID %>");
                    var masterTable1 = grid1.get_masterTableView();
                    var grid2 = $find("<%= RadGrid2.ClientID %>");
                    var masterTable2 = grid2.get_masterTableView();
                    var batchEditManager = grid2.get_batchEditingManager();
                    var tables = [];
                    tables.push(masterTable1);
                    tables.push(masterTable2);
                    batchEditManager.saveTableChanges(tables);
                    alert("save completed");
                } catch (e) { alert(e);}
            }

     

    Thanks,

    Emad

  8. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 07 Mar Link to this post

    Hello Emad,

    You could use the BatchEditingManager and see if there is open row or cell for editing and initiate the saving only if no cell/row is opened:
    <telerik:RadCodeBlock runat="server">
        <script>
            function saveGrid(sender, args) {
                var grid = $find("<%= RadGrid1.ClientID%>");
                var tableView = grid.get_masterTableView();
                var batchManager = grid.get_batchEditingManager();
                if (batchManager.get_currentlyEditedRow() || batchManager.get_currentlyEditedCell()) {
                    alert("pending editing");
                }
                else {
                    alert("could initiate save");
                    //initate the save through the BatchEditingManager
                }
            }
        </script>
    </telerik:RadCodeBlock>

    Hope this helps.


    Regards,
    Konstantin Dikov
    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
  9. Emad
    Emad avatar
    16 posts
    Member since:
    Dec 2015

    Posted 07 Mar in reply to Konstantin Dikov Link to this post

    Thanks, It's working fine..
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017