This is the first of a couple of blog entires I am writing for web-based ASP.NET Crystal Reports. Both of these blog entires concentrate on having more control over the actual Report Generation to be able to replace the default Crystal Report toolbar, with a custom one, that integrates better with the overall design of your web application.

Now, thinking of replacing the default Crystal Report toolbar, the first thing you would need to provide users is a way of navigation between the Report pages without the toolbar. And for this, you further need to know the total number of pages that the report would have based on the data loaded into it, and the print settings you have specified.

As it turns out, you do not have a direct NumberOfPages, or a similar property on the Crystal Report Viewer control. But if you analyze this control, you will notice that it provides a ViewInfo object (of type CrystalDecisions.Web.ViewInfo), that should provide sufficient information for you to be able to think of creating a custom toolbar for the report.

In particular, it provides the LastPageNumber and PageNumber properties, which respectively give you the total number of pages, and the current page number of your Crystal Report.

However, be aware that these properties are not set correctly, until the Crystal Report Viewer control is finished rendering the Report (or in other terms, is finished generating the markup for the report), after which you should be able to get correct values for these properties.

So, here’s the code for getting your hands on correct values of these properties:

 

{syntaxhighlighter brush: csharp;fontsize: 100; first-line: 1; }protected void Page_Init (object sender, EventArgs e)
{
this.CrystalReportViewer1.AfterRender += this.CrystalReportViewer1_AfterRender;

……
}

protected void CrystalReportViewer1_AfterRender (object sender, EventArgs e)
{
CrystalDecisions.Web.ViewInfo v=this.CrystalReportViewer1.ViewInfo;

int totalPages = v.LastPageNumber;
int currentPage = v.PageNumber;
……..
}
{/syntaxhighlighter}

As you can see, it is pretty straight forward to get your hands on these values, just keep in mind to do this in the AfterRender event of the Report Viewer control(you can also use the event AfterRenderObject, but that is raised multiple times after each particular object of the Report has rendered).

Another property you might be interested in is the ViewInfo object’s IsLastPageNumberKnown property, which returns true or false depending upon whether the Viewer has completed rendering the Report, and therefore knows the Last Page Number for the report.