How to clear cache when closing the Report Viewer

To clear the server-side cache of the Bold Reports® ASP.NET Web Forms Report Viewer, call the clearReportCache and destroy methods. Use these methods when you close the Report Viewer or switch to another report in your application.

CSHTML

Add the following code to the page that hosts the Report Viewer to clear the Report Viewer cache and destroy the viewer before the page unloads.

<div id="reportviewer"></div>
<script>
 var isSubmit = true;
 $(document.body).bind('submit', $.proxy(this.formSubmit, this));
function formSubmit(args)
{
        isSubmit = false;
}

window.onbeforeunload = function () {
if (isSubmit) {
var reportviewerObj = $("#reportviewer").data("boldReportViewer");
        reportviewerObj.clearReportCache();
        reportviewerObj.destroy();
}

isSubmit = true;

};
</script>

Web API

In the Report Viewer Web API PostReportAction method, run the GC collection when the report action is ClearCache, as in the following code sample.

public object PostReportAction([FromBody] Dictionary<string, object> jsonArray)
{
  bool isclearcache = false;
  if (jsonArray != null && jsonArray.ContainsKey("reportAction") && jsonArray["reportAction"].ToString() == "ClearCache")
  {
    isclearcache = true;
  }
  var reportresult = ReportHelper.ProcessReport(jsonArray, this, this._cache);
  if (isclearcache)
  {
      GC.Collect(); isclearcache = false;
  }

 return reportresult;
}

See also