简体   繁体   中英

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.

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.

to get the pages as for me, I had to render the report in pdf, then using pdfreader class from Itextsharp library to get total pages

var bytes=viewer.Render("PDF");
PdfReader reader = new PdfReader(bytes);
var pageCount = reader.NumberOfPages

this works well if you want to render your rdlc in pdf format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM