Export RadGrid content to Excel/Word/CSV/PDF with Ajax enabled

Thread is closed for posting
11 posts, 0 answers
  1. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 04 Apr 2007 Link to this post

    Requirements

    RadControls version

    RadGrid, RadAjax
    or
    RadControls for ASP.NET AJAX Q1 2008

    .NET version

    2.0 / 3.x

    Visual Studio version

    2005 / 2008

    programming language

    JavaScript, VB.NET, C#

    browser support

    all browsers supported by RadControls


      PROJECT DESCRIPTION
    The demo presents how to export the grid content to Excel/Word/CSV/PDF when:
    • RadGrid resides inside RadAjaxPanel
    • RadGrid is ajaxified through RadAjaxManager
    • The RadGrid AJAX mode is enabled and the export action is triggered from buttons residing in the grid body (RadGrid Classic only, the built-in AJAX is removed from RadGrid Prometheus)

    Generally speaking, the exporting feature of the control work with regular postbacks only. The reason is the grid prepares additional information when performing export operation (available on postback). When the action is performed through asynchronous requests, this information can not be passed through the XMLHttpObject - that is why the communication between the browser and the server fails.

    To bypass the limitation you can wire the OnRequestStart event of the ajax panel/ajax manager or the grid itself, determine whether the target control is ajaxified and explicitly disable its ajax mode to export with regular postback. Below is the main part of the code:

    <script type="text/javascript"
        function mngRequestStarted(ajaxManager, eventArgs) 
        { 
            if (eventArgs.get_eventTarget().indexOf("mngBtn") != -1) 
                eventArgs.set_enableAjax(false); 
        } 
    </script> 
  2. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 05 Apr 2007 Link to this post

    Hello guys,

    In addition to the previous post, I am attaching another project presenting how to export to Excel/Word when the grid resides in content page and is ajaxified through RadAjaxManager located in the Master page. The difference is that the OnRequestStart handler has to be placed in the master page aspx and the ajax manager settings should be added either:

    • using RadAjaxManagerProxy in the content page to append the ajax settings to the manager declaratively (RadAjax Prometheus only)
    • dynamically from the code-behind of the master page
    Best,
    Stephen
    the telerik team

    Instantly find answers to your questions at the new telerik Support Center
  3. slai
    slai avatar
    7 posts
    Member since:
    Jun 2007

    Posted 20 Sep 2007 Link to this post

    What if radGrid resides in content page and is ajaxified by simplying setting the radGrid's EnableAJAX="True"?
  4. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 20 Sep 2007 Link to this post

    Hi Simon,

    I guess that the sample for the first grid instance in the first project in this thread will be applicable when the control resides in content page and its built-in Ajax mechanism is switched on. Let me know if I am leaving something out.

    Regards,
    Stephen
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. slai
    slai avatar
    7 posts
    Member since:
    Jun 2007

    Posted 20 Sep 2007 Link to this post

    Hi Stephen,

    Based on the project description (i.e. 1st zip file), the requirements are
    • RadGrid resides inside RadAjaxPanel
    • RadGrid is ajaxified through RadAjaxManager
    May be I'm missing something as well, in order to Ajaxified my radGrid, I did not add RadAjaxpanel and RadAjaxManager controls to the page. I just turn on the flag!
  6. slai
    slai avatar
    7 posts
    Member since:
    Jun 2007

    Posted 20 Sep 2007 Link to this post

    It works now after I upgraded RadGrid to the latest version i.e. 5.0

    Thanks.
  7. Rycho
    Rycho avatar
    162 posts
    Member since:
    Feb 2007

    Posted 21 Sep 2007 Link to this post

    I'm not sure if new version v 5.0 has a bug but earlier versions worked fine in my case:

    I export grid to i.e. Excel with Ajax enabled="false" and if I set Grid1.ExportSettings.IgnorePaging = True I always get such an error:

    RegisterRequiresControlState can only be called before and during PreRender

    Switching this property to false doesn't cause mentioned error. Any idea?

    Regards,
        Rycho
  8. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 24 Sep 2007 Link to this post

    Hi Rycho,

    I tried to recreate the problem using the latest version 5.0.0 of RadGrid but my attempts were not successful (see the attached project). Can you please outline the differences between my test case and yours or modify the demo to reproduce the erroneous behavior? They can identify the cause of the issue you experience to address it accordingly.

    Best regards,

    Stephen

    the Telerik team


    Instantly find answers to your questions at the new Telerik Support Center
  9. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 23 Oct 2007 Link to this post

    Hi guys,

    We just wanted to let you know, that we've added one more project to show you how to export from withing MS UpdatePanel, when the control is in RadGridTemplateColumn or in the CommandItemTemplate. Avoiding ajaxification from MS UpdatePanel is easy considering the control in question is available directly on the page by setting it declaratively as PostBackTrigger. However when in Template control, you cannot do that declaratively and need to locate and access it on the server. According to the MS AJAX documentation adding PostBackTrigger control programmatically is not supported, however you can use the RegisterPostBackControl() method of the ScriptManager for this purpose. Here is the code we've used in the example:

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridDataItem)  
            {  
                Button btn = (e.Item as GridDataItem)["TemplateColumn"].FindControl("Button1"as Button;  
                ScriptManager1.RegisterPostBackControl(btn);  
            }  
            if (e.Item is GridCommandItem)  
            {  
                Button btncmd = (e.Item as GridCommandItem).FindControl("btnExcel"as Button;  
                ScriptManager1.RegisterPostBackControl(btncmd);  
     
                btncmd = (e.Item as GridCommandItem).FindControl("btnWord"as Button;  
                ScriptManager1.RegisterPostBackControl(btncmd);  
     
                btncmd = (e.Item as GridCommandItem).FindControl("btnCSV"as Button;  
                ScriptManager1.RegisterPostBackControl(btncmd);  
     
                btncmd = (e.Item as GridCommandItem).FindControl("btnPDF"as Button;  
                ScriptManager1.RegisterPostBackControl(btncmd);  
            }  
        } 


    Best regards,
    Steve
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  10. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 15 Jan 2008 Link to this post

    Hi Rycho,

    We're adding one more project that is somehow related to the Export/RadGrid/Ajax scenario that this code library covers - namely exporting RadGrid's content from context RadMenu which is ajax enabled. Since the export requires standard postback, we have to somehow disable the ajax only for the specific menu items which trigger the export and keep the ajax for the rest.
    This is easily achieved by using the menu item's value to check against the appropriate one. As usual sample project is attached to show you our implementation.

    Best regards,
    Steve
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  11. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 27 Jul 2009 Link to this post

    Hello guys,

    Note that we updated the sample projects, attached to this code-library thread.
    - we removed the obsolete client-side methods - for example, we replaced EnableAjax with set_enableAjax().
    - we simplified the javascript code and optimized it to cover more scenarios.

    Regards,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top