如何在 MVC.NET 2 中使用 ReportViewer 2010

发布于 2024-09-16 13:42:51 字数 35 浏览 0 评论 0原文

基本上我想知道如何将报告嵌入到 MVC.Net 2 中。

Basically I want to know how to embed a report into MVC.Net 2.

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

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

发布评论

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

评论(1

情定在深秋 2024-09-23 13:42:51

我做了这个问题,因为网上没有足够的信息,或者信息不完整,所以你可以开始工作。

您必须知道的第一件事是报表查看器是一个 Web 控件,因此您不能在 MVC 上使用它,因此您要做的第一件事是创建一个 Web 表单,以便您可以添加报表查看器。在我完成的示例中,我使用的是 Visual Studio 2010。

Web 表单如下所示:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Report Viewer</title>
</head>
<body>
    <div style="width: auto;">
        <form id="form1" runat="server" style="width: 100%; height: 100%;">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <rsweb:ReportViewer ID="rptViewer" runat="server" Width="100%" Height="100%" AsyncRendering="False"
            SizeToReportContent="True">
        </rsweb:ReportViewer>
        </form>
    </div>
</body>
</html>

Web 表单背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var reportServer = ConfigurationManager.AppSettings["ReportServer"].ToString();
        var reportPath = ConfigurationManager.AppSettings["ReportPath"].ToString();

        rptViewer.ServerReport.ReportServerUrl = new Uri(reportServer);
        rptViewer.ShowToolBar = false;
        rptViewer.ServerReport.ReportPath = reportPath + Request.QueryString["ReportName"];
        List<ReportParameter> parameters = new List<ReportParameter>();
        string[] keys = Request.QueryString.AllKeys;
        for (int i = 1; i < Request.QueryString.Count; i++)
        {
            parameters.Add(new ReportParameter(keys[i], Request.QueryString[i]));
        }
        this.ReportViewer1.ServerReport.SetParameters(parameters);
        this.ReportViewer1.ProcessingMode = ProcessingMode.Remote;
        this.ReportViewer1.ShowParameterPrompts = false;
        this.ReportViewer1.ShowPromptAreaButton = false;
        this.ReportViewer1.ServerReport.Refresh();

        rptViewer.ProcessingMode = ProcessingMode.Remote;
        rptViewer.ServerReport.Refresh();
    }
}

现在我们需要使用 MVC。我们有两个选择,一是打开一个带有 JavaScript 弹出窗口的新窗口,或者使用 iframe。

我将同时执行这两项操作,以便您可以对视图有一个最好的了解:

<iframe id="Frame1" src="<%= Session["Url"] %>" width="230" height="230" frameborder="0"></iframe> **1
 function OpenReports(name) {
            var width = (screen.availWidth - 700).toString();
            var height = (screen.availHeight - 100).toString();
            window.open('/Reporting/Reports.aspx?ReportName=' + name,
                 'mywindow', 'width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=yes,status=no,' +
                'menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes' + ',screenX=0,screenY=0,left=0,top=0');

        } **2

**1 SessionURL 是一个会话变量,其中包含我们要显示的路径和报告。这也是使用 iframe 嵌入报告的第一种方法

**2 /Reporting/Reports.aspx 是我们刚刚完成的 Web 表单的路径。这是第二种方式,打开一个新窗口。

在控制器中:

 public ActionResult ViewName()
 {
    Session["Url"] = "/Reporting/Reports.aspx?ReportName=Report44";
    return View();
 }**1

**1 /Reporting/Reports.aspx 是我们刚刚完成的 Web 表单的路径。

另外,如果您使用的是 Report Viewer 10,请记住 web.config 中的此功能:

<system.web>
    <httpHandlers>
      <add verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </httpHandlers>
</system.web>

希望本教程对某人有所帮助:)

I did the question, because there's not enough information on the web, or the information is not that complete so you can start working.

The first thing you have to know is that the report viewer is a webcontrol so you can't use it on MVC, so first thing you have to do is create a web form so you can add the report viewer. In the example I've done I'm using Visual Studio 2010.

The webform looks like this:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Report Viewer</title>
</head>
<body>
    <div style="width: auto;">
        <form id="form1" runat="server" style="width: 100%; height: 100%;">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <rsweb:ReportViewer ID="rptViewer" runat="server" Width="100%" Height="100%" AsyncRendering="False"
            SizeToReportContent="True">
        </rsweb:ReportViewer>
        </form>
    </div>
</body>
</html>

The code behind of the web form:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var reportServer = ConfigurationManager.AppSettings["ReportServer"].ToString();
        var reportPath = ConfigurationManager.AppSettings["ReportPath"].ToString();

        rptViewer.ServerReport.ReportServerUrl = new Uri(reportServer);
        rptViewer.ShowToolBar = false;
        rptViewer.ServerReport.ReportPath = reportPath + Request.QueryString["ReportName"];
        List<ReportParameter> parameters = new List<ReportParameter>();
        string[] keys = Request.QueryString.AllKeys;
        for (int i = 1; i < Request.QueryString.Count; i++)
        {
            parameters.Add(new ReportParameter(keys[i], Request.QueryString[i]));
        }
        this.ReportViewer1.ServerReport.SetParameters(parameters);
        this.ReportViewer1.ProcessingMode = ProcessingMode.Remote;
        this.ReportViewer1.ShowParameterPrompts = false;
        this.ReportViewer1.ShowPromptAreaButton = false;
        this.ReportViewer1.ServerReport.Refresh();

        rptViewer.ProcessingMode = ProcessingMode.Remote;
        rptViewer.ServerReport.Refresh();
    }
}

Now we need to use the MVC. We have two options one, open a new window with a javascript pop up or use an iframe.

I will do both so you can have a best idea on the View:

<iframe id="Frame1" src="<%= Session["Url"] %>" width="230" height="230" frameborder="0"></iframe> **1
 function OpenReports(name) {
            var width = (screen.availWidth - 700).toString();
            var height = (screen.availHeight - 100).toString();
            window.open('/Reporting/Reports.aspx?ReportName=' + name,
                 'mywindow', 'width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=yes,status=no,' +
                'menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes' + ',screenX=0,screenY=0,left=0,top=0');

        } **2

**1 SessionURL is a Session Variable with the path and report we want to show. Also this is the first way to do embed the Report using an iframe

**2 /Reporting/Reports.aspx is the path of the webform we just did eaelier. This is the second way, opening a new window.

In the Controller:

 public ActionResult ViewName()
 {
    Session["Url"] = "/Reporting/Reports.aspx?ReportName=Report44";
    return View();
 }**1

**1 /Reporting/Reports.aspx is the path of the webform we just did eaelier.

Also If your are using Report Viewer 10 please remember this feature in the web.config:

<system.web>
    <httpHandlers>
      <add verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </httpHandlers>
</system.web>

Hope all this tutorial helps somebody :)

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