Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

14.15 Runtime Report Rendering Events

Runtime Report Rendering Events

The ASP.NET viewer has events that let you track the progress of the report’s rendering process. This is useful when your application needs to know the exact moment a report is finished rendering. For example, you could write an entry to a report log file upon completion or queue up another report for processing on a high-volume site. To do this, you can add code to the AfterRender() event. The events, listed in Table 14-5, are triggered in the following order.

BeforeRender()

BeforeRenderContent()

BeforeRenderObject() – repeated for each report object

AfterRenderObject() – repeated for each report object

AfterRenderContent()

AfterRender()

I think that the most interesting event is the BeforeRenderObject() event. It is triggered before every report object is rendered to the report. This gives you the ability to evaluate every report object as it is printed and add javascript to different browser events. For example, you can add code to the OnClick() javascript event. By using theBeforeRender() event in conjunction with javascript events, you take report customization to a whole new level.

The following code sample demonstrates how to show a pop-up window to the user when they click on text object on the report. To simplify the example, the alert message only displays the text shown in the text box.

Listing 14-10. Adding an Alert() action to a text field’s OnClick() event.
[VB.NET]
Protected Sub CrystalReportViewer1_BeforeRenderObject(ByVal source As Object, ByVal e As _
CrystalDecisions.Web.HtmlReportRender.BeforeRenderObjectEvent) Handles CrystalReportViewer1.BeforeRenderObject
Dim myText As CrystalDecisions.CrystalReports.ViewerObjectModel.TextObjectInstance
If TypeOf e.Object Is CrystalDecisions.CrystalReports.ViewerObjectModel.TextObjectInstance Then
myText = CType(e.Object, CrystalDecisions.CrystalReports.ViewerObjectModel.TextObjectInstance)
e.OnClickHandler = "alert('" & myText.getObjectText & "');"
End If
End Sub
[C#]
protected void CrystalReportViewer1_BeforeRenderObject(object source,
CrystalDecisions.Web.HtmlReportRender.BeforeRenderObjectEvent e)
{
CrystalDecisions.CrystalReports.ViewerObjectModel.TextObjectInstance myField;
if (e.Object is CrystalDecisions.CrystalReports.ViewerObjectModel.TextObjectInstance)
{
myField = (CrystalDecisions.CrystalReports.ViewerObjectModel.TextObjectInstance)e.Object;
e.OnClickHandler = "alert('" + myField.getObjectText() + "');";
}
}

The code first declares an object variable of type TextObjectInstance. This is within the class CrystalDecisions.CrystalReports.ViewerObjectModel. This class has a data type defined for every possible report object that can be printed on the report. You can review the help file for an official list of all the type names.

Since the BeforeRender() event is called before every report object is rendered, the code first checks the data type of the current object being rendered. The current object being printed is stored in the argument e.Object. If the types match, e.Object is cast as a TextObjectInstance data type and assigned to the object variable myText.

The last step assigns the alert() JavaScript code to the OnClickHandler() object. You can assign any JavaScript here or you could even call JavaScript functions in a common library and pass the current object as a parameter.

Unfortunately, the render events cast report objects as classes derived from the ViewerObjectModel class. The properties of this class are primarily read-only and are only useful for determining which report object is currently being printed. If they were derived from the ReportDefinition class (see Chapter 15), you could modify the properties of the report object prior to it being printed and make your reports completely customizable. But this isn’t the case.

Being able to add JavaScript to each report object is a every powerful feature to have. If you were really serious about modifying the report objects during runtime, you could write JavaScript functions which take the current object and modify its properties via the JavaScript DOM. Doing it this way lets you use the browser to update the HTML rather than setting properties of the report object.