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/devv/p/3255343.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值