none
How does GetTotalPages work ? RRS feed

  • Question

  • I’d like to know how I can get the number of page of a report using the reportViewer webform.


    Here is my code:

    protected override void OnPreRender(EventArgs e) 
     // Set the processing mode for the ReportViewer to Remote 
     reportViewer.ProcessingMode = ProcessingMode.Remote; 
     reportViewer.AsyncRendering = false
     
     // Set the report server URL and report path 
     reportViewer.ServerReport.ReportServerUrl = new Uri("http://" + s.strServerName + "/reportserver"); 
     reportViewer.ServerReport.ReportPath = reportPath
     
     // Set the report parameters for the report 
     reportViewer.ServerReport.SetParameters(parameterSelected);                      
     reportViewer.CurrentPage = 2
     
     int k = reportViewer.ServerReport.GetTotalPages(); 


    But k is always equal to zero…
    I’ve read that I should use the RenderingComplete event, but I did not understand how it works for webforms.


    Thanks

    Julien

    Tuesday, September 16, 2008 2:33 PM

Answers

  • 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.

    The delay to PreRender prevents report pages from being rendered multiple times during a postback.  For example, if the code you posted is in the OnInit event handler and caused a report page rendering, then it is possible that a later postback event to handle a report drillthrough or bookmark would change the report or target page and cause the viewer to throw out the first rendering.

    Tuesday, September 16, 2008 2:59 PM
    Moderator

All replies

  • 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.

    The delay to PreRender prevents report pages from being rendered multiple times during a postback.  For example, if the code you posted is in the OnInit event handler and caused a report page rendering, then it is possible that a later postback event to handle a report drillthrough or bookmark would change the report or target page and cause the viewer to throw out the first rendering.

    Tuesday, September 16, 2008 2:59 PM
    Moderator
  • Ok, I put the GetTotalPages in the Render and it works fine: k is equal to the number of pages of the report, but since I have added theses lines, the report is not rendered anymore :(

    protected override void Render(HtmlTextWriter output) 
        int k = reportViewer.ServerReport.GetTotalPages(); 



    Wednesday, September 17, 2008 7:51 AM
  • At last I've put it in  the page OnPreRenderComplete and it works :)
    Wednesday, September 17, 2008 11:03 AM
  • The report didn't render with your override of Render because you never call base.Render(output);.
    Wednesday, September 17, 2008 4:29 PM
    Moderator
  • Maybe I've talk too quickly. Indded my solution did not work so weel so  at last I try

    protected override void OnPreRender(EventArgs e)  
    {  
       // Set the processing mode for the ReportViewer to Remote  
       reportViewer.ProcessingMode = ProcessingMode.Remote;  


       // Set the report server URL and report path  
       reportViewer.ServerReport.ReportServerUrl = new Uri("http://" + s.strServerName + "/reportserver");  
       reportViewer.ServerReport.ReportPath = reportPath;  
      
       // Set the report parameters for the report  
       reportViewer.ServerReport.SetParameters(parameterSelected);                       
       reportViewer.CurrentPage = 1;  
    }  
     
    protected override void Render(HtmlTextWriter output) 
       base.Render(output); 
       int k = reportViewer.ServerReport.GetTotalPages(); 
    }

     But k is still equal to zero :(
    Monday, September 22, 2008 11:09 AM
  • My mistake, I forget to put
    reportViewer.AsyncRendering = false;

    :)
    Monday, September 22, 2008 4:09 PM
  • Maybe I've talk too quickly. Indded my solution did not work so weel so  at last I try

    protected override void OnPreRender(EventArgs e)  
    {  
       // Set the processing mode for the ReportViewer to Remote  
       reportViewer.ProcessingMode = ProcessingMode.Remote;  


       // Set the report server URL and report path  
       reportViewer.ServerReport.ReportServerUrl = new Uri("http://" + s.strServerName + "/reportserver");  
       reportViewer.ServerReport.ReportPath = reportPath;  
      
       // Set the report parameters for the report  
       reportViewer.ServerReport.SetParameters(parameterSelected);                       
       reportViewer.CurrentPage = 1;  
    }  
     
    protected override void Render(HtmlTextWriter output) 
       base.Render(output); 
       int k = reportViewer.ServerReport.GetTotalPages(); 
    }

     But k is still equal to zero :(

    base.Render(output); should be at the end. Works for me.


    Friday, June 9, 2017 4:43 PM