查看 MVC Razor 应用程序中的 rdlc 文件

发布于 2024-12-11 14:10:15 字数 200 浏览 1 评论 0原文

我尝试在 rdlc Razor 视图中显示。将 ReportViewer 控件嵌入到 ascx 控件中,该控件将传递数据。

问题是,用于绘制报表的 ReportViewer 控件会生成回发,并从 Razor 重新加载页面,我会丢失 Page 对象,而我无法执行回发。

谁能帮助我吗? Razor 中是否有使用 ReportViewer 控件的帮助程序?

I try to show in a view rdlc Razor. Embed the ReportViewer control in an ascx control, which will pass the data.

The problem is that the ReportViewer control to paint the report generates a postback, and reload the page from the Razor, I lose the Page object, which I can not do postback.

Can anyone help me?
Are there any helper to use the ReportViewer Control in Razor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风夜微凉 2024-12-18 14:10:15

这是一个简单的任务。您可以按照以下步骤操作。

  1. 在您的解决方案中创建一个文件夹并命名为Reports
  2. 添加 ASP.Net Web 表单并将其命名为 ReportView.aspx
  3. 创建一个类 ReportData 并添加将其复制到Reports文件夹中。将以下代码添加到类中。

    公共类ReportData
    {
        公共报告数据()
        {
            this.ReportParameters = new List<参数>();
            this.DataParameters = new List<参数>();
        }
    
        公共 bool IsLocal { 获取;放; }
        公共字符串报告名称{获取;放; }
        公共列表<参数>报告参数 { 获取;放; }
        公共列表<参数>数据参数 { 获取;放; }
    }
    
    公共类参数
    {
        公共字符串参数名 { get;放; }
        公共字符串值{获取;放; }
    }
    
  4. 添加另一个类并将其命名为ReportBasePage.cs。在该类中添加以下代码。

    公共类ReportBasePage:System.Web.UI.Page
    {
        受保护的 ReportData ReportDataObj { 获取;放; }
    
        受保护的重写 void OnInit(EventArgs e)
        {
            基.OnInit(e);
            if (HttpContext.Current != null)
                if (HttpContext.Current.Session["ReportData"] != null)
                {
                    ReportDataObj = HttpContext.Current.Session["ReportData"] as ReportData;
                    返回;
                }
            ReportDataObj = new ReportData();
            CaptureRouteData(页面.Request);
        }
    
    
        私有无效CaptureRouteData(HttpRequest请求)
        {
            var mode = (request.QueryString["rptmode"] + "").Trim();
            ReportDataObj.IsLocal = 模式==“本地”?真:假;
            ReportDataObj.ReportName = request.QueryString["报告名称"] + "";
            string dquerystr = request.QueryString["参数"] + "";
            if (!String.IsNullOrEmpty(dquerystr.Trim()))
            {
                var param1 = dquerystr.Split(',');
                foreach(参数 1 中的字符串 pm)
                {
                    var rp = 新参数();
                    var kd = pm.Split('=');
                    if (kd[0].Substring(0, 2) == "rp")
                    {
                        rp.ParameterName = kd[0].Replace("rp", "");
                        if (kd.Length > 1) rp.Value = kd[1];
                        ReportDataObj.ReportParameters.Add(rp);
                    }
                    else if (kd[0].Substring(0, 2) == "dp")
                    {
                        rp.ParameterName = kd[0].Replace("dp", "");
                        if (kd.Length > 1) rp.Value = kd[1];
                        ReportDataObj.DataParameters.Add(rp);
                    }
                }
            }
        }
    }
    
  5. ScriptManager添加到ReportView.aspx页面。现在使用报表查看器访问该页面。在报表查看器中设置属性AsyncRendering="false"。代码如下。

     
        
    
  6. ReportView.aspx.cs中添加两个NameSpace

    使用 Microsoft.Reporting.WebForms;
    使用系统.IO;
    

    System.Web.UI.Page更改为ReportBasePage。只需使用以下内容替换您的代码即可。

    公共分部类 ReportView : ReportBasePage
    {
        protected void Page_Load(对象发送者,EventArgs e)
        {
            if (!IsPostBack)
            {
                RenderReportModels(this.ReportDataObj);
            }
        }
    
        私有无效 RenderReportModels(ReportData 报告数据)
        {
            RASolarERPData dal = new RASolarERPData();
            列表<期末库存评估> objClosingInventory = new List();
    
            // 重置报告属性。
            ReportViewerRSFReports.Height = Unit.Parse("100%");
            ReportViewerRSFReports.Width = Unit.Parse("100%");
            ReportViewerRSFReports.CssClass = "表";
    
            // 清除所有以前的数据源。
            this.ReportViewerRSFReports.LocalReport.DataSources.Clear();
    
            // 设置本地处理的报告模式。
            ReportViewerRSFReports.ProcessingMode = ProcessingMode.Local;
    
            // 验证报告源。
            var rptPath = Server.MapPath(@"./Report/" + reportData.ReportName +".rdlc");
    
            //@"E:\RSFERP_SourceCode\RASolarERP\RASolarERP\Reports\Report\" + reportData.ReportName + ".rdlc";
            //Server.MapPath(@"./Report/ClosingInventory.rdlc");
    
            if (!File.Exists(rptPath))
                返回;
    
            // 设置报告路径。
            this.ReportViewerRSFReports.LocalReport.ReportPath = rptPath;
    
            // 设置报告参数。
            var rpPms = ReportViewerRSFReports.LocalReport.GetParameters();
            foreach(以 rpm 为单位的 rpm)
            {
                var p = reportData.ReportParameters.SingleOrDefault(o => o.ParameterName.ToLower() == rpm.Name.ToLower());
                if (p != null)
                {
                    ReportParameter rp = new ReportParameter(rpm.Name, p.Value);
                    ReportViewerRSFReports.LocalReport.SetParameters(rp);
                }
            }
    
            //设置报表SP执行的数据参数
            objClosingInventory = dal.ClosingInventoryReport(this.ReportDataObj.DataParameters[0].Value);
    
            // 加载数据源。
            var dsmems = ReportViewerRSFReports.LocalReport.GetDataSourceNames();
            ReportViewerRSFReports.LocalReport.DataSources.Add(new ReportDataSource(dsmems[0], objClosingInventory));
    
            // 刷新报表查看器。
            ReportViewerRSFReports.LocalReport.Refresh();
        }
    }
    
  7. 报告文件夹添加一个文件夹并将其命名为报告。现在将RDLC报告添加到Reports/Report文件夹中,并将其命名为ClosingInventory。 rdlc

  8. 现在添加一个控制器并将其命名为ReportController。在控制器中添加以下操作方法。

    public ActionResult ReportViewer()
        {                
            ViewData["reportUrl"] = "../Reports/View/local/ClosingInventory/";
    
            返回视图();
        }
    
  9. 添加视图页面,单击ReportViewer控制器。将视图页面命名为 ReportViewer.cshtml。将以下代码添加到视图页面。

    @using (Html.BeginForm("登录"))
         { 
               @Html.DropDownList("ddlYearMonthFormat", new SelectList(ViewBag.YearMonthFormat, "YearMonthValue", "YearMonthName"), new { @class = "DropDown" })
    
      在途库存:@Html.TextBox("txtStockInTransit", "", new { @class = "LogInTextBox" })
    
     
    
      }
    
  10. 添加Iframe。设置Iframe的属性如下

    frameborder="0" width="1000";高度=“1000”;样式=“溢出:隐藏;”滚动=“否”
    
  11. 将以下 JavaScript 添加到查看器。

     函数 ReportValidationCheck() {
    
        var url = $('#hdUrl').val();
        var 年月 = $('#ddlYearMonthFormat').val();      
        var stockInTransit = $('#txtStockInTransit').val()
    
        if (stockInTransit == "") {
            库存在途 = 0;
        }
    
        if (年月 == "0") {
            Alert("请正确选择月份。");
        }
        别的 {
    
            //url = url + "dpSpYearMonth=" + 年月 + ",rpYearMonth=" + 年月 + ",rpStockInTransit=" + stockInTransit;
    
            url = "../Reports/ReportView.aspx?rptmode=local&reportname=ClosingInventory¶meters=dpSpYearMonth=" + 年月 + ",rpYearMonth=" + 年月 + ",rpStockInTransit=" + stockInTransit;
    
            var myframe = document.getElementById("ifrmReportViewer");
            if (myframe !== null) {
                如果(myframe.src){
                    myframe.src = url;
                }
                else if (myframe.contentWindow !== null && myframe.contentWindow.location !== null) {
                    myframe.contentWindow.location = url;
                }
                else { myframe.setAttribute('src', url); }
            }
        }
    
        返回假;
    }
    
  12. Web.config 文件将以下键添加到 appSettings 部分

    添加

    key="UnobtrusiveJavaScriptEnabled" value="true"

    system.web handlers 部分添加以下键

     添加 `verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler、Microsoft.ReportViewer.WebForms、Version=10.0.0.0、Culture=neutral、PublicKeyToken =b03f5f7f11d50a3a"`
    
  13. 将数据源更改为您自己的。这个解决方案非常简单,我想每个人都喜欢它。

This is a simple task. You can follow the following steps.

  1. Create a folder in your solution and give a name Reports.
  2. Add a ASP.Net web form and named it ReportView.aspx
  3. Create a Class ReportData and add it to the Reports folder. Add the following code to the Class.

    public class ReportData
    {
        public ReportData()
        {
            this.ReportParameters = new List<Parameter>();
            this.DataParameters = new List<Parameter>();
        }
    
        public bool IsLocal { get; set; }
        public string ReportName { get; set; }
        public List<Parameter> ReportParameters { get; set; }
        public List<Parameter> DataParameters { get; set; }
    }
    
    public class Parameter
    {
        public string ParameterName { get; set; }
        public string Value { get; set; }
    }
    
  4. Add another Class and named it ReportBasePage.cs. Add the following code in this Class.

    public class ReportBasePage : System.Web.UI.Page
    {
        protected ReportData ReportDataObj { get; set; }
    
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (HttpContext.Current != null)
                if (HttpContext.Current.Session["ReportData"] != null)
                {
                    ReportDataObj = HttpContext.Current.Session["ReportData"] as ReportData;
                    return;
                }
            ReportDataObj = new ReportData();
            CaptureRouteData(Page.Request);
        }
    
    
        private void CaptureRouteData(HttpRequest request)
        {
            var mode = (request.QueryString["rptmode"] + "").Trim();
            ReportDataObj.IsLocal = mode == "local" ? true : false;
            ReportDataObj.ReportName = request.QueryString["reportname"] + "";
            string dquerystr = request.QueryString["parameters"] + "";
            if (!String.IsNullOrEmpty(dquerystr.Trim()))
            {
                var param1 = dquerystr.Split(',');
                foreach (string pm in param1)
                {
                    var rp = new Parameter();
                    var kd = pm.Split('=');
                    if (kd[0].Substring(0, 2) == "rp")
                    {
                        rp.ParameterName = kd[0].Replace("rp", "");
                        if (kd.Length > 1) rp.Value = kd[1];
                        ReportDataObj.ReportParameters.Add(rp);
                    }
                    else if (kd[0].Substring(0, 2) == "dp")
                    {
                        rp.ParameterName = kd[0].Replace("dp", "");
                        if (kd.Length > 1) rp.Value = kd[1];
                        ReportDataObj.DataParameters.Add(rp);
                    }
                }
            }
        }
    }
    
  5. Add ScriptManager to the ReportView.aspx page. Now Take a Report Viewer to the page. In report viewer set the property AsyncRendering="false". The code is given below.

        <rsweb:ReportViewer ID="ReportViewerRSFReports" runat="server" AsyncRendering="false"
            Width="1271px" Height="1000px" >
        </rsweb:ReportViewer>
    
  6. Add two NameSpace in ReportView.aspx.cs

    using Microsoft.Reporting.WebForms;
    using System.IO;
    

    Change the System.Web.UI.Page to ReportBasePage. Just replace your code using the following.

    public partial class ReportView : ReportBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RenderReportModels(this.ReportDataObj);
            }
        }
    
        private void RenderReportModels(ReportData reportData)
        {
            RASolarERPData dal = new RASolarERPData();
            List<ClosingInventoryValuation> objClosingInventory = new List<ClosingInventoryValuation>();
    
            // Reset report properties.
            ReportViewerRSFReports.Height = Unit.Parse("100%");
            ReportViewerRSFReports.Width = Unit.Parse("100%");
            ReportViewerRSFReports.CssClass = "table";
    
            // Clear out any previous datasources.
            this.ReportViewerRSFReports.LocalReport.DataSources.Clear();
    
            // Set report mode for local processing.
            ReportViewerRSFReports.ProcessingMode = ProcessingMode.Local;
    
            // Validate report source.
            var rptPath = Server.MapPath(@"./Report/" + reportData.ReportName +".rdlc");
    
            //@"E:\RSFERP_SourceCode\RASolarERP\RASolarERP\Reports\Report\" + reportData.ReportName + ".rdlc";
            //Server.MapPath(@"./Report/ClosingInventory.rdlc");
    
            if (!File.Exists(rptPath))
                return;
    
            // Set report path.
            this.ReportViewerRSFReports.LocalReport.ReportPath = rptPath;
    
            // Set report parameters.
            var rpPms = ReportViewerRSFReports.LocalReport.GetParameters();
            foreach (var rpm in rpPms)
            {
                var p = reportData.ReportParameters.SingleOrDefault(o => o.ParameterName.ToLower() == rpm.Name.ToLower());
                if (p != null)
                {
                    ReportParameter rp = new ReportParameter(rpm.Name, p.Value);
                    ReportViewerRSFReports.LocalReport.SetParameters(rp);
                }
            }
    
            //Set data paramater for report SP execution
            objClosingInventory = dal.ClosingInventoryReport(this.ReportDataObj.DataParameters[0].Value);
    
            // Load the dataSource.
            var dsmems = ReportViewerRSFReports.LocalReport.GetDataSourceNames();
            ReportViewerRSFReports.LocalReport.DataSources.Add(new ReportDataSource(dsmems[0], objClosingInventory));
    
            // Refresh the ReportViewer.
            ReportViewerRSFReports.LocalReport.Refresh();
        }
    }
    
  7. Add a Folder to the Reports Folder and named it Report. Now add a RDLC report to the Reports/Report folder and named it ClosingInventory.rdlc.

  8. Now add a Controller and Named it ReportController. In to the controller add the following action method.

    public ActionResult ReportViewer()
        {                
            ViewData["reportUrl"] = "../Reports/View/local/ClosingInventory/";
    
            return View();
        }
    
  9. Add a view page click on the ReportViewer Controller. Named the view page ReportViewer.cshtml. Add the following code to the view page.

    @using (Html.BeginForm("Login"))
         { 
               @Html.DropDownList("ddlYearMonthFormat", new SelectList(ViewBag.YearMonthFormat, "YearMonthValue", "YearMonthName"), new { @class = "DropDown" })
    
      Stock In Transit: @Html.TextBox("txtStockInTransit", "", new { @class = "LogInTextBox" })
    
     <input type="submit" onclick="return ReportValidationCheck();" name="ShowReport"
                     value="Show Report" />
    
      }
    
  10. Add an Iframe. Set the property of the Iframe as follows

    frameborder="0"  width="1000"; height="1000"; style="overflow:hidden;" scrolling="no"
    
  11. Add Following JavaScript to the viewer.

     function ReportValidationCheck() {
    
        var url = $('#hdUrl').val();
        var yearmonth = $('#ddlYearMonthFormat').val();      
        var stockInTransit = $('#txtStockInTransit').val()
    
        if (stockInTransit == "") {
            stockInTransit = 0;
        }
    
        if (yearmonth == "0") {
            alert("Please Select Month Correctly.");
        }
        else {
    
            //url = url + "dpSpYearMonth=" + yearmonth + ",rpYearMonth=" + yearmonth + ",rpStockInTransit=" + stockInTransit;
    
            url = "../Reports/ReportView.aspx?rptmode=local&reportname=ClosingInventory¶meters=dpSpYearMonth=" + yearmonth + ",rpYearMonth=" + yearmonth + ",rpStockInTransit=" + stockInTransit;
    
            var myframe = document.getElementById("ifrmReportViewer");
            if (myframe !== null) {
                if (myframe.src) {
                    myframe.src = url;
                }
                else if (myframe.contentWindow !== null && myframe.contentWindow.location !== null) {
                    myframe.contentWindow.location = url;
                }
                else { myframe.setAttribute('src', url); }
            }
        }
    
        return false;
    }
    
  12. Web.config file add the following key to the appSettings section

    add

    key="UnobtrusiveJavaScriptEnabled" value="true"

    In system.web handlers Section add the following key

     add `verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"`
    
  13. Change your data source as your own. This solution is very simple and I think every one enjoy it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文