Making standard postback for exporting RadGrid with AjaxManager

7 posts, 1 answers
  1. Tushar
    Tushar avatar
    25 posts
    Member since:
    Mar 2014

    Posted 30 May 2014 Link to this post

    Hi,
    I have implemented custom export functionality to export RadGrid data.
    I have added a menu “Export All Columns” to HeaderContextMenu of Grid, which when clicked Grid data should be exported to CSV.

    RadGrid is ajaxified using RadAjaxManager:
        <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
            <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
                <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="PanelVendor" />
                        <telerik:AjaxUpdatedControl ControlID="pnlGrid" LoadingPanelID="RadAjaxLoadingPanel1" />
                    <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" />
                </UpdatedControls>
            </telerik:AjaxSetting>        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="UsersGrid">
                <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="UsersGrid" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>

    Following this link http://www.telerik.com/support/code-library/export-radgrid-content-to-excel-word-csv-pdf-with-ajax-enabled , I have added javascript code to disable ajax and make standard postback:
    function onRadAjaxRequestStart(ajaxManager, eventArgs) {
        if (eventArgs.EventTargetElement.value != undefined && eventArgs.EventTargetElement.value.indexOf("Export") != -1) {
            eventArgs.set_enableAjax(false);
            ajaxManager.set_enableAJAX(false);
        }
    }

    Now this all works fine and data is exported to CSV when “Export All Columns” header menu is clicked.
    But the RadAjaxLoadingPanel is displayed on the grid and it does not go away even after CSV is downloaded.

    What do I need to do so that RadAjaxLoadingPanel does not appear while exporting?
  2. Answer
    Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2425 posts

    Posted 04 Jun 2014 Link to this post

    Hi Tushar,

    The Code-Library that you are referring to is a rather old and I strongly recommend that you take a look at the following help article, where detailed information for exporting from AJAX enabled RadGrid is available:
    Furthermore, for your convenience, following is a very simple example demonstrating how to implement the approach from the help article:
    <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" DefaultLoadingPanelID="LoadingPanel1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
        <ClientEvents OnRequestStart="onRadAjaxRequestStart" />
    </telerik:RadAjaxManager>
     
    <telerik:RadAjaxLoadingPanel runat="server" Skin="Default" ID="LoadingPanel1"></telerik:RadAjaxLoadingPanel>
     
    <telerik:RadCodeBlock runat="server">
        <script type="text/javascript">
            function onRadAjaxRequestStart(sender, args) {
                if (args.get_eventTarget().indexOf("ExportTo") >= 0)
                    args.set_enableAjax(false);
            }
        </script>
    </telerik:RadCodeBlock>
     
    <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView CommandItemDisplay="Top" CommandItemSettings-ShowExportToCsvButton="true"></MasterTableView>
    </telerik:RadGrid>

    Finally, please inspect your page for any JavaScript errors when you try to export from the grid, which could prevent the proper work of the controls.

    I hope this helps.


    Regards,
    Konstantin Dikov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Lenny_shp
    Lenny_shp avatar
    343 posts
    Member since:
    Jul 2006

    Posted 01 Oct 2015 in reply to Konstantin Dikov Link to this post

    2015.2.908.45  RadGrid MasterTableView CommandItemSettings-ShowExportToExcelButton="true"

    args.get_eventTarget() returns "RadGrid1" when Export button is clicked and not any "ExportTo" button.

    No difference with RadCodeBlock or RadScriptBlock.
  4. Lenny_shp
    Lenny_shp avatar
    343 posts
    Member since:
    Jul 2006

    Posted 01 Oct 2015 in reply to Lenny_shp Link to this post

    I found that RadGrid1's ClientSetting OnCommand="CheckChanges" was preventing the ExportToExcelButton to be reflected in args.get_eventTarget.

    Researching...

  5. Lenny_shp
    Lenny_shp avatar
    343 posts
    Member since:
    Jul 2006

    Posted 01 Oct 2015 in reply to Lenny_shp Link to this post

    Same issue applies to RadAjaxPanel.   I saw some people having the same issue and don't see any resolution for this with the built-in Export Command when RadGrid OnCommand is used, args.get_eventTarget in OnRequestStart has "RadGrid1" instead of "ExportToExcel" button.

     Do we have to use external button outside of the grid to do the export??

  6. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2425 posts

    Posted 06 Oct 2015 Link to this post

    Hi,

    When you attach handler to the client-side OnCommand event of the grid, the commands mechanism will be changed and the postback will be initiated from the RadGrid. In order to handle that scenario you need to use global variable to determine if an Export command was fired:
    <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" DefaultLoadingPanelID="LoadingPanel1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
        <ClientEvents OnRequestStart="onRadAjaxRequestStart" />
    </telerik:RadAjaxManager>
     
    <telerik:RadAjaxLoadingPanel runat="server" Skin="Default" ID="LoadingPanel1"></telerik:RadAjaxLoadingPanel>
     
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            var enableAjax = true;
     
            function onRadAjaxRequestStart(sender, args) {
                args.set_enableAjax(enableAjax);
                enableAjax = true;
            }
     
            function CheckChanges(sender, args) {
                if (args.get_commandName().indexOf("Export") >= 0) {
                    enableAjax = false;
                }
            }
        </script>
    </telerik:RadCodeBlock>
     
    <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView CommandItemDisplay="Top" CommandItemSettings-ShowExportToCsvButton="true"></MasterTableView>
        <ClientSettings>
            <ClientEvents OnCommand="CheckChanges" />
        </ClientSettings>
    </telerik:RadGrid>

    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
  7. Lenny_shp
    Lenny_shp avatar
    343 posts
    Member since:
    Jul 2006

    Posted 07 Oct 2015 in reply to Konstantin Dikov Link to this post

    Konstantin,

    Using a global variable worked, thank you!

Back to Top