ReportViewerForMvc.cs 5.55 KB
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;
        }
    }
}