CrystalReportViewer 按钮使用 MVC 框架损坏 [英] CrystalReportViewer Buttons Broken using MVC Framework

查看:21
本文介绍了CrystalReportViewer 按钮使用 MVC 框架损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 MVC 框架(第 5 版)和 CrystalReportViewer 控件来显示我们的报表.我无法让报表查看器控件顶部的任何按钮起作用.

We are using the MVC framework (release 5) and the CrystalReportViewer control to show our reports. I cannot get any of the buttons at the top of the report viewer control to work.

如果我正在处理HoursSummary"报告.如果我将鼠标悬停在 IE 中报表查看器上的任何按钮上,页面底部显示的链接是../HoursSummary".这将创建一个 'http://localhost/HoursSummary' 的 url.没有HoursSummary"控制器,所以我不断收到 404 错误.

If I'm working with the report 'HoursSummary'. If I hover over any of the buttons on the report viewer in IE the displayed link at the bottom of the pages is '../HoursSummary'. This creates a url of 'http://localhost/HoursSummary'. There is no 'HoursSummary' controller so I keep receiving 404 errors.

  • 我想我想重定向到'http://localhost/reports/HoursSummary',因为我确实有报告控制器.如果这是正确的方法,有人知道我应该在 CrystalReportViewer 控件上设置哪个属性来实现吗?
  • 有没有更简单的方法来处理这种情况?
  • I believe I want to redirect to 'http://localhost/reports/HoursSummary' since I do have a reports controller. If this is the correct method does anyone know which property I should set on the CrystalReportViewer control to make that happen?
  • Is there an easier method to handle this situation?

推荐答案

我们能够让报表查看器正常工作,并且在过去的几个月里一直在生产中使用它,没有任何问题.

We were able to get the report viewer to work and have been using it for the past few months in production without any issues.

  • 我们有一个报告控制器,其中列出了指向我们要运行的报告的链接
  • 单击其中一个链接将对后端进行 ajax 调用并返回部分页面,我们可以在其中填写所需的所有参数.
  • 参数填写完毕后,我们将表单提交到' eportsName of Report'.
  • 回到 Reports 控制器,我们调用 SQL,返回我们的数据,然后调用另一个名为完整报告"的视图
  • 完整报表"视图只有一个水晶报表查看器控件,它会自动获取我们通过 ViewData 传递给它的报表数据、填充报表、呈现它并将其发送给用户

一切似乎都很好.

更新

我在上面最初列出的步骤中添加了一些代码和说明.我遗漏的关键项目是最终视图背后有一些代码,因此它可以与 Crystal Reports 一起使用.后面的代码很少,但需要.为了使 Crystal Reports 正常工作,您最终会得到以下文件:

I've added some code and clarification to the steps I originally listed above. The key item I left out was there is some code behind with the final View so it will work with Crystal Reports. The code behind is minimal, but needed. For Crystal Reports to work you are going to end up with the following files:

  • 用于设计报告的布局文件.rpt
  • 包含 Crystal Reports Report 控件的 aspx 文件.这是包含一些代码的文件.

有关如何创建可与 Crystal Reports 一起使用的视图的详细信息:

Details on how to create a View that will work with Crystal Reports:

  • 使用 Crystal Reports 设计器创建报表布局.生成的文件将是一个 .rpt 文件.为了这个例子,我们称这个文件为 AllJobsSummaryReportLayout.rpt.
  • 在设计报表时,为数据库字段"选择一个业务实体或 DTO,该业务实体或 DTO 保存从 SQL 返回的结果.
  • 顺便说一句,我们的系统中有一些数据传输对象 (DTO),它们仅包含标量值和字符串,这些 DTO 没有智能.当控制器被调用时,它会调用模型,大多数这些报告的模型返回一个 DTO 列表,然后我们将其传递给要呈现的视图.这些 DTO 不知道如何查询自己、显示自己,它们只包含从 SQL 返回的实际值,然后由其他人呈现.
  • 一旦布局水晶报表文件AllJobsSummaryReportLayout.rpt,我们设计我们的Controller.在控制器中,我们获取运行报告所需的任何参数,调用模型,模型返回我们的 DTO 列表,如下面的控制器片段所示:

  • Create the layout of your report using the Crystal Reports Designer. The resulting file will be an .rpt file. For the sake of this example, let's call this file AllJobsSummaryReportLayout.rpt.
  • While designing your report, for the 'Database Fields' select one of the business entities or DTOs that holds the results coming back from SQL.
  • A quick aside, we have a few data transfer objects (DTOs) in our system that contain nothing more than scalar values and strings, there is no intelligence in these DTOs. When the Controller is called, it calls the Model, the Model for most of these reports returns a List of DTOs that we then pass to the View to be rendered. These DTOs do not know how to query themselves, display themselves, they only contain actual values returned from SQL that someone else then renders.
  • Once the layout Crystal Report file is completed, the AllJobsSummaryReportLayout.rpt, we design our Controller. In the Controller we take in any parameters needed to run the report, call the Model, Model returns our list of DTOs, as seen in the snippet below from the Controller:

