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

Export To Excel Not working from Button control

1 Answer 178 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Venkata
Top achievements
Rank 1
Venkata asked on 29 Dec 2015, 09:05 PM

Hi,

I am using Telerik Controls version 2015.1.401.40

I am trying to Export Grid data to excel. But nothing happening

Here is my code

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ClientEvents OnRequestStart="onRequestStart" />
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="Grid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="Grid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
 
  <telerik:RadGrid ID="Grid1" runat="server" AllowPaging="true" AllowSorting="true" Width="100%"                                 Height="100%" AllowFilteringByColumn="true" Skin="Bootstrap"  AutoGenerateColumns="false"
        PageSize="25" AllowMultiRowSelection="true" OnNeedDataSource="Grid1_NeedDataSource"
         OnItemCommand="Grid1_ItemCommand">
                <HeaderStyle HorizontalAlign="Left" Font-Bold="true" Font-Size="10pt"></HeaderStyle>
                <GroupingSettings CaseSensitive="false" />
                <MasterTableView Name="Master"  Width="100%">
                    <PagerStyle Position="Bottom" PageSizeControlType="RadDropDownList" Mode="NextPrevNumericAndAdvanced"
                        ShowPagerText="true" PageSizes="25,50,100,200" />
                    <Columns>
                        <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn">
                            <HeaderStyle HorizontalAlign="Left" Width="25px"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Left" Width="25px"></ItemStyle>
                        </telerik:GridClientSelectColumn>
                        <telerik:GridBoundColumn DataField="Name" HeaderText="Name" AllowFiltering="false">
                            <HeaderStyle HorizontalAlign="Left" Wrap="false"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Left" Wrap="true"></ItemStyle>
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Address" HeaderText="Address" AllowFiltering="false">
                            <HeaderStyle HorizontalAlign="Left" Wrap="false"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Left" Wrap="true"></ItemStyle>
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="E-Mail" HeaderText="E-Mail" AllowFiltering="false">
                            <HeaderStyle HorizontalAlign="Left" Wrap="false"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Left" Wrap="false"></ItemStyle>
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Phone" HeaderText="Phone"     AllowFiltering="false">
                            <HeaderStyle HorizontalAlign="Left" Wrap="false"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Left" Wrap="true"></ItemStyle>
                        </telerik:GridBoundColumn>                      
                    </Columns>                  
                </MasterTableView>
                <ClientSettings>                   
                </ClientSettings>
            </telerik:RadGrid>

 on my ASPX.cs page

protected void Grid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        try
        {
            switch (e.CommandName)
            {
                case "ExportToExcel":
                    GridLeads.GridLines = GridLines.Both;
                    GridLeads.MasterTableView.GridLines = GridLines.Both;
                    GridLeads.MasterTableView.HeaderStyle.BackColor = System.Drawing.Color.LightGray;
                    
                    GridLeads.MasterTableView.ItemStyle.BackColor = System.Drawing.Color.LightYellow;
                    GridLeads.MasterTableView.AlternatingItemStyle.BackColor = System.Drawing.Color.LightYellow;
                    GridLeads.Columns.FindByUniqueName("ClientSelectColumn").Visible = false;
 
                    GridLeads.MasterTableView.AllowFilteringByColumn = false;
                    GridLeads.MasterTableView.PagerStyle.Position = GridPagerPosition.Bottom;
                    GridLeads.ExportSettings.ExportOnlyData = true;
                    GridLeads.ExportSettings.OpenInNewWindow = true;
                    GridLeads.ExportSettings.IgnorePaging = true;
 
                   
                    GridLeads.ExportSettings.FileName = "Lead(s) Report";
                    GridLeads.MasterTableView.ExportToExcel();
                    break;
            }
 
        }
        catch (Exception ex)
        {
            throw new Exception("Error While Loading Grid ItemCommand", ex);
        }
    }

  my ASPX page

<asp:Button ID="btnExport" runat="server" OnClientClick="ExportToExcel();return false;"
                                       ToolTip="Export To Excel" class="btn btn-default btn-sm" Text="Export" />
 
 
 
       <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
       <script type="text/javascript">
 
          function onRequestStart(sender, args) {
               if (args.get_eventTarget().indexOf("btnExport") >= 0)
               {                 
                   args.set_enableAjax(false);
               }
            
 
           function ExportToExcel() {
               $find('<%= Grid1.ClientID %>').get_masterTableView().fireCommand("ExportToExcel");
           }

 </script>
    </telerik:RadCodeBlock>

 

 

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 30 Dec 2015, 11:27 AM
Hi Venkata,

The OnRequestStart logic should check for the corresponding initiator's ID:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/ajaxified-radgrid/what-you-should-have-in-mind/export-from-ajaxified-grid

However, when you use the fireCommand method, the initiator is the grid, not btnExport. Therefore, you can remove the OnClientClick handler of the mentioned button and use its server-side side OnClick event handler to call the grid export methods on the code-behind.

I hope the clarification was helpful. Please give it a try and let me know if it works for you.

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
Tags
Grid
Asked by
Venkata
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or