Link to home
Start Free TrialLog in
Avatar of kristenhazard
kristenhazard

asked on

Crystal Reports 10 .NET BeforeRender event never gettting triggered

I want to set my page number info (custom navigation) so BeforeRender I want actually retrieve the page numbers But I can't get any of the crystalreportviewer events to actually trigger. Do I need to initialize somewhere? Where?

See my code below. My CrystalReportViewer is crGeneric

            private void crGeneric_BeforeRender(object source, CrystalDecisions.Web.HtmlReportRender.BeforeRenderEvent e)
            {
                  // get page number
                  string currentPageNumber;
                  string lastPageNumber;
                  bool isLastPageKnown;
                  currentPageNumber = crGeneric.ViewInfo.PageNumber.ToString();
                  isLastPageKnown = crGeneric.ViewInfo.IsLastPageNumberKnown;
                  if (isLastPageKnown)
                  {
                        lastPageNumber = crGeneric.ViewInfo.LastPageNumber.ToString();
                  }
                  else
                  {
                        lastPageNumber = "?";
                  }
                  Label_lastpagenum.Text = lastPageNumber;
                  Label_pagenum.Text = currentPageNumber;
            }
Avatar of Mike McCracken
Mike McCracken

Try using the FORMAT event.

mlmcc
this works fine for me


Private Sub CrystalReportViewer1_BeforeRender(ByVal source As Object, ByVal e As CrystalDecisions.Web.HtmlReportRender.BeforeRenderEvent) Handles CrystalReportViewer1.BeforeRender
        Response.Write("XXXXXXXXXXXXXX")
End Sub
Avatar of kristenhazard

ASKER

This is VB, can you make it work in C#?
ASKER CERTIFIED SOLUTION
Avatar of EwaldL
EwaldL

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was IT!
:-)

I find the most conenient way to get such events to work is to go into the viewer's property window, click the events flash in there, and then just double click on the event name. this creates teh entry in the InitializeComponent() method automatically for me.
Very cool, but I don't see the event flash.  I clicked on my viewer then looked in my properties window,but don't see anything to do with events. Please advise.
have you go tthe viewer selected when looking at the properties window?

ie if you have the form selected you should see these icons the the top of the properties window
- Categorised
- Alphabetic
- Properties
- Property Pages

if i then select the viewer on the form, the events icon (flash) appears in between the properties and property pages icon.

in case that's not happening for you, what can you see at the top of the property window?
Found it thank you.

Well I don't know if you can answer this but here goes. I thought I could set a label control text based on my BeforeRender event code, but it doesn't seem to be working.

private void crGeneric_BeforeRender(object source, CrystalDecisions.Web.HtmlReportRender.BeforeRenderEvent e)
            {
                  // get page number
                  string currentPageNumber;
                  string lastPageNumber;
                  bool isLastPageKnown;
                  currentPageNumber = crGeneric.ViewInfo.PageNumber.ToString();
                  isLastPageKnown = crGeneric.ViewInfo.IsLastPageNumberKnown;
                  if (isLastPageKnown)
                  {
                        lastPageNumber = crGeneric.ViewInfo.LastPageNumber.ToString();
                  }
                  else
                  {
                        lastPageNumber = "?";
                  }
                  Label_lastpagenum.Text = lastPageNumber;
                  Label_pagenum.Text = currentPageNumber;
            }

Basically the last 2 lines are not working. I have 2 label and I was trying to set the .Text.

Any thoughts would be very much appreciated.
if you want to build your own navigation bar, then better put the above code into the afterrender event. Before the report is rendered the viewer won't know anything about the report.

other than that i guess the code seems fine. but let me know in case it doesnt work and i will have a look. please explain then what exactly doesnt work, or what happens instead ;-)
OK.

I'm buidling my own navigation bar but I'm building it in my asp page not the reportviewer.

Both before and after render work in terms of getting the correct page number information. My problem is this. I'm trying to write that page number information into my ASP page. I have two label web controls that I am trying to set the .text property on. But it's like I don't have access to them in the BeforeRender code. I know I have access to those label controls in the Page_Load code but not in the Before or AfterRender code.

Am I making sense?
do you mean you get an error when trying to use Label_pagenum or Label_lastpagenum in either of the events? could it be that the labels are not declared on class level for some reason?

i tried your code out and it works just fine for me, see below. i placed two labels onto the viewer's form and set them through code

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace BeforeRenderEventCs
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
            protected System.Web.UI.WebControls.Label Label_pagenum;
            protected System.Web.UI.WebControls.Label Label_lastpagenum;
            private CrystalReport1 crReport;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);

                  crReport = new CrystalReport1();
                  CrystalReportViewer1.ReportSource = crReport;
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.CrystalReportViewer1.BeforeRender += new CrystalDecisions.Web.HtmlReportRender.BeforeRenderEventHandler(this.CrystalReportViewer1_BeforeRender);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void CrystalReportViewer1_BeforeRender(object source, CrystalDecisions.Web.HtmlReportRender.BeforeRenderEvent e)
            {
                  // get page number
                  string currentPageNumber;
                  string lastPageNumber;
                  bool isLastPageKnown;
                  currentPageNumber = CrystalReportViewer1.ViewInfo.PageNumber.ToString();
                  isLastPageKnown = CrystalReportViewer1.ViewInfo.IsLastPageNumberKnown;
                  if (isLastPageKnown)
                  {
                        lastPageNumber = CrystalReportViewer1.ViewInfo.LastPageNumber.ToString();
                  }
                  else
                  {
                        lastPageNumber = "?";
                  }
                  
                  Label_lastpagenum.Text = lastPageNumber;
                  Label_pagenum.Text = currentPageNumber;
            }

      }
}
Very strange. When I debug I see that the text value is changing but what is rendered in my web browser is the initial default value.  I'm at a loss.
have you tried my above code? it works for me.

could it be that you are usingthe page_load method for instantiating the report and passing it to the viewer? use the OnInit() for this instead
What is this:

private CrystalReport1 crReport;

???
I'm trying to follow your example exactly and got hung up on the private CrystalReport1 crReport;
OK, I got it working with a very basic example of hardcoding my source. In my real life example I am setting the whole report doc into the sesion and retrieving it on init so there must be something strange there. I'll figure it out. Thanks for all your help.
that is a report added to the project

Project ->Add new item -> Crystal report

then follow the prompts.

Sorry, but I got to log off, probably for the rest of the week.

Best of luck!