This example demonstrates how to export a dashboard displayed using the ASP.NET MVC Dashboard extension on the server side using the WebDashboardExporter class. The following API is used:
- The ASPxClientDashboard.BeforeRender event is handled to obtain the client-side DashboardControl with the ASPxClientDashboard.GetDashboardControl method.
- The AJAX request is used to send the dashboard identifier and state to the server side. On the server side, these values are received as action method parameters and passed to the WebDashboardExporter.ExportToPdf method.
Files to look at
Documentation
More Examples
- Dashboard for Web Forms - How to Enable Export in Data Inspector
- Dashboard for Web Forms - How to export Web Dashboard into PDF with different filter values on different pages
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using System;
using System.IO;
using System.Net;
using System.Web.Mvc;
using System.Xml;
namespace MvcDashboard_ServerExport.Controllers {
public class HomeController : Controller {
public ActionResult Index() {
return View();
}
[HttpPost]
public ActionResult ExportDashboardToPdf(string DashboardID, string DashboardState) {
using (MemoryStream stream = new MemoryStream()) {
string dashboardID = DashboardID;
DashboardState dashboardState = new DashboardState();
dashboardState.LoadFromJson(DashboardState);
DashboardPdfExportOptions pdfOptions = new DashboardPdfExportOptions();
pdfOptions.ExportFilters = true;
pdfOptions.DashboardStatePosition = DashboardStateExportPosition.Below;
string dateTimeNow = DateTime.Now.ToString("yyyyMMddHHmmss");
string serverPath = Server.MapPath(@"~/App_Data/Export");
if(!Directory.Exists(serverPath)) {
Directory.CreateDirectory(serverPath);
}
string filePath = Path.Combine(serverPath, dashboardID);
string uniqueId = "_" + dateTimeNow + ".pdf";
var exporter = new WebDashboardExporter(DashboardConfigurator.Default);
exporter.ExportToPdf(dashboardID, stream, new System.Drawing.Size(1024, 768), dashboardState, pdfOptions);
SaveFile(stream, filePath + uniqueId);
ContentResult result = new ContentResult();
result.Content = filePath + uniqueId;
return result;
}
}
private void SaveFile(MemoryStream stream, string path) {
var fileStream = System.IO.File.Create(path);
stream.WriteTo(fileStream);
fileStream.Close();
}
}
}
JavaScriptfunction onBeforeRender(sender) {
var control = sender.getDashboardControl();
control.registerExtension(new DevExpress.Dashboard.DashboardPanelExtension(control));
$("#buttonContainer").dxButton({
text: "Export to PDF",
onClick: function (param) {
var dashboardID = control.dashboardContainer().id;
var dashboardStateJson = control.getDashboardState();
$.ajax({
url: 'Home/ExportDashboardToPdf',
data: {
DashboardID: dashboardID,
DashboardState: dashboardStateJson
},
type: 'POST',
success: function (result) {
DevExpress.ui.notify({ message: 'A dashboard was exported to ' + result, shading: true }, "success", 5000);
}
});
}
});
}