Skip to content

DevExpress-Examples/asp-net-web-forms-grid-show-loading-panel-during-export

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Grid View for ASP.NET Web Forms - How to show an ASPxLoadingPanel during export

This example demonstrates how to display the ASPxLoadingPanel while the Grid View is exporting data.

Show ASPxLoadingPanel During Export

Implementation Details

You can show the loading panel on the client when you start the export process. There are no client events that signal that the export is complete and the loading panel should be hidden. This example illustrates how to use the ASPxCallback control to negate this limitation.

  1. Use the callback control's PerformCallback client event to start the export process.

    function onExportWithCallbackClick(exportType) {
        loadingPanel.Show();
        ExportCallback.PerformCallback(exportType);
    }
  2. Handle the callback control's Callback server event. In the event handler, use the DevExpress printing API to export the grid data to the specified format and save the result to a stream.

    protected void ExportCallback_Callback(object source, CallbackEventArgs e) {
        PrintingSystemBase ps = new PrintingSystemBase();
        PrintableComponentLinkBase lnk = new PrintableComponentLinkBase(ps);
        lnk.Component = MyGridExporter;
    
        CompositeLinkBase compositeLink = new CompositeLinkBase(ps);
        compositeLink.Links.AddRange(new object[] { lnk });
        compositeLink.CreateDocument();
    
        MemoryStream stream = new MemoryStream();
        string type = e.Parameter.ToString();
    
        switch (type) {
            case "pdf":
                compositeLink.PrintingSystemBase.ExportToPdf(stream);
                break;
            case "xls":
                compositeLink.PrintingSystemBase.ExportToXls(stream);
                break;
            case "rtf":
                compositeLink.PrintingSystemBase.ExportToRtf(stream);
                break;
        }
    
        Session["ExportStream"] = stream;
        Session["type"] = type;
    }
  3. After the callback is processed on the server and returns to the client, you can handle the result in the callback control's CallbackComplete event handler. Initiate a postback to download the exported file.

    function ExportCallbackComplete(s, e) {
        loadingPanel.Hide();
        response_btn.DoClick();
    }
  4. On the server, write the file data to the response as shown below.

    protected void response_btn_Click(object sender, EventArgs e) {
        MemoryStream stream = Session["ExportStream"] as MemoryStream;
        string type = Session["type"].ToString();
        WriteToResponse(MyGrid.ID, true, type, stream);
    }
    protected void WriteToResponse(string fileName, bool saveAsFile, string fileFormat, MemoryStream stream) {
        if (Page == null || Page.Response == null) return;
        string disposition = saveAsFile ? "attachment" : "inline";
        Page.Response.Clear();
        Page.Response.Buffer = false;
        Page.Response.AppendHeader("Content-Type", string.Format("application/{0}", fileFormat));
        Page.Response.AppendHeader("Content-Transfer-Encoding", "binary");
        Page.Response.AppendHeader("Content-Disposition", string.Format("{0}; filename={1}.{2}", disposition, HttpUtility.UrlEncode(fileName).Replace("+", "%20"), fileFormat));
        Page.Response.BinaryWrite(stream.ToArray());
        Page.Response.End();
    }

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)