如何在关闭选项卡或页面时处理水晶报表

本文关键字:处理 报表 水晶 选项 | 更新日期: 2023-09-27 18:27:10

如何在关闭选项卡或页面时处理水晶报告?这是我实现报表查看器的方式。有人能提出关闭和处理报告的正确方法吗。

protected void Page_Init(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {
            bool isValid = true;
            // Setting ReportName
            string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();
            // Setting Report Data Source     
            var rptSource = System.Web.HttpContext.Current.Session["rptSource"];
            if (string.IsNullOrEmpty(strReportName)) // Checking is Report name provided or not
            {
                isValid = false;
            }

            if (isValid) // If Report Name provided then do other operation
            {
                rd = new ReportDocument();
                if (Session["ReportDocument"] != null)
                {
                    rd = Session["ReportDocument"] as ReportDocument;
                    rd.Load(strReportName);
                    CrystalReportViewer1.ReportSource = rd;
                }
                else
                {                        
                    string stringReportPath = strReportName;
                    //Loading Report
                    rd.Load(stringReportPath);
                    // Setting report data source
                    if (rptSource != null && rptSource.GetType().ToString() != "System.String")
                        rd.SetDataSource(rptSource);
                    Session["ReportDocument"] = rd;
                    CrystalReportViewer1.ReportSource = rd;
                }
                Session["ReportName"] = "";
                Session["rptSource"] = "";
            }
            else
            {
                Response.Write("<H2>Nothing Found; No Report name found</H2>");
            }
        }
        else
        {
            ReportDocument doc = (ReportDocument)Session["ReportDocument"];
            CrystalReportViewer1.ReportSource = doc;
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
}

编辑:在关闭选项卡或页面时,是否有任何方法可以处理报告?如果我试图像这个一样在Page_Unload中处理报告

protected void Page_Unload(object sender, EventArgs e)
{
if (rd != null)
 {
   rd.Close();
   rd.Dispose();
 }
}

这样就无法导航到报告的其他页面。那么,有人能提出调用Close()和Dispose()方法的正确方法吗?

编辑:还有一个问题读起来很相似,但实际上不同,因为这个问题解释了什么是空引用异常以及如何修复它。但我想要的是如何在关闭选项卡或页面时关闭和处理crytal报告。

如何在关闭选项卡或页面时处理水晶报表

        private bool disposed = false;
       ReportDocument doc=new ReportDocument ();
       ReportDocument rd=new ReportDocument ();
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    doc.Dispose(); //context means your crystal report document object.
                    rd.Dispose(); 
                }
            }
            this.disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

您的代码

protected void Page_Init(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                bool isValid = true;
                // Setting ReportName
                string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();
                // Setting Report Data Source     
                var rptSource = System.Web.HttpContext.Current.Session["rptSource"];
                if (string.IsNullOrEmpty(strReportName)) // Checking is Report name provided or not
                {
                    isValid = false;
                }

                if (isValid) // If Report Name provided then do other operation
                {
                    rd = new ReportDocument();
                    if (Session["ReportDocument"] != null)
                    {
                        rd = Session["ReportDocument"] as ReportDocument;
                        rd.Load(strReportName);
                        CrystalReportViewer1.ReportSource = rd;
                    }
                    else
                    {                        
                        string stringReportPath = strReportName;
                        //Loading Report
                        rd.Load(stringReportPath);
                        // Setting report data source
                        if (rptSource != null && rptSource.GetType().ToString() != "System.String")
                            rd.SetDataSource(rptSource);
                        Session["ReportDocument"] = rd;
                        CrystalReportViewer1.ReportSource = rd;
                    }
                    Session["ReportName"] = "";
                    Session["rptSource"] = "";
                }
                else
                {
                    Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            else
            {
                doc=new ReportDocument();
                doc = (ReportDocument)Session["ReportDocument"];
                CrystalReportViewer1.ReportSource = doc;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }

     finally
       {
         Dispose();
       }
    }