ReportViewerForMvc.cs
5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
namespace Vrh.Web.OneReport.ReportViewerForMvc
{
/// <summary>
/// Encapsulates the methods and properties used for the ReportViewerForMvc extension.
/// </summary>
public static class ReportViewerForMvc
{
private static ReportViewer reportViewer;
internal static ReportViewer ReportViewer
{
get { return reportViewer; }
set
{
//TODO: Implement dynamic ID
reportViewer = value;
reportViewer.ID = "ReportViewer1";
}
}
internal static HtmlString GetIframe(ReportViewer reportViewer, object htmlAttributes, int asyncPostBackTimeout)
{
string IframeId;
if (reportViewer == null) { throw new ArgumentNullException("reportViewer", "Value cannot be null."); }
ReportViewerForMvc.ReportViewer = reportViewer;
IDictionary<string, object> parsedHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
IframeId = parsedHtmlAttributes["id"] == null
? "r" + Guid.NewGuid().ToString()
: TagBuilder.CreateSanitizedId(parsedHtmlAttributes["id"].ToString());
if (IframeId == null) { throw new ArgumentNullException("htmlAttributes.id", "Value cannot be null."); }
string applicationPath = (HttpContext.Current.Request.ApplicationPath == "/") ? "" : HttpContext.Current.Request.ApplicationPath;
var iFrametagBuilder = new TagBuilder("iframe");
iFrametagBuilder.GenerateId(IframeId);
iFrametagBuilder.MergeAttribute("src", applicationPath + "/Areas/OneReport/ReportViewerWebForm.aspx?AsyncPostBackTimeOut=" + asyncPostBackTimeout.ToString());
iFrametagBuilder.MergeAttributes(parsedHtmlAttributes, false);
iFrametagBuilder.SetInnerText("iframes not supported.");
string IFramTagtext = iFrametagBuilder.ToString();
IFramTagtext += "<script src=\"" + WebResourceHelper.GetWebResourceUrl(typeof(ReportViewerForMvc), "Vrh.Web.OneReport.ReportViewerForMvc.Scripts.ReceiveMessage.js") + "\"></script>";
IFramTagtext += "<script>ReportViewerForMvc.setIframeId('" + IframeId + "');</script>";
return new HtmlString(IFramTagtext);
}
/// <summary>
/// Constructs a ReporViewer
/// </summary>
/// <param name="reportViewer">The object containing the ReportViewer properties.</param>
/// <param name="report">An object containing the LocalReport/ServerReport properties.</param>
/// <returns>An instance of ReportViewer with the specified properties</returns>
public static ReportViewer AnonymousReportViewer(object reportViewer, object report) { return AnonymousReportViewer(reportViewer, report, null, null); }
/// <summary>
/// Constructs a ReporViewer with parameters
/// </summary>
/// <param name="reportViewer">The object containing the ReportViewer properties.</param>
/// <param name="report">An object containing the LocalReport/ServerReport properties.</param>
/// <param name="parameters">Object that contains the parameters for a report.</param>
/// <returns>An instance of ReportViewer with the specified properties</returns>
public static ReportViewer AnonymousReportViewer(object reportViewer, object report, object parameters) { return AnonymousReportViewer(reportViewer, report, parameters, null); }
/// <summary>
/// Constructs a ReporViewer with parameters and data sources.
/// </summary>
/// <param name="reportViewer">The object containing the ReportViewer properties.</param>
/// <param name="report">An object containing the LocalReport/ServerReport properties.</param>
/// <param name="parameters">Object that contains the parameters for a report.</param>
/// <param name="dataSources">The data sources to be added to the report.</param>
/// <returns>An instance of ReportViewer with the specified properties</returns>
public static ReportViewer AnonymousReportViewer(object reportViewer, object report, object parameters, object dataSources)
{
if (reportViewer == null) { throw new ArgumentNullException("reportViewer", "Value cannot be null."); }
if (report == null) { throw new ArgumentNullException("report", "Value cannot be null."); }
ReportViewer reportViewerControl = ReportViewerHelper.AnonymousToReportViewer(reportViewer);
if (reportViewerControl.ProcessingMode == ProcessingMode.Local)
{
reportViewerControl.LocalReport.SetProperties(ReportViewerHelper.AnonymousToLocalReport(report));
if (parameters != null) { reportViewerControl.LocalReport.SetParameters(ReportViewerHelper.AnonymousToReportParameter(parameters)); }
if (dataSources != null) { reportViewerControl.LocalReport.DataSources.Add(ReportViewerHelper.AnonymousToReportDataSourceList(dataSources)); }
}
else if (reportViewerControl.ProcessingMode == ProcessingMode.Remote)
{
reportViewerControl.ServerReport.SetProperties(ReportViewerHelper.AnonymousToServerReport(report));
if (parameters != null) { reportViewerControl.ServerReport.SetParameters(ReportViewerHelper.AnonymousToReportParameter(parameters)); }
}
return reportViewerControl;
}
}
}