This is a migrated thread and some comments may be shown as answers.

RadGrid Export to Excel in IE9

2 Answers 97 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 04 Dec 2012, 05:50 PM
I have a rad grid that is AJAXified through a RadAjaxManager and I use a javascript function to stop the async request so I can export. This works perfectly in Chrome and Firefox. But does not seem to work in internet explorer. The page just refreshes. Any workarounds to this?

<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <script type="text/javascript">
 
        Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initRequest);
        function initRequest(sender, args) {
            if (args.get_postBackElement().id.indexOf("btn_ExcelExport") != -1) {
                args.set_cancel(true);  //stop async request
                sender._form["__EVENTTARGET"].value = args.get_postBackElement().id.replace(/\_/g, "$");
                sender._form["__EVENTARGUMENT"].value = "";
                sender._form.submit();
                return;
            }
        }
      
    </script>
 
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="rg_Clients">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="rg_Clients" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadGrid ID="rg_Clients" runat="server" AllowFilteringByColumn="True" AllowPaging="True"
        ActiveItemStyle-BackColor="#004070" ActiveItemStyle-ForeColor="White" AllowSorting="True"
        CellSpacing="0" GridLines="Both" OnNeedDataSource="rg_Clients_NeedDataSource"
        Skin="WebBlue" OnItemCommand="rg_Clients_ItemCommand" AutoGenerateColumns="false"
        EnableLinqExpressions="false" HeaderStyle-Font-Bold="true" OnColumnCreating="rg_Clients_ColumnCreating"
        GroupingSettings-CaseSensitive="false" OnPreRender="rg_Clients_PreRender">
        <MasterTableView AutoGenerateColumns="false" AllowFilteringByColumn="True">
            <Columns>
                <telerik:GridTemplateColumn AllowFiltering="false" UniqueName="ClientID" HeaderStyle-Width="40px"
                    ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
                    <HeaderTemplate>
                        <asp:ImageButton ID="btn_ExcelExport" runat="server" ImageUrl="~/Images/Excel-icon.png"
                            Width="30" Height="30" OnClick="btn_ExcelExport_Click" CausesValidation="false" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Button ID="btn_View" CommandName="Select" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ClientID") %>'
                            runat="server" Text="View" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
        <ExportSettings Excel-Format="ExcelML" FileName="ClientExport" IgnorePaging="true"
            OpenInNewWindow="true"  ExportOnlyData="true" />
    </telerik:RadGrid>


protected void btn_ExcelExport_Click(object sender, ImageClickEventArgs e)
        {
            string[] UserRoles = Roles.GetRolesForUser();
 
            rg_Clients.MasterTableView.GetColumn("ClientID").Visible = false;
 
            //Dont need to see the filter boxes in the excel sheet
            foreach (GridFilteringItem Filter in rg_Clients.MasterTableView.GetItems(GridItemType.FilteringItem))
                Filter.Visible = false;
 
            //Data Admin and Site Admin can export all Clients, Sales Admin can export their own and who they are supervising,
            //  and Sales Person can only export their own             
            if (UserRoles.Contains("Sales Admin"))
            {
                string Source = PageController.GetUserFullName((Guid)Membership.GetUser().ProviderUserKey);
                List<string> Sources = ClientController.GetSupervisorSources((Guid)Membership.GetUser().ProviderUserKey);
                if (rg_Clients.MasterTableView.FilterExpression != String.Empty)
                    rg_Clients.MasterTableView.FilterExpression += "AND ";
                string QueryFilter = "([Sources] LIKE \'%" + Source + "%\') ";
                foreach (string S in Sources)
                    QueryFilter += "OR ([Sources] LIKE \'%" + S + "%\') ";
 
                rg_Clients.MasterTableView.FilterExpression += QueryFilter;
            }
            else if (UserRoles.Contains("Sales Person"))
            {
                string Source = PageController.GetUserFullName((Guid)Membership.GetUser().ProviderUserKey);
                if (rg_Clients.MasterTableView.FilterExpression != String.Empty)
                    rg_Clients.MasterTableView.FilterExpression += "AND ";
                rg_Clients.MasterTableView.FilterExpression += "([Sources] LIKE \'%" + Source + "%\') ";
            }
 
            rg_Clients.MasterTableView.ExportToExcel();

The rest of the columns are created programmatically on the first page load as some of the grid bound columns implement custom filters for google-like filtering and some are dropdownlists etc.

2 Answers, 1 is accepted

Sort by
0
Accepted
Manuel Buendia
Top achievements
Rank 1
answered on 04 Dec 2012, 07:32 PM
Yo lo que hago es agregar este metodo en mi javascript

<
script language="javascript" type="text/javascript" >
    function requestStart(sender, args) {
        if (args.get_eventTarget().indexOf("Excel") >= 0)
            args.set_enableAjax(false);
    }
</script>

Posteriormente
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1" ClientEvents-OnRequestStart="requestStart">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
 </AjaxSettings>
</telerik:RadAjaxManager>
en la parte del Radgrid Command
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToExcelCommandName)
            {
                //Generalizo la forma de exportar
                ConfigureExport("Consulta", this.RadGrid1);
                //despues de configurar la forma de exporta, ahora si mando los datos.
                RadGrid1.MasterTableView.ExportToExcel();
            }
}


public void ConfigureExport(string Nombre, RadGrid rd)
   {
       //Solo Exporto los datos no imagenes
       rd.ExportSettings.ExportOnlyData = true;
       //Ignoro la paginaciĆ³n, no importa si en la presentacion se muestran 5, aqui toda la informaciĆ³n
       rd.ExportSettings.IgnorePaging = true;
       //Abro en una pagina nueva para no refrescar la pagina donde se muestra
       rd.ExportSettings.OpenInNewWindow = true;
       //Le digo el nombre de mi archivo, y el nombre de la hoja de excel
       rd.ExportSettings.FileName = Nombre;
       //Quito los filtro utilizados(quito fila de filtros)
       rd.AllowFilteringByColumn = true;
 
 
       //Quito las columnas que no me importan
       //rd.MasterTableView.Columns.FindByUniqueName("CheckBox2").Visible = false;
 
       //Cambio el formato de como esportar; prueba
       //El formato de mi grid
       //rd.ExportSettings.Excel.Format = GridExcelExportFormat.Biff;
       //texto plano
       //rd.ExportSettings.Excel.Format = GridExcelExportFormat.Html;
       //texto plano
       //rd.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
 
       //rd.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
 
   }






Saludos 
0
Chris
Top achievements
Rank 1
answered on 04 Dec 2012, 08:22 PM
Thank You. Now works in all browsers.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Manuel Buendia
Top achievements
Rank 1
Chris
Top achievements
Rank 1
Share this question
or