【发布时间】:2014-02-27 06:01:21
【问题描述】:
我正在使用 Visual Studio 2010 和 Crystal Report 13.0
报表查看器正确显示第一页。但是下一页按钮不起作用。如果我单击下一页按钮,则它会显示加载消息并仅停留在那里。所有报告查看器控件都不起作用。
请帮帮我
【问题讨论】:
-
这是发生在本地机器还是服务器上。你可以看看下面的链接。 stackoverflow.com/questions/53347/…
我正在使用 Visual Studio 2010 和 Crystal Report 13.0
报表查看器正确显示第一页。但是下一页按钮不起作用。如果我单击下一页按钮,则它会显示加载消息并仅停留在那里。所有报告查看器控件都不起作用。
请帮帮我
【问题讨论】:
我找到了解决方案。
手动添加 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;
}
}
【讨论】:
您可以将 CrystalReportViewer AutoDataBind 属性与 DataBinding 事件结合使用,而不是手动识别要绑定的时刻。
自动绑定定义:
// Summary:
// Boolean. Gets or sets whether automatic data binding to a report source is
// used. If the value is set to True, the DataBind() method is called after
// OnInit() or Page_Init().
[Category("Data")]
[DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool AutoDataBind { get; set; }
您可以通过以下方式使用此属性:
在 ASPX 中:
<CR:CrystalReportViewer ID="_reportViewer" runat="server" AutoDataBind="true" OnDataBinding="_reportViewer_DataBinding" />
在 ASPX.CS 中:
protected void _reportViewer_DataBinding(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;
}
}
【讨论】:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
int pageIndex =((CrystalDecisions.Shared.PageRequestContext)
CrystalReportViewer1.RequestContext).PageNumber;
//Bind Report with filter and datasource
string ControID = GetPostBackControlName(this);
//get and check Crystal Report Navigation button event after Bind Report
if (ControID == null)
{
((CrystalDecisions.Shared.PageRequestContext)
CrystalReportViewer1.RequestContext).PageNumber = pageIndex;
}
}
}
public string GetPostBackControlName(Page Page)
{
Control control = null;
string ctrlname = Page.Request.Params["__EVENTTARGET"];
if (ctrlname != null && ctrlname != String.Empty)
{
control = Page.FindControl(ctrlname);
}
else
{
string ctrlStr = String.Empty;
Control c = null;
foreach (string ctl in Page.Request.Form)
{
if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
{
ctrlStr = ctl.Substring(0, ctl.Length - 2);
c = Page.FindControl(ctrlStr);
}
else
{
c = Page.FindControl(ctl);
}
if (c is System.Web.UI.WebControls.Button ||
c is System.Web.UI.WebControls.ImageButton)
{
control = c;
break;
}
}
}
if (control == null)
{
return null;
}
else
{
return control.ID;
}
}
【讨论】: