MVC4 Razor视图下使用iframe加载RDLC报表

MVC视图下默认是不支持服务器端控件的,所以,为了能够通过report viewer控件加载报表,需要在MVC视图添加嵌入的页面。

起初在stackoverflow上找到一个解决方案,见这里。不过这里的解决方案的一个最大缺陷是,不支持页面导航,也就是只能显示报表的第一页。

因为,原作者也说了,MVC页面下不支持导航控件的post back,也就无法正常工作。

看到评论里有人说iframe才可以解决多页报表的问题,于是试了试,最终成功搞定。

1.在项目中新建立WebForm1.aspx,并修改Global.asax.cs使得aspx页面可以直接访问。

routes.IgnoreRoute("WebForms/{weform}");

2.在需要加载报表的试图页面添加iframe定义,并添加对WebForm1.aspx的引用。

<iframe id="ifr" src="../WebForms/WebForm1.aspx" width="900px" height="700px" frameborder=0>

</iframe>

3.WebForm1.aspx中添加report viewer控件,并引用设计的报表。

 

<form id="Form1" runat="server">

  <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

  <asp:UpdatePanel ID="uid"  runat="server">

    <ContentTemplate>

      <rsweb:ReportViewer ID="ReportViewer1" runat="server" AsyncRendering="false"

        Height="800px" Width="900px" ShowFindControls="False"

        ShowBackButton="False" PageCountMode="Actual"></rsweb:ReportViewer>

    </ContentTemplate>
  </asp:UpdatePanel>

</form>

 

4.后台代码的Page_Load方法中,添加数据源。

ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/Report1.rdlc");

ReportDataSource reportDataSource = new ReportDataSource("DataSet1", yourdatasource);

ReportViewer1.LocalReport.DataSources.Add(reportDataSource);

ReportViewer1.LocalReport.Refresh();

5.现在报表就可以显示了,不过为了让报表可以接收页面的参数,需要添加一点代码。在视图页面的刷新方法中,根据不同的过滤条件,为iframe指定不同的url地址。

function reloadData() {

        var fil = filterfield.value;

        var urlstr = ifr.location.href + '?filter=' + fil;

        ifr.window.location.href=urlstr;

    }

6.在WebForm1.aspx的后台代码中,获取url参数。

string filter= Request.QueryString["filter"];

7.将参数应用到数据查询中,这样就可以通过页面查询更新报表的显示数据了。

原文地址:https://www.cnblogs.com/lgx5/p/5580523.html