加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-2 – 如何在MVC.NET 2中使用ReportViewer 2010

发布时间:2020-12-16 07:15:28 所属栏目:asp.Net 来源:网络整理
导读:基本上我想知道如何将报告嵌入到MVC.Net 2中. 解决方法 我提出了这个问题,因为网上没有足够的信息,或者信息不完整,所以你可以开始工作了. 您必须要知道的第一件事是报表查看器是一个web控件,因此您无法在MVC上使用它,因此您首先要做的是创建一个Web表单,以便
基本上我想知道如何将报告嵌入到MVC.Net 2中.

解决方法

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

您必须要知道的第一件事是报表查看器是一个web控件,因此您无法在MVC上使用它,因此您首先要做的是创建一个Web表单,以便添加报表查看器.在我已经完成的示例中,我使用的是Visual Studio 2010.

webform看起来像这样:

<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是我们刚刚做过的webform的路径.这是第二种方式,打开一个新窗口.

在控制器中:

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

** 1 /Reporting/Reports.aspx是我们刚刚做过的webform的路径.

此外,如果您使用的是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>

希望所有这个教程可以帮助别人:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读