Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get total number of pages in Report Viewer

Status
Not open for further replies.

JCruz063

Programmer
Feb 21, 2003
716
US
Hi All,
I'm developing an ASP.NET application that allows users to view and download reports. I'm using Crystal Reports 10. To view reports on the browser, I'm using a Web Forms Viewer (CrystalDecisions.Web.CrystalReportViewer). This Web Forms viewer displays some nasty navigational buttons which I hate, and so I decided to create my own navigational "Prev/Next" buttons. Thus, I have two button controls on my form and, not surprisingly, their text properties are set to "Prev Page" and "Next Page", respectively.

I would like to disable the Prev button when the user is at the first page because there are no more pages before the first one (duh!). To do this, I keep track of the current page in a Session variable and disable the "Prev" button when the page becomes 1. Like this:
Code:
[COLOR=blue]int[/color] page = ([COLOR=blue]int[/color])Session["page"];
[COLOR=blue]if[/color] (page > 1)
{
  page--;
  [COLOR=green]// reportVr is the Web Forms viewer[/color]
  reportVr.ShowNthPage(page);
  Session["page"] = page;
  [COLOR=blue]if[/color] (page == 1)
    [COLOR=green]// Disable "Prev" button[/color]
    btnPrev.Enabled = [COLOR=blue]false[/color];    
}
Alright, the code is straight forward; if the page becomes one, disable the Prev button so that the user knows he's in the first page.

Now, enough of that. My problem comes in when I have to do this for the "Next" button. I have to disable it when I'm at the last page (so that the user knows there are no more pages to view), but I don't know what the last page is because the Web Forms Viewer doesn't seem to expose a property for it. Therefore, I can't disable the "Next" button when I'm at the last page. Check this code out:
Code:
[COLOR=blue]int[/color] page = ([COLOR=blue]int[/color])Session["page"];
[COLOR=blue]if[/color] (page < [COLOR=red][hmmm ???][/color])
{
  page++;
  [COLOR=green]// reportVr is the Web Forms viewer[/color]
  reportVr.ShowNthPage(page);
  Session["page"] = page;
  [COLOR=blue]if[/color] (page == [COLOR=red][hmmm ???][/color])
    [COLOR=green]// Disable "Next" button[/color]
    btnNext.Enabled = [COLOR=blue]false[/color];    
}
When the user gets to the last page, if he clicks next he'll just get the same page again. And let me tell you, he could click as many times as he wants and he'll get the same page over and over.

Can anybody tell me how I can get the total number pages in a Web Forms Viewer?

Thanks!

JC

We don't see things as they are; we see them as we are. - Anais Nin
 
Hi, I'm looking at similar problem - searching to get Current page and Total Pages from Crystal Viewer...
Did you find an answer?

Cheers,
Lady Linet
 
I did, although it may not be the most efficient one. Here's how you would get the total number of pages in the Crystal Report's Viewer:

The trick is to use the Viewer's Navigate event, which is called everytime a different page is shown. Its e argument contains a property called NewPageNumber which returns the number of the page that will be displayed. Given this, you could call the Viewer's ShowLastPage() method when the form first loads and get the total number of pages in the Navigate event. Then, after storing the total number of pages (perhaps in a Session variable), simply call the Viewer's ShowFirstPage() method to go back to the first page. Here's how the code would look:
Code:
[COLOR=green]// Declare a variable to hold number of pages[/color]
[COLOR=blue]private int[/color] _totalPages = 0;

[COLOR=green]// In the Page_Load event...[/color]
[COLOR=blue]if [/color](!Page.IsPostBack)
{
[COLOR=green]   ...[/color]
[COLOR=green]   // This will trigger the Navigate event
   where you get _totalPages[/color]
   reportVr.ShowLastPage();
   
   Session["totalPages"] = _totalPages;

[COLOR=green]   // Show first page again[/color]
   reportVr.ShowFirstPage();
}
[COLOR=blue]else[/color]
{
   _totalPages = ([COLOR=blue]int[/color])Session["totalPages"];
[COLOR=green]   ...[/color]
}

[COLOR=green]// The Nagivate event looks like this:[/color]
[COLOR=blue]private void[/color] reportVr_Navigate([COLOR=blue]object[/color] sender, 
    CrystalDecisions.Web.NavigateEventArgs e)
{
[COLOR=green]   // Get _totalPages only once[/color]
   [COLOR=blue]if[/color] (_totalPages == 0)
      _totalPages = e.NewPageNumber;
}
The code in the Nagivate event ignores any page shift if the variable _totalPages has a non-zero value. This is the behavior that I want but your case may be different and you should adjust the code in this event based on your needs.

Hope this helps!

JC

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top