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(obj ect source, CrystalDecisions.Web.HtmlR eportRende r.BeforeRe nderEvent e)
{
// get page number
string currentPageNumber;
string lastPageNumber;
bool isLastPageKnown;
currentPageNumber = crGeneric.ViewInfo.PageNum ber.ToStri ng();
isLastPageKnown = crGeneric.ViewInfo.IsLastP ageNumberK nown;
if (isLastPageKnown)
{
lastPageNumber = crGeneric.ViewInfo.LastPag eNumber.To String();
}
else
{
lastPageNumber = "?";
}
Label_lastpagenum.Text = lastPageNumber;
Label_pagenum.Text = currentPageNumber;
}
See my code below. My CrystalReportViewer is crGeneric
private void crGeneric_BeforeRender(obj
{
// get page number
string currentPageNumber;
string lastPageNumber;
bool isLastPageKnown;
currentPageNumber = crGeneric.ViewInfo.PageNum
isLastPageKnown = crGeneric.ViewInfo.IsLastP
if (isLastPageKnown)
{
lastPageNumber = crGeneric.ViewInfo.LastPag
}
else
{
lastPageNumber = "?";
}
Label_lastpagenum.Text = lastPageNumber;
Label_pagenum.Text = currentPageNumber;
}
this works fine for me
Private Sub CrystalReportViewer1_Befor eRender(By Val source As Object, ByVal e As CrystalDecisions.Web.HtmlR eportRende r.BeforeRe nderEvent) Handles CrystalReportViewer1.Befor eRender
Response.Write("XXXXXXXXXX XXXX")
End Sub
Private Sub CrystalReportViewer1_Befor
Response.Write("XXXXXXXXXX
End Sub
ASKER
This is VB, can you make it work in C#?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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?
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?
ASKER
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(obj ect source, CrystalDecisions.Web.HtmlR eportRende r.BeforeRe nderEvent e)
{
// get page number
string currentPageNumber;
string lastPageNumber;
bool isLastPageKnown;
currentPageNumber = crGeneric.ViewInfo.PageNum ber.ToStri ng();
isLastPageKnown = crGeneric.ViewInfo.IsLastP ageNumberK nown;
if (isLastPageKnown)
{
lastPageNumber = crGeneric.ViewInfo.LastPag eNumber.To String();
}
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.
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(obj
{
// get page number
string currentPageNumber;
string lastPageNumber;
bool isLastPageKnown;
currentPageNumber = crGeneric.ViewInfo.PageNum
isLastPageKnown = crGeneric.ViewInfo.IsLastP
if (isLastPageKnown)
{
lastPageNumber = crGeneric.ViewInfo.LastPag
}
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 ;-)
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 ;-)
ASKER
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?
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.Cryst alReportVi ewer 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.Repor tSource = 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. BeforeRend er += new CrystalDecisions.Web.HtmlR eportRende r.BeforeRe nderEventH andler(thi s.CrystalR eportViewe r1_BeforeR ender);
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
private void CrystalReportViewer1_Befor eRender(ob ject source, CrystalDecisions.Web.HtmlR eportRende r.BeforeRe nderEvent e)
{
// get page number
string currentPageNumber;
string lastPageNumber;
bool isLastPageKnown;
currentPageNumber = CrystalReportViewer1.ViewI nfo.PageNu mber.ToStr ing();
isLastPageKnown = CrystalReportViewer1.ViewI nfo.IsLast PageNumber Known;
if (isLastPageKnown)
{
lastPageNumber = CrystalReportViewer1.ViewI nfo.LastPa geNumber.T oString();
}
else
{
lastPageNumber = "?";
}
Label_lastpagenum.Text = lastPageNumber;
Label_pagenum.Text = currentPageNumber;
}
}
}
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.Cryst
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
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.Repor
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.CrystalReportViewer1.
this.Load += new System.EventHandler(this.P
}
#endregion
private void CrystalReportViewer1_Befor
{
// get page number
string currentPageNumber;
string lastPageNumber;
bool isLastPageKnown;
currentPageNumber = CrystalReportViewer1.ViewI
isLastPageKnown = CrystalReportViewer1.ViewI
if (isLastPageKnown)
{
lastPageNumber = CrystalReportViewer1.ViewI
}
else
{
lastPageNumber = "?";
}
Label_lastpagenum.Text = lastPageNumber;
Label_pagenum.Text = currentPageNumber;
}
}
}
ASKER
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
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
ASKER
What is this:
private CrystalReport1 crReport;
???
private CrystalReport1 crReport;
???
ASKER
I'm trying to follow your example exactly and got hung up on the private CrystalReport1 crReport;
ASKER
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!
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!
mlmcc