var reportViewData = model.AllJobsSummaryQuery(startDate, endDate);
if (0 != reportViewData.Count())
{
    var report = new AllJobsSummaryReportLayout();
    report.SetDataSource(reportViewData);
    report.SetParameterValue("startDate", startDate);
    report.SetParameterValue("endDate", endDate);
    ViewData["ReportData"] = report;
    returnView = "AllJobsSummaryView";
}
else
    returnView = "noReportView";
return View(returnView);

  • 请注意这里的几项,我们正在创建一个变量报告",它是我们在上面创建的 Crystal Report 布局文件 AllJobsSummaryReportLayout.rpt 的一种类型.

  • Note a couple of items here, we are creating a varible 'report' that is a type of the Crystal Report layout file, AllJobsSummaryReportLayout.rpt, that we created above.

    创建报告"变量后,我们设置数据源值和所需的任何参数,并将项目捆绑到 ViewData 中.

    Once we created the 'report' variable we set the data source values and any parameters we need, and bundle the item up into the ViewData.

    现在让我们看看 AllJobsSummaryView.aspx.该文件上有一个带有 Crystal Reports 查看器的表单和一个代码隐藏文件:

    Now let's take a look at AllJobsSummaryView.aspx. This file has a form on it with a Crystal Reports Viewer and a code behind file:

    
    
    <a href="/Reports" id="Report"><<返回报告主页面</a><br/><CR:CrystalReportViewer ID="ReportViewer" runat="server" AutoDataBind="True" EnableDatabaseLogonPrompt="False"EnableParameterPrompt="False" HasCrystalLogo="False" DisplayGroupTree="False"HasDrillUpButton="False" HasToggleGroupTreeButton="False" HasViewList="False"HasSearchButton="False" EnableDrillDown="False" EnableViewState="True"Height="50px" ReportSourceID="CrystalReportSource1" Width="350px"/><CR:CrystalReportSource ID="CrystalReportSource1" runat="server"><报告文件名="AllJobsSummaryReportLayout.rpt"></报告></CR:CrystalReportSource></div></表格>

    <%@ Page Title="All Jobs Summary Report" Language="C#" AutoEventWireup="true" CodeBehind="AllJobsSummaryView.aspx.cs" Inherits="V.Views.Reports.AllJobsSummaryView"%> <%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %> <form id="form1" runat="server"> <div> <a href="/Reports" id="Report"><< Return to Report Main Page</a><br /> <CR:CrystalReportViewer ID="ReportViewer" runat="server" AutoDataBind="True" EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" HasCrystalLogo="False" DisplayGroupTree="False" HasDrillUpButton="False" HasToggleGroupTreeButton="False" HasViewList="False" HasSearchButton="False" EnableDrillDown="False" EnableViewState="True" Height="50px" ReportSourceID="CrystalReportSource1" Width="350px" /> <CR:CrystalReportSource ID="CrystalReportSource1" runat="server"> <Report FileName="AllJobsSummaryReportLayout.rpt"> </Report> </CR:CrystalReportSource> </div> </form>

    • 还有文件后面的代码:

      • And the code behind file:

         using System;
         using System.Collections.Generic;
         using System.Linq;
         using System.Web;
         using System.Web.Mvc;
        
         namespace V.Views.Reports
         {
            public partial class AllJobsSummaryView : ViewPage
            {
                protected void Page_Init(object sender, EventArgs e)
                {
                    ReportViewer.ReportSource = ViewData["ReportData"];
                }
        
                protected void Page_Unload(object sender, EventArgs e)
                {
                    ((AllJobsSummaryReportLayout)ViewData["ReportData"]).Close();
                    ((AllJobsSummaryReportLayout)ViewData["ReportData"]).Dispose();
                }
            }
         }
        

      • Page_Unload 是关键,如果没有它,Crystal Reports 将生成错误您已超出管理员设置的最大报告数."

      • The Page_Unload is key, without it you will have an error generated by Crystal Reports 'You have exceeded the max number of reports set by your administrator.'

        这种方法在生产环境中已经运行了两年多.

        This method is still working in a production environment for well over two years now.

        这篇关于CrystalReportViewer 按钮使用 MVC 框架损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