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

RadGrid Export not working

2 Answers 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Manas
Top achievements
Rank 1
Manas asked on 28 Jun 2012, 09:50 PM
Hello there the RadGrid Export functionality is not working for me. Can you help what am I doing wrong?

Below is the aspx page code. I am trying to use btnExport to export to excel.


<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.Master"
    CodeBehind="WorklistSummary.aspx.cs" Inherits="MyCompany.WorklistSummary"
    MaintainScrollPositionOnPostback="true" %>
 
<asp:Content runat="server" ID="Content1" ContentPlaceHolderID="cph1">
    <telerik:RadAjaxManagerProxy runat="server" ID="RadAjaxManagerProxy1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="pnlMain">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="pnlMain" LoadingPanelID="lp1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="btnSearch">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="grdUserLoad" LoadingPanelID="lp1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="grdUserLoad">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="grdUserLoad" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>
    <div class="formLayoutD">
        <asp:Panel runat="server" ID="pnlMain" Height="99%" Width="100%">
            <h3>
                Worklist Summary</h3>
            <asp:ValidationSummary ID="valSummary" runat="server" DisplayMode="BulletList" CssClass="validationSummary"
                ShowSummary="true" ValidationGroup="DateNeeded" />
            <table width="450px">
                <tr style="border: 1; border-color: black; border-width: 2">
                    <td style="width: 20%; text-align: left">
                        Admit Date Between:
                    </td>
                    <td style="width: 35%">
                        <telerik:RadDatePicker ID="dpStartAdmitDate" runat="server" AutoPostBack="false"
                            Calendar="" Width="100px">
                            <Calendar ID="Calendar1" ShowRowHeaders="False" runat="server" />
                        </telerik:RadDatePicker>
                        <asp:RequiredFieldValidator ID="valdpStartAdmitDate1" runat="server" ControlToValidate="dpStartAdmitDate"
                            ValidationGroup="DateNeeded" SetFocusOnError="true" ErrorMessage="Start Date is required"
                            Display="None" Text="" />
                        And
                        <telerik:RadDatePicker ID="dpEndAdmitDate" runat="server" AutoPostBack="false" Width="100px">
                            <Calendar ID="Calendar2" ShowRowHeaders="False" runat="server" />
                        </telerik:RadDatePicker>
                        <asp:RequiredFieldValidator ID="valdpEndAdmitDate1" runat="server" ControlToValidate="dpEndAdmitDate"
                            ValidationGroup="DateNeeded" SetFocusOnError="true" ErrorMessage="End Date is required"
                            Display="None" Text="" />
                    </td>
                    <td style="width: 5%;" align="left">
                        <asp:Button runat="server" ID="btnSearch" Text="Search" Width="75px" ValidationGroup="DateNeeded"
                            OnClick="btnSearch_Click" />
                    </td>
                    <td style="width: 5%;" align="left">
                        <asp:Button runat="server" ID="btnExport" Text="Export" Width="75px"
                            OnClick="btnExport_Click"  />
                    </td>
                </tr>
 
            </table>
            <div>
                <telerik:RadGrid ID="grdUserLoad" runat="server" AutoGenerateColumns="False" CssClass="Grid"
                    Skin="Windows7" GridLines="Vertical" Width="80%" AllowMultiRowSelection="false"
                    AllowFilteringByColumn="False" AllowSorting="false" AllowPaging="True" PageSize="20"
                    OnNeedDataSource="grdUserLoad_OnNeedDataSource" >
                    <ExportSettings HideStructureColumns="True" IgnorePaging="True" ExportOnlyData="True" />
                    <MasterTableView Width="100%" EnableViewState="True" >
                         <GroupByExpressions>
                           <telerik:GridGroupByExpression runat="server">
                                <SelectFields>
                                    <telerik:GridGroupByField runat="server" FieldName="Name" HeaderText="User Name" />
                                </SelectFields>
                                <GroupByFields>
                                    <telerik:GridGroupByField runat="server" FieldName="Name" HeaderText="" SortOrder="Ascending" />
                                </GroupByFields>
                            </telerik:GridGroupByExpression>
                        </GroupByExpressions>
                        <RowIndicatorColumn>
                            <HeaderStyle Width="10px"></HeaderStyle>
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn>
                            <HeaderStyle Width="10px"></HeaderStyle>
                        </ExpandCollapseColumn>
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="User" DataField="Name" UniqueName="UserName"
                                Visible="False" />
                            <telerik:GridBoundColumn HeaderText="Group(s)" DataField="ParentName" UniqueName="GroupName" />
                            <telerik:GridBoundColumn HeaderText="Workload" DataField="CountAccount" UniqueName="CountAccount" />
                        </Columns>
                    </MasterTableView>
                    <HeaderStyle CssClass="GridHeader" />
                    <PagerStyle AlwaysVisible="False" Mode="NextPrevAndNumeric" HorizontalAlign="Center"
                        Height="35px"></PagerStyle>
                    <GroupingSettings CaseSensitive="False" />
                </telerik:RadGrid>
            </div>
        </asp:Panel>
    </div>
</asp:Content>

Below is the btnExport_Click: 


protected void btnExport_Click(object sender, EventArgs e)
{
    grdUserLoad.MasterTableView.ExportToExcel();
}

In the btnSearch click event I fill in the grid. I tried with EnableViewState = true and/or with NeedDataSource where I fill the Grid again just in case while exporting it may need it. But nothing works.

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 29 Jun 2012, 05:00 AM
Hello Manas,

If you are using master/content pages, your AjaxRequest javascript event handler should be placed on the page where you have defined your RadAjaxManager.
aspx:
function onRequestStart(sender, args)
        {
            if (args.get_eventTarget().indexOf("btnExport") >= 0)
                args.set_enableAjax(false);
        }
    </script>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="onRequestStart" ...
Also check the following help documentation which explains the same.
Export from Ajaxified Grid.

Thanks,
Shinu.
0
Manas
Top achievements
Rank 1
answered on 29 Jun 2012, 02:42 PM
Hello Shinu,
Thank you for your reply, I had already seen the radGrid export article/sample on Telerik but since our AjaxManager is in the Master page I will prefer not to change that to add OnRequestStart event handler there. That is why I am trying the btnExport which is not Ajaxfied. I want to implement it using a button like the sample shows but it does not work for me. It is my understanding that the OnRequestStart event is not used if with the Command Button implementation in the sample. I compromised and made the change in the Master file to include the OnRequestStart event.

But the export does not export all pages when you are not on the first page of the radgrid. We are using NeedDataSource and setting the DataSource there in addition to setting DataSource on btnSearch_Clicked event.

Thanks,
Manas
Tags
Grid
Asked by
Manas
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Manas
Top achievements
Rank 1
Share this question
or