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

RadGrid Cancelling Export if Request is Too Large

2 Answers 99 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kris
Top achievements
Rank 1
Kris asked on 30 Dec 2014, 07:27 PM
I have a radgrid that may, at times, hold up to or over 30,000 rows. We also have custom paging in place on this radgrid. I've read several places that the radgrid is not meant to handle exporting such large amounts of data. I'm totally fine with this but, from a user point of view, I need a way to cancel such requests and display a message to the user. I'd like to either:

A) Set the exporting buttons on the radgrid to disabled but still show them when the records are over a certain amount.

Or

B) Stop the exporting event on the item command (or at any other point?) when records are over a certain amount and display a message to the user (this one is more favorable).

I have found that I can cancel the exporting by reacting to the client side event of the item command but the moment I add the client side event, even though it does absolutely nothing, the exporting no longer works. The application fires the item command event in the code behind and on the client side but never reaches the grid exporting event. It also never exports. Instead it merely returns the radgrid with the new page size and the removed filters and headers.

protected void RadGridInventory_ItemCommand(object sender, GridCommandEventArgs e)
        {
               if (e.CommandName == RadGrid.ExportToExcelCommandName || e.CommandName == RadGrid.ExportToCsvCommandName)
            {
                RadGridInventory.PageSize = RadGridInventory.MasterTableView.VirtualItemCount;
            }
 
            if (e.CommandName == RadGrid.ExportToExcelCommandName)
            {
                var headerItem = (GridHeaderItem)RadGridInventory.MasterTableView.GetItems(GridItemType.Header)[0];
                headerItem.Visible = true;
                isExport = true;
            }
 
            if (e.CommandName == RadGrid.ExportToCsvCommandName)
            {
                RadGridInventory.MasterTableView.GetColumn("CSVAcountId").Display = true;
                RadGridInventory.MasterTableView.GetColumn("AccountId").Display = false;
                RadGridInventory.MasterTableView.GetColumn("CSVStatus").Display = true;
                RadGridInventory.MasterTableView.GetColumn("Status").Display = false;
           }
        }
 
protected void RadGridInventory_GridExporting(object source, GridExportingArgs e)
        {
            if (e.ExportType == ExportType.Excel)
            {
                string customText = "<h1 style='text-align:center;'>Inventory Report for " + _applicationUser.Customer.Name + "</h1>";
                customText += "<p style='text-align:center;'><b>Current Date:</b> " + DateTime.Now + "<br />";
 
                if(RadDatePickerStarting.SelectedDate != null && RadDatePickerEnding.SelectedDate != null){
                    customText += "<b>Date Range Covered:</b> " + RadDatePickerStarting.SelectedDate + " - " + RadDatePickerEnding.SelectedDate + "<br />";
                } else {
                    customText += "<b>Date Range Covered:</b> " + RadComboBoxChoices.SelectedItem.Text + "<br />";
                }
 
                customText += "<b>Clients:</b>";
                foreach (var client in ClientSelector1.SelectedClients)
                {
                    customText += " " + client.Name + ",";
                }
 
                customText = customText.TrimEnd(',');
                customText += "</p>";
                e.ExportOutput = e.ExportOutput.Replace("<body>", "<body>" + customText);
            }
            isExport = false;
        }

<script type="text/javascript">
 
        function onRequestStart(sender, args) {
            if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0 ||
                    args.get_eventTarget().indexOf("ExportToCsvButton") >= 0) {
                args.set_enableAjax(false);
            }
        }
 
        function onCommand(sender, args) {
        }
    </script>

Any advice or examples on how to achieve either of these outcomes would be greatly appreciated.

2 Answers, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 31 Dec 2014, 04:00 PM
Hello Kris,

Please this approach a try:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
    if (e.CommandName.Contains("Export"))
    {
        if (check if item count is greater than given number)
        {
            e.Canceled = true;
            RadAjaxPanel1.Alert("Too many items selected!");
        }
    }
}

In this case I show an alert through the RadAjaxPanel control but this would also work for RadAjaxManager. The important bit is to set the Canceled property from the event arguments.

Let me know whether this helps.

Regards,
Daniel
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Kris
Top achievements
Rank 1
answered on 31 Dec 2014, 08:50 PM
Thank you Daniel, this was exactly what I was looking for!
Tags
Grid
Asked by
Kris
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Kris
Top achievements
Rank 1
Share this question
or