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.將參數應用到數據查詢中,這樣就可以通過頁面查詢更新報表的顯示數據了。

 

有錯誤請指正。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM