分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

在mvc视图中实现rdlc报表展示

发布时间:2023-09-06 01:09责任编辑:林大明关键词:暂无标签

需求:在view视图页面中嵌入rdlc报表,rdlc的xml为动态传入的xml字符串。本项目是基于abp框架

可能出现问题:

1、rdlc报表是由asp.net的服务器控件ReportViewer来支持的,view视图不能直接使用服务器控件

2、ReportViewer需要通过aspx页面来承载,并在服务端事件中完成对控件的xml绑定、datatable绑定

3、由于是基于abp框架的项目,不能在aspx.cs后台页面中直接实例化IxxAppService接口的实现类


想达到的效果如下图:

 上部分为报表的筛选区域,下部分为rdlc报表展示内容。


本次实现的思路为:

1、以view视图为主页面,在视图的上部分完成筛选信息的展示和绑定;

2、视图下部分嵌入一个iframe,iframe地址指向一个aspx页面,该页中实现ReportViewer控件的赋值和绑定

视图页面的代码参考如下:

 ???<div class="tab-content" style="width:100%;height:100%"> ???????<!--筛选区域--> ???????<div role="tabpanel" class="tab-pane active" id="navMenu" style="padding-top: 4px;padding-bottom: 1px;"> ???????????<div class="collapse row" id="filterHts" style="margin-right: 2px;"> ???????????</div> ???????????<div id="divTools" style="padding: 0 5px 5px;cursor: pointer;height: 33px;line-height:28px" title="点击展开搜索条件区"> ???????????????<span id="searchTools" onclick="$(‘#filterHts‘).click();" style="display:none;float:left"></span> ???????????????<span id="ExternalTools" style="float:left;"></span> ???????????????<span id="spTools" style="float:left;"></span> ???????????????<span id="spanSearch" style="float:right;line-height:12px;height:12px"><i class="fa fa-chevron-down" title="点击展开搜索条件区" aria-hidden="true" style="cursor:pointer;margin:0px 0px 0px 5px;color:#76838f"></i><i class="fa fa-chevron-up" aria-hidden="true" title="点击关闭搜索条件区" style="cursor:pointer;display:none;margin-left:5px;color:#76838f"></i></span> ???????????</div> ???????</div> ???????<div style="width:100%;height:500px"> ???????????<iframe style="width:100%;height:500px" id="ifm" src="~/Rdlc/rdlc.aspx"></iframe> ???????</div> ?????</div>
View Code

 rdlc.aspx页面代码参考:

<%@ Page Language="C#" CodeBehind="rdlc.aspx.cs" Inherits="Easyman.Web.Rdlc.rdlc" %><%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ???<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ???<title></title></head><body> ???<form id="form1" runat="server"> ???????<div> ???????????<asp:HiddenField runat="server" ID="rpName" /> ???????????<asp:HiddenField runat="server" ID="xmlStr" /> ???????????<asp:HiddenField runat="server" ID="hidDataTable" /> ???????????<asp:ScriptManager ID="ScriptManager1" runat="server"> ???????????</asp:ScriptManager> ???????????<rsweb:ReportViewer ID="reportViewer1" runat="server" ?DocumentMapWidth="100%" Font-Names="Verdana" ????????????????Font-Size="8pt" WaitMessageFont-Names="Verdana" ?AsyncRendering="False" SizeToReportContent="True" ????????????????WaitMessageFont-Size="14pt" Width="100%" Height="100%" ZoomMode="FullPage" > ???????????</rsweb:ReportViewer> ???????</div> ???????<div style="display:none"> ???????????<asp:Button ID="SearchBtn" runat="server" OnClick="SearchBtn_Click" Text="查询" /> ???????</div> ???</form></body></html>
View Code

当点击了查询按钮之后,在视图页中实现对 rdlc.aspx页面中的隐藏控件赋值(aspx页面中隐藏控件有:xml、datatable的json数据)

隐藏控件赋值之后触发 rdlc.aspx页面查询按钮事件SearchBtn_Click

