<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.
10 Answers, 1 is accepted
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
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
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
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....
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
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);
}
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
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>
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