?
Solved

Crystal Reports 10 .NET BeforeRender event never gettting triggered

Posted on 2004-11-18
18
Medium Priority
?
1,201 Views
Last Modified: 2012-05-05
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;
            }
0
Comment
Question by:kristenhazard
  • 9
  • 8
18 Comments
 
LVL 101

Expert Comment

by:mlmcc
ID: 12617450
Try using the FORMAT event.

mlmcc
0
 
LVL 13

Expert Comment

by:EwaldL
ID: 12619462
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
 

Author Comment

by:kristenhazard
ID: 12619492
This is VB, can you make it work in C#?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Accepted Solution

by:
EwaldL earned 2000 total points
ID: 12619618
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
 

Author Comment

by:kristenhazard
ID: 12619720
That was IT!
0
 
LVL 13

Expert Comment

by:EwaldL
ID: 12619861
:-)

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
 

Author Comment

by:kristenhazard
ID: 12619895
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
 
LVL 13

Expert Comment

by:EwaldL
ID: 12619951
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
 

Author Comment

by:kristenhazard
ID: 12620026
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
 
LVL 13

Expert Comment

by:EwaldL
ID: 12620099
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
 

Author Comment

by:kristenhazard
ID: 12620199
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
 
LVL 13

Expert Comment

by:EwaldL
ID: 12620344
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
 

Author Comment

by:kristenhazard
ID: 12620444
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
 
LVL 13

Expert Comment

by:EwaldL
ID: 12620464
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
 

Author Comment

by:kristenhazard
ID: 12620528
What is this:

private CrystalReport1 crReport;

???
0
 

Author Comment

by:kristenhazard
ID: 12620534
I'm trying to follow your example exactly and got hung up on the private CrystalReport1 crReport;
0
 

Author Comment

by:kristenhazard
ID: 12620677
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
 
LVL 13

Expert Comment

by:EwaldL
ID: 12620680
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question