我在aspx页面上有一个Crystal Report Viewer控件,该页面应该具有内置分页功能。
当我第一次点击“下一页”按钮时,我从第1页移到第2页,但是每隔一次我点击“下一页”,报告就会重新加载到第2页。
答案 0 :(得分:24)
问题可能源于在ReportSource
事件期间设置Crystal Report Viewer控件的Page_Load
。这导致每次页面加载都会覆盖页面调度信息,因此当“页面”应该为2时,“当前页面”会重新设置为1.
作为一种简单的解决方案,您可以将设置ReportSource
的代码移动到Page_Init
答案 1 :(得分:5)
手动添加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;
}
}
答案 2 :(得分:4)
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;
}
答案 3 :(得分:1)
将报告源放在 Page_Init 而不是Page_Load中 并报告参数 Page_Load 我认为它的工作原理可能
答案 4 :(得分:1)
对我有用!!
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;
}
答案 5 :(得分:0)
我之前在visual studio 2008上遇到过这个问题, 安装crystal report基本服务包1解决了问题
答案 6 :(得分:0)
我也有这个问题,检查是否有其他WebControl干扰Crystal(回调问题?JavaScript?我不确定)。在我的特定情况下,当放入同一页面时,Dart File Upload和Crystal相处得不好。
答案 7 :(得分:0)
我将所有加载报告放在Page_Init而不是Page_Load中。它运作正常。
答案 8 :(得分:0)
对我来说,这一点工作正常,直到我从13.0.1升级到13.0.10,所以显然发生了一些变化。
我已经尝试了上述解决方案,但由于我们回发更新报告参数这一事实很复杂,如果您不在页面加载上设置报告参数,则刷新失败。所以截至今天,我还没有在页面加载中没有任何东西的情况下工作。
我怀疑我可能需要添加一些逻辑来检查更改的参数值,并且只有在更改后才刷新报告。
最后我删除了我们的"侧栏"页面向上/向下链接并使用查看器上的链接。鉴于页面挂钩可以使它工作,我看不出任何办法 - 设置报告参数会重置分页,但不设置它们意味着页面不会加载。
答案 9 :(得分:0)
我有一些方法可以分享。我开发了一个简单的CR包装器,可以帮助渲染Crystal Reports。按原样使用此代码。请随意修改,扩展代码的任何部分。 下载链接https://1drv.ms/u/s!AlTLBrnU_bLUiaoUxcxNiRCZRQjWng
示例如何使用enter code here
实现IDisposable接口的CR包装类来利用包装器。在Page_Init中设置报告源并在Page_Load事件中报告参数并在Page_Unload事件中处理报告文档。
第二种方法使用静态类并在1处呈现报告,最后处理报告文档。报告源应保存到会话变量中。请注意,第二种方法并不适用于高流量Web应用程序,因为使用了静态类。如果两个用户同时运行任何报告,则会发生冲突。
方法1
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();
}
}
}
方法2
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;
}
}
}
}