[英]How do I release a Crystal Report object when an asp.net page closes
I have a asp .net 3.5 app using Crystal Reports for Visual Studio 2010. 我有一个使用Crystal Reports for Visual Studio 2010的ASP .NET 3.5应用程序。
I have an issue with a report preview screen. 我的报告预览屏幕有问题。
If I don't dispose of the report object in the page unload the object stays active and never goes away until the application pool of the site is recycled. 如果我不处理页面卸载中的报表对象,则该对象将保持活动状态,直到站点的应用程序池被回收后才消失。 Also, the temp files created by the report are not cleaned up at that point.
此外,此时尚未清除报表创建的临时文件。
If I dispose of the report object in the form unload, the object is released and the temp files are removed. 如果我以卸载形式处理报表对象,则会释放该对象并删除临时文件。
But if I do that, then the report object is gone and if the user selects the option to export the report (from the viewer control on the page that is still open) I get the error: 但是,如果我这样做,那么报表对象就消失了,并且如果用户选择了导出报表的选项(从仍处于打开状态的页面上的查看器控件中),我将收到错误消息:
not set to an instance of on object
In my ReportViewer page I just have the viewer control. 在我的ReportViewer页面中,我只有查看器控件。
In the page load I create the report object and pass it to the viewer. 在页面加载中,我创建报告对象并将其传递给查看器。 This all works great.
这一切都很好。
My disposal code is the a method that handles MyBase.Unload
, right now my problem is that this event is fired even when the page isn't being closed and I cant find an event that would be like the closing event on a normal window. 我的处置代码是处理
MyBase.Unload
的方法,现在我的问题是,即使未关闭页面也触发了此事件,并且我无法在正常窗口中找到类似于关闭事件的事件。
What do i need to do to properly dispose of the report object, but not until after the viewer is actually closed? 我需要怎么做才能正确处理报表对象,但是要等到查看器实际关闭之后才可以?
I finally found an answer to this issue that works properly. 我终于找到了正确解决该问题的答案。
This can only be accomplished by using a script and a little extra code 这只能通过使用脚本和一些额外的代码来完成
in the report viewer's .aspx file i had to add a script and an onunload call 在报表查看器的.aspx文件中,我必须添加一个脚本和一个onunload调用
<script language="javascript" type="text/javascript" >
function cleanupCR() {
__doPostBack('', 'DisposeOfCR');
}
</script>
and 和
<body onunload="cleanupCR()">
then in the report viewers .aspx.vb file i modified my post back catch to handel the disposing of the crystal reports objects 然后在报表查看器.aspx.vb文件中,我修改了我的post back catch以处理水晶报表对象的处理
If Me.IsPostBack Then
rpt = Session(Me.HIDDENRPTKEY.Value)
CrystalReportViewer1.ReportSource = rpt
If (Me.Request.Params("__EVENTARGUMENT") = "DisposeOfCR") Then
DisposeOfReportObject()
End If
Return
End If
and finally in the same i added the method to dispose of the report objects: 最后,我同样添加了处理报表对象的方法:
If rpt IsNot Nothing Then
Try
If rpt.Database IsNot Nothing Then
rpt.Database.Dispose()
End If
Catch
End Try
rpt.Close()
rpt.Dispose()
End If
if the report database object is invalid the rpt.Database test will throw an error and for now i just put the empty catch there to allow for that. 如果报表数据库对象无效,则rpt.Database测试将引发错误,而现在我只是将空catch放在那儿,以允许这样做。 i will have to find the proper way to test that value so it won't throw an error.
我将必须找到测试该值的正确方法,以便它不会引发错误。
Why not writing the 为什么不写
Report.Close();
Report.Dispose();
in the Page_Unload? 在Page_Unload中?
This will clear them as well, and you can use them till the time they are (Export them etc). 这也将清除它们,您可以使用它们直到它们消失(导出它们等)。 Its working at my end.
它在我的工作。
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose(); //context means your crystal report document object.
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.