【发布时间】:2009-08-27 19:15:01
【问题描述】:
我在一个 aspx 页面上有一个 Crystal Report Viewer 控件,它应该有内置的分页。
当我第一次单击“下一页”按钮时,我从第 1 页移动到第 2 页,但每隔一次我单击“下一页”,报告就会重新加载到第 2 页。
【问题讨论】:
-
发布您的 ReportViewer 代码隐藏以获得有用的帮助!
我在一个 aspx 页面上有一个 Crystal Report Viewer 控件,它应该有内置的分页。
当我第一次单击“下一页”按钮时,我从第 1 页移动到第 2 页,但每隔一次我单击“下一页”,报告就会重新加载到第 2 页。
【问题讨论】:
问题可能源于在Page_Load 事件期间设置Crystal Report Viewer 控件的ReportSource。这会导致每次页面加载都会覆盖分页信息,因此“当前页面”在应该为 2 时被重新设置为 1。
作为一个简单的解决方案,您可以将设置ReportSource 的代码移动到Page_Init
【讨论】:
Dispose()覆盖中。
手动添加 Page_Init() 事件并将其连接到 InitializeCompnent() 中
this.Init += new System.EventHandler(this.Page_Init).
将Page_Load的内容移动到Page_Init()。
在 PageInIt 中添加if (!IsPostBack) 条件。
protected void Page_Init(object sender, EventArgs e) {
if (!IsPostBack)
{
ReportDocument crystalReportDocument = new ReportDocumment();
crystalReportDocument.SetDataSource(DataTableHere);
_reportViewer.ReportSource = crystalReportDocument;
Session["ReportDocument"] = crystalReportDocument;
}
else
{
ReportDocument doc = (ReportDocument)Session["ReportDocument"];
_reportViewer.ReportSource = doc;
}
}
【讨论】:
if (!Page.IsPostBack)
{
//Write your Report display code
crystalRep.Load(Server.MapPath("DueFD.rpt"));
crystalRep.SetDatabaseLogon(DB_UId, DB_Pass, Serv_Name, DB_Name);
CrystalReportViewer1.ReportSource = crystalRep;
// session code
ReportDocument doc = (ReportDocument)Session["ReportDocument"];
CrystalReportViewer1.ReportSource = doc;
}
else
{
ReportDocument doc = (ReportDocument)Session["ReportDocument"];
CrystalReportViewer1.ReportSource = doc;
}
【讨论】:
将报告源放入 Page_Init 而不是 Page_Load 以及报表参数Page_Load 我认为它可能有效
【讨论】:
对我有用!!
ReportDocument rd = new ReportDocument();
protected void Page_Load(object sender, EventArgs e)
{
string rptpath = WebConfigurationManager.AppSettings["ReportPath"].Replace("{TID}", Convert.ToString(Session["TID"]));
rd.Load(rptpath);
DataTable dtable = TemplateModel.LoadChequeData(Convert.ToString(Session["CID"]));
rd.SetDataSource(dtable);
}
protected void Page_Init(object sender, EventArgs e)
{
CrystalReportViewer1.ReportSource = rd;
}
【讨论】:
我之前在 Visual Studio 2008 上遇到过这个问题, 安装水晶报表基本服务包 1 解决了问题
【讨论】:
我也遇到过这个问题,请检查是否没有其他 WebControl 干扰 Crystal(回调问题?JavaScript?我不确定)。在我的具体情况下,Dart File Upload 和 Crystal 在放在同一个页面时相处得并不好。
【讨论】:
我将所有加载报告放在 Page_Init 而不是 Page_Load 中。它工作正常。
【讨论】:
我有一些方法可以分享。我开发了可以帮助呈现 Crystal Reports 的简单 CR 包装器。按原样使用此代码。请随意修改、扩展代码的任何部分。 下载链接https://1drv.ms/u/s!AlTLBrnU_bLUiaoUxcxNiRCZRQjWng
以及如何使用enter code here实现IDisposable接口的CR包装类的示例。在 Page_Init 中设置报表源,在 Page_Load 事件中设置报表参数,在 Page_Unload 事件中处理报表文档。
第二种方法使用静态类,并在其中呈现报表,最后处理报表文档。报告源应保存到会话变量中。请注意,第二种方法并不适合高流量的 Web 应用程序,因为使用的是静态类。如果两个用户同时运行任何报告,就会发生冲突。
方法一
using System;
using System.Linq;
using CrystalDecisions.Shared;
using System.Configuration;
using System.Web.Configuration;
using WebReports.Models.Helpers.CrystalReports; //Reference to CR wrapper
namespace YourNamespace
{
public partial class ReportForm : System.Web.UI.Page
{
protected string _serverName;
protected string _databaseName;
protected string _schemaName;
protected string _userId;
protected string _userPassword;
protected bool _integratedSecurity;
protected string _databaseType;
//Wrapper Report Document
protected CReportDocument _reportDocument;
/// <summary>
/// Load report
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Init(object sender, EventArgs e)
{
//Wrapper object
this._reportDocument = new CReportDocument();
//These settings should be initialized from i.e. web.config in Page_PreInit
this._reportDocument.ServerName = this._serverName;
this._reportDocument.DatabaseName = String.Empty;
this._reportDocument.SchemaName = this._schemaName;
this._reportDocument.DatabaseType = CReportDatabaseType.ORACLE;
this._reportDocument.UserId = this._userId;
this._reportDocument.UserPassword = this._userPassword;
this._reportDocument.IntegratedSecurity = false;
//Get report name from query string. Define Your own method to get report name
var parReportName = Request.QueryString["reportname"];
if (String.IsNullOrEmpty(parReportName))
{
lblConfigError.Text = "Crystal Report name is not being provided.";
return;
}
//Set Report file
this._reportDocument.ReportFile = Server.MapPath("~/ReportFiles/") + parReportName;
//Set Report documant
this._reportDocument.SetReportDocument();
//Get Report Document
crViewer.ReportSource = this._reportDocument.GetReportDocument();
}
protected void Page_Load(object sender, EventArgs e)
{
CReportParameter reportParameter;
//Get parameters Your own method to provide report parameters
var parFimYear = RouteData.Values["par1"];
var parFimCityCode = RouteData.Values["par2"];
if (par1 == null || par2 == null)
{
lblConfigError.Text = "Crystal Report parameters are not being provided.";
return;
}
//Define Report Parameter
reportParameter = new CReportParameter();
reportParameter.ParameterName = "@parYear";
reportParameter.ParameterValue = parFimYear;
reportParameter.crParameterValueKind = ParameterValueKind.StringParameter;
_reportDocument.AddCReportParameter(reportParameter);
reportParameter = new CReportParameter();
reportParameter.ParameterName = "@parCityCode";
reportParameter.ParameterValue = parFimCityCode;
reportParameter.crParameterValueKind = ParameterValueKind.StringParameter;
_reportDocument.AddCReportParameter(reportParameter);
//Set report parameters
this._reportDocument.SetReportParameters();
}
protected void Page_Unload(object sender, EventArgs e)
{
this._reportDocument.Dispose();
}
}
}
方法二
using System;
using System.Linq;
using CrystalDecisions.Shared;
using System.Configuration;
using System.Web.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using WebReports.Models.Helpers.CrystalReports; //Reference to CR wrapper
namespace YourNamespace
{
public partial class ReportForm : System.Web.UI.Page
{
protected string _serverName;
protected string _databaseName;
protected string _schemaName;
protected string _userId;
protected string _userPassword;
protected bool _integratedSecurity;
protected string _databaseType;
/// <summary>
/// Load report
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Init(object sender, EventArgs e)
{
CReportParameter reportParameter;
//These settings should be initialized from i.e. web.config in Page_PreInit
if (!IsPostBack)
{
if (this._databaseType == CReportDatabaseType.ORACLE.ToString())
{
//static wrapper class
CReportDocumentManager.ServerName = this._serverName;
CReportDocumentManager.DatabaseName = String.Empty;
CReportDocumentManager.SchemaName = this._schemaName;
CReportDocumentManager.DatabaseType = CReportDatabaseType.ORACLE;
CReportDocumentManager.UserId = this._userId;
CReportDocumentManager.UserPassword = this._userPassword;
CReportDocumentManager.IntegratedSecurity = false;
//Get report name from query string. Define Your own method to get report name
var parReportName = Request.QueryString["reportname"];
if (String.IsNullOrEmpty(parReportName))
{
lblConfigError.Text = "Crystal Report name is not being provided.";
return;
}
//get par1. Your own method to provide report parameters. This is from MVC application
var par1 = RouteData.Values["par1"];
//get par2
var par2 = RouteData.Values["par2"];
if (par1 == null || par2 == null)
{
lblConfigError.Text = "Crystal Report parameters are not being provided.";
return;
}
reportParameter = new CReportParameter();
reportParameter.ParameterName = "@parYear";
reportParameter.ParameterValue = par1;
reportParameter.CReportParameterValueKind = ParameterValueKind.StringParameter;
CReportDocumentManager.AddCReportParameter(reportParameter);
reportParameter = new CReportParameter();
reportParameter.ParameterName = "@parCityCode";
reportParameter.ParameterValue = par2;
reportParameter.CReportParameterValueKind = ParameterValueKind.StringParameter;
CReportDocumentManager.AddCReportParameter(reportParameter);
CReportDocumentManager.ReportFile = Server.MapPath("~/ReportFiles/") + parReportName;
ReportDocument doc = CReportDocumentManager.GetCReportDocument();
crViewer.ReportSource = doc;
Session[parReportName] = doc;
}
}
else
{
var parReportName = Request.QueryString["reportname"];
ReportDocument doc = (ReportDocument)Session[parReportName];
crViewer.ReportSource = doc;
}
}
}
}
【讨论】:
对我来说,在我从 13.0.1 升级到 13.0.10 之前,这一切正常,所以很明显发生了一些变化。
我已经尝试过上面的解决方案,但是由于我们回传更新报告参数,并且如果您没有在页面加载时设置报告参数,刷新会失败。所以截至今天,如果页面加载中没有任何内容,我还没有让它工作。
我怀疑我可能需要添加一些逻辑来检查更改的参数值,并且仅在更改时才刷新报告。
最后我删除了我们的“侧边栏”页面上/下链接,并使用了查看器上的链接。我看不到任何方法,因为可以使用页面挂钩来使其工作 - 设置报表参数会重置分页,但不设置意味着页面不会加载。
【讨论】: