ReportViewer动态加载RDLC报表文件

I have a reportviewer control. I programmatically set the local report path based on a drop down on a page. This allows the user to choose which format (RDLC file) to use when generating the report.
The first request, the report processes properly, however if I choose a different report format, the old report still renders. I'm doing a .refresh for the report...
I tried turning off Viewstate for the report, but it says that Viewstate must be true for the ReportViewer control.
If I "reenter" the page as new (such as by clicking on my menu item for that page), it will then run the first report type chosen.
I'm changing the local report path such as  ReportViewer1.LocalReport.ReportPath = "myreport1.rdlc"
or
ReportViewer1.LocalReport.ReportPath = "myreport2.rdlc"

 

Solution 1:

使用多个ReportViewer,每个ReportViewer显示不同的RDLC。根据用户的选择显示一个ReportViewer,隐藏其他的ReportViewer。就在页面中添加多个ReportViewer控件来对应多个rdlc文件,然后根据需要显示和隐藏部分ReportViewer控件。

Solution 2:

Because there is no ReportViewer.Reset() in C#, the solution is to create an new instance of the report.  The trick is to Swap out the previous ReportViewer with the New ReportViewer.

	ControlCollection cc = this.ReportViewer1.Parent.Controls;
//get index of previous ReportViewer so we can swap it out.
int prevIndex = cc.IndexOf(this.ReportViewer1);
// Remove previous ReportViewer
cc.Remove(this.ReportViewer1);
//add new instance of ReportViewer.  
ReportViewer1 = new Microsoft.Reporting.WebForms.ReportViewer();
// Reset report properties.
ReportViewer1.Height = Unit.Parse("100%");
ReportViewer1.Width = Unit.Parse("100%");
ReportViewer1.CssClass = "table";
//Add the new ReportViewer to the previous ReportViewer location.
cc.AddAt(prevIndex, ReportViewer1);
// Clear out any previous datasources.
this.ReportViewer1.LocalReport.DataSources.Clear();
//Set report mode for local processing.
ReportViewer1.ProcessingMode = ProcessingMode.Local;
// Create a new report dataset.
DataSet dataSet = new DataSet();
this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("ReportPeopleMailingList.rdlc");
// Load dataset.
try
{ 	// retrieve dataset.
dataSet = (DataSet)Session["sessionDataSetCli"];	}
catch
{
return;
}
// Assign report parameters.
Microsoft.Reporting.WebForms.ReportParameter[] parms = new Microsoft.Reporting.WebForms.ReportParameter[1];
parms[0] = new Microsoft.Reporting.WebForms.ReportParameter("title", "Clients");
ReportViewer1.LocalReport.SetParameters(parms);
// Load the dataSource.
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetPeople_adPeople", dataSet.Tables[0]));
// Refresh the ReportViewer
ReportViewer1.LocalReport.Refresh();
 
        Dim cc As ControlCollection = ReportViewer1.Parent.Controls
        Dim prevIndex As Integer = cc.IndexOf(ReportViewer1)
        cc.Remove(ReportViewer1)
        ReportViewer1 = New Microsoft.Reporting.WebForms.ReportViewer()
        ReportViewer1.Height = Unit.Parse("450px")
        ReportViewer1.Width = Unit.Parse("980px")
        cc.AddAt(prevIndex, ReportViewer1)
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.ReportPath = reportPath
        ReportViewer1.LocalReport.DataSources.Add(datasource)
        ReportViewer1.LocalReport.SetParameters(New ReportParameter() {.....})
        ReportViewer1.LocalReport.Refresh()
 
Solution 3:

Must do a

ReportViewer1.Reset()

ReportViewer1.LocalReport.Dispose()

first. Then set your reportpaths, then do a
ReportViewer1.LocalReport.Refresh()
You must have VS2005 SP1 for this.

From: 

http://blog.csdn.net/qiujiahao/archive/2007/08/09/1733415.aspx

http://objectmix.com/dotnet/304568-change-report-path-reportviewer-control.html

http://66.129.67.4/t/1257010.aspx

http://forums.asp.net/t/1183208.aspx

http://social.msdn.microsoft.com/forums/en-US/vsreportcontrols/thread/c6524b68-bdfd-4f10-848f-355bc9445323/

http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.aspnet.webcontrols/2008-07/msg00024.html

 

posted @   emanlee  阅读(7280)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
历史上的今天:
2007-09-16 SQL Server里面如何导出包含(insert into)数据的SQL脚本
点击右上角即可分享
微信分享提示