关键js如下:

 ???????$("#ifm").contents().find("#rpName").val($("#Name").val()); ???????$("#ifm").contents().find("#xmlStr").val(Encrypt(rdlcReport.RdlcXml)); ???????$("#ifm").contents().find("#hidDataTable").val(data.responseText); ???????//endregion ???????$("#ifm").contents().find("#SearchBtn").click();//触发RDLC子页面的查询按钮 ???????$("#ifm").contents().find("#SearchBtn").hide();
View Code

 rdlc.aspx页面查询后台事件方法SearchBtn_Click的代码如下:

 ???????protected void SearchBtn_Click(object sender, EventArgs e) ???????{ ???????????string xmlStr = EncryptHelper.AesDecrpt(this.xmlStr.Value); ???????????string tbJson = this.hidDataTable.Value; ???????????string rpName = this.rpName.Value; ???????????DataTable dt = new DataTable(); ???????????if(!string.IsNullOrEmpty( tbJson)&&tbJson!="[]") ???????????{ ???????????????dt = JSON.ToDataTable(tbJson); ???????????} ???????????reportViewer1.LocalReport.DataSources.Clear(); ???????????reportViewer1.LocalReport.DisplayName = rpName; ???????????reportViewer1.LocalReport.LoadReportDefinition(GenerateRdlc(xmlStr)); ???????????ReportDataSource reportDataSource = new ReportDataSource("DataSet1",dt); ???????????reportViewer1.LocalReport.DataSources.Add(reportDataSource); ???????????reportViewer1.LocalReport.Refresh(); ???????????????????}
View Code

  rdlc.aspx.cs后台的完整代码如下:

using Abp.Domain.Repositories;using Easyman.Common;using Easyman.Common.Helper;using Easyman.Domain;using Easyman.Service;using Microsoft.Reporting.WebForms;using System;using System.Collections.Generic;using System.Data;using System.IO;using System.Linq;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace Easyman.Web.Rdlc{ ???public partial class rdlc : System.Web.UI.Page ???{ ???????protected void Page_Load(object sender, EventArgs e) ???????{ ???????} ???????/// <summary> ???????/// 以内存流形式返回rdlc报表配置信息 ???????/// </summary> ???????/// <param name="inStr"></param> ???????/// <returns></returns> ???????public MemoryStream GenerateRdlc(string inStr) ???????{ ???????????byte[] b = Encoding.UTF8.GetBytes(inStr); ???????????MemoryStream ms = new MemoryStream(b); ???????????return ms; ???????} ???????protected void SearchBtn_Click(object sender, EventArgs e) ???????{ ???????????string xmlStr = EncryptHelper.AesDecrpt(this.xmlStr.Value); ???????????string tbJson = this.hidDataTable.Value; ???????????string rpName = this.rpName.Value; ???????????DataTable dt = new DataTable(); ???????????if(!string.IsNullOrEmpty( tbJson)&&tbJson!="[]") ???????????{ ???????????????dt = JSON.ToDataTable(tbJson); ???????????} ???????????reportViewer1.LocalReport.DataSources.Clear(); ???????????reportViewer1.LocalReport.DisplayName = rpName; ???????????reportViewer1.LocalReport.LoadReportDefinition(GenerateRdlc(xmlStr)); ???????????ReportDataSource reportDataSource = new ReportDataSource("DataSet1",dt); ???????????reportViewer1.LocalReport.DataSources.Add(reportDataSource); ???????????reportViewer1.LocalReport.Refresh(); ???????????????????} ???}}
View Code

总体思路为:

1)在view视图页中实现筛选区域的值绑定和获取;

2)并在筛选条件下查询满足的datatable数据并转化为json字符串赋值给子页面rdlc.aspx的隐藏控件;

3)在view视图中给子页面rdlc.aspx隐藏控件xml配置信息赋值

4)在rdlc.aspx子页面的后台事件方法SearchBtn_Click中直接获取隐藏控件中的datatable数据和rdlc的配置xml字符串信息,然后绑定到ReportViewer控件

在mvc视图中实现rdlc报表展示

原文地址:http://www.cnblogs.com/senyier/p/7482401.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved