Search code examples
c#asp.netcustom-controlsreportviewerreport-viewer2010

reportviewer.LocalReport.GetTotalPages() returns 0 or error


I'm using a user control, and added my report viewer and a custom toolbar. I want to create a custom navigation for it aswell, but for some reason when I want to check the total pages to decide whether or not to show the navigation buttons it either returns 0 or "This expression causes side effects and will not be evaluated" error..

I've ran out of ideas and not quite sure where to go from here..

<rsweb:reportviewer 
ID="rvReports" 
runat="server" ShowToolBar="False"
SizeToReportContent="True" AsyncRendering="false" />

codebehind:

rds = new Microsoft.Reporting.WebForms.ReportDataSource("dsName", myclasstoload());     
rvReports.LocalReport.DataSources.Add(rds);
rvReports.PageCountMode = PageCountMode.Actual;
rvReports.LocalReport.Refresh();
rvReports.DataBind();


if (rvReports.LocalReport.GetTotalPages() > 1)
{
 liFirst.Visible = true;
 liPrevious.Visible = true;
 liNext.Visible = true;
 liLast.Visible = true;
}

this is all on the databind event in my usercontrol (.ascx). Any help is more than appreciated.


Solution

  • This msdn question is probably your answer, the GetTotalPages() method can't be called until after the report has rendered. The relevant quote:

    The report server won't calculate the total page count until rendering the first page of the report. The ReportViewer doesn't request a page rendering from the server until the ASP.Net event PreRender. If you move the GetTotalPages call to a point after the ReportViewer.PreRender event has fired, you should get the behavior you want.

    See also the ASP.NET Page Lifecycle for reference.