radgrid export to excel not working

11 posts, 0 answers
  1. Jeff
    Jeff avatar
    9 posts
    Member since:
    Jan 2013

    Posted 22 Jan 2014 Link to this post

    Here is my grid:

                    <asp:Button ID="btnExcel" runat="server" CssClass="inpButton" Text="Export to Excel" OnClick="btnExcel_Click" />
                    <telerik:RadGrid ID="grdInvoices" AutoGenerateColumns="false" AllowSorting="false" AllowMultiRowSelection="false" runat="server">
                        <MasterTableView>
                            <Columns>
                                <telerik:GridBoundColumn HeaderText="Invoice Number" UniqueName="InvNum" DataField="invoiceNumber" HeaderStyle-Width="10%"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn HeaderText="Note Code" UniqueName="Note" DataField="noteCode" HeaderStyle-Width="10%"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn HeaderText="PO Number" UniqueName="po" DataField="poNumber" HeaderStyle-Width="30%"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn HeaderText="Invoice Date" UniqueName="invDate" DataField="invoiceDate" HeaderStyle-Width="10%"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn HeaderText="Original Invoice Balance" UniqueName="OrigBal" DataField="origBalance" HeaderStyle-Width="15%"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn HeaderText="Invoice Payments" UniqueName="InvPay" DataField="paymentAmount" HeaderStyle-Width="10%"></telerik:GridBoundColumn>
                                <telerik:GridBoundColumn HeaderText="Present Invoice Balance" UniqueName="InvBal" DataField="presentBalance" HeaderStyle-Width="15%"></telerik:GridBoundColumn>
                            </Columns>
                        </MasterTableView>
                        <ClientSettings EnableRowHoverStyle="true">
                            <Selecting AllowRowSelect="True"></Selecting>
                        </ClientSettings>
                    </telerik:RadGrid>


    And here is the button click logic:
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            try
            {
                grdInvoices.ExportSettings.ExportOnlyData = true;
                grdInvoices.ExportSettings.IgnorePaging = true;
                grdInvoices.ExportSettings.OpenInNewWindow = true;
                grdInvoices.ExportSettings.FileName = @"C:\jasInv.xlxs";
                grdInvoices.MasterTableView.ExportToExcel();
            }
            catch (Exception ex)
            {
                txtError.Text = ex.Message;
            }
        }

    It does nothing.  The file isn't created, no errors, nothing.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Jan 2014 Link to this post

    Hi Jeff,

    The exporting feature works only with regular postbacks. This means, that the asynchronous postback should be canceled when performing an export. So if you have ajaxified your RadGrid make sure you explicitly disable its ajax mode to export with regular postback. Please look into this documentation for more information on Export from Ajaxified Grid

    JavaScript:
    function onRequestStart(sender, args) {
      if (args.get_eventTarget().indexOf("btnExcel") >= 0)
        args.set_enableAjax(false);
    }

    Thanks,
    Princy


  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Allen
    Allen avatar
    50 posts
    Member since:
    Mar 2011

    Posted 05 Jun 2015 in reply to Princy Link to this post

    What about for the built-in CommandItem buttons that do this?
  5. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 10 Jun 2015 Link to this post

    Hi Allen,

    The IDs of the automatically generated Export buttons are provided in the documentation link from the previous post:
    • ExportToExcelButton
    • ExportToWordButton
    • ExportToPdfButton
    • ExportToCsvButton

    Hope this helps.

    Regards,
    Eyup
    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
  6. Digger
    Digger avatar
    5 posts
    Member since:
    Sep 2009

    Posted 24 Jul 2015 Link to this post

    Not understanding the automatically generated Export buttons, and how i tie the onRequestStart function to be called.

    Have multiple RadGrids in a RadMultiPageView.  (No asp panel wrapping grids).  I created the script:

    function onRequestStart(sender, args) {
                    if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0)
                        args.set_enableAjax(false);
                    if (args.get_eventTarget().indexOf("ExportToWordButton") >= 0)
                        args.set_enableAjax(false);
                    if (args.get_eventTarget().indexOf("ExportToPdfButton") >= 0)
                        args.set_enableAjax(false);
                    if (args.get_eventTarget().indexOf("ExportToCsvButton") >= 0)
                        args.set_enableAjax(false);

    }

    Who needs to call this function?  The Grid? the Multi page?  What control do i need to set the OnRequestStart to be called?  Is this call only available for asp panels?  Do i have to create my own buttons and not use the built in Command Items?

    Thanks

  7. Digger
    Digger avatar
    5 posts
    Member since:
    Sep 2009

    Posted 24 Jul 2015 in reply to Digger Link to this post

    Ok, Found the OnRequestStart function in the AjaxManager.  Did not work with the ClientEvents-OnRequestStart (All my Multipage Views stopped working).  When i moved the command to <ClientEvents OnRequestStart="myscript" /> it worked.

     My fustration here is that your examples all seem to work with user created buttons.   I could not find any examples with your built-in CommandItem functions for ​export.   Nor could i find the explicit call to OnRequestStart  in the AjaxManager in your examples.

     This is your built-in function - Give explicit full examples of your default built-in functions.;   I find this throughout all your controls, in that you gloss over your built in functions and how to implement, and give many examples of custom controls.   Wasted many hours thinking i had to build this custom control to make what you have built-in already work because you just gloss over the built in control functions....

     

  8. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 29 Jul 2015 Link to this post

    Hello Digger,

    Note that before introducing the XLSX and DOXS export format we were using the built-in command item buttons to export the grid in our example. Nevertheless in order to demonstrates the newly added formats our designer decided that it will be easier for customer to try them by using an external buttons. This way there is a different button for each export format and you can test them without any additional modifying.
    However thank you for your feedback. I will discuss your suggestion with our designers and we will change the layout of the page and we will include the built-in buttons in the demos.

    Regards,
    Kostadin
    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. Paul
    Paul avatar
    5 posts
    Member since:
    Jun 2014

    Posted 11 Jan in reply to Kostadin Link to this post

    Would have to strongly agree with Digger on his point regarding it being completely confusing.

    On the surface it seems like you would do this in the Ajax code, but this doesn't work for me:

    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="onRequestStart"</telerik:RadAjaxManager>

    In the javascript region ... 

    function onRequestStart(sender, args) {
                if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0)
                    args.set_enableAjax(false);
                if (args.get_eventTarget().indexOf("ExportToPdfButtonn") >= 0)
                    args.set_enableAjax(false);
                if (args.get_eventTarget().indexOf("ExportToCsvButton") >= 0)
                    args.set_enableAjax(false);
    }

     

  10. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 14 Jan Link to this post

    Hi Paul,

    Generally speaking the approach which stops the ajax request could be use for any controls which perform such request and not only the buttons which export the grid. When onRequestStart is fired you can get the target element which perform the request and you can check if this element is the button which fired the export command. This way you can manually disable the ajax and perform a full postback which is required when export the grid.
    You can see this approach in action in the following live example. I would appreciate if you can debug the client code and check what is the value when you call args.get_eventTarget(). Additionally you can check the value when you call args.get_eventArgument() as well.

    Regards,
    Kostadin
    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
  11. Paul
    Paul avatar
    5 posts
    Member since:
    Jun 2014

    Posted 18 Jan in reply to Kostadin Link to this post

    I'm still in string agreement with Digger about the confusing. All that we want to do is use the standard button as its designed to work with Ajax. Radgrid should have better programming to deal with supporting Ajax and exports together.

    In the end, I just added a client script in the Content section to force the 'postback'. I had to catch a few postbacks for other features and cancel them, but its simple and works. 

    <script type="text/javascript"> 

      function OnClientItemClicked() { __doPostBack(); }

    </script>

  12. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 21 Jan Link to this post

    Hello Paul,

    With our latest official release (Q1 2016) there is not need to cancel the ajax if you are using the built-in buttons. Please check out the following help article for more information.

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