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;
            }
kristenhazardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
Try using the FORMAT event.

mlmcc
0
EwaldLCommented:
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
0
kristenhazardAuthor Commented:
This is VB, can you make it work in C#?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

EwaldLCommented:
it also works for me in C#, maybe you are missing this line
this.CrystalReportViewer1.BeforeRender += new CrystalDecisions.Web.HtmlReportRender.BeforeRenderEventHandler(this.CrystalReportViewer1_BeforeRender);

in the InitializeComponent() event?

here is the whole project:


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;
            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)
            {
                  Response.Write("XXXX");
            }

      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kristenhazardAuthor Commented:
That was IT!
0
EwaldLCommented:
:-)

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.
0
kristenhazardAuthor Commented:
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.
0
EwaldLCommented:
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?
0
kristenhazardAuthor Commented:
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.
0
EwaldLCommented:
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 ;-)
0
kristenhazardAuthor Commented:
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?
0
EwaldLCommented:
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;
            }

      }
}
0
kristenhazardAuthor Commented:
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.
0
EwaldLCommented:
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
0
kristenhazardAuthor Commented:
What is this:

private CrystalReport1 crReport;

???
0
kristenhazardAuthor Commented:
I'm trying to follow your example exactly and got hung up on the private CrystalReport1 crReport;
0
kristenhazardAuthor Commented:
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.
0
EwaldLCommented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Crystal Reports

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.