Report Scripts

April 15, 2012

When designing a report you can use scripts to handle special requirments. Scripts are program commands, placed within the event handlers of the report, band, and control elements. When the corresponding event occurs, the script code runs.  Events occur when the report is previewed, printed, or exported.

Although when in the Report Designer, virtually any task can be accomplished without scripting (Conditionally Change a Control’s Appearance, Conditionally Change a Label’s Text and Conditionally Hide Bands), scripting is made available to extend the standard functionality even more.  The Scripts Tab allows you to manage your report’s scripts. It provides centralized access to all the scripts being written for a report or any of its elements (bands and controls).  The built-in scripts validation capability is provided in this tab. After clicking Validate, the result is displayed in the Scripts Errors Panel.  Scripts are saved to a file along with the report’s layout.

The report scripts may be written in one of the following languages that the .NET framework supports – C#, Visual Basic and J#. Since J# is not installed with the framework installation, by default, make sure it is present before writing code in it. The scripting language is specified via the Script Language property of the Report object.

Script execution is performed in the following way: The report engine generates a temporary class in memory. The names of the variables are defined by the Name properties of the controls and objects they represent. When the script is preprocessed, its namespace directives are cut from the script code and added to the namespace, where the temporary class is defined. After preprocessing, all scripts are placed in the code of the temporary class. Then, the resulting class is compiled in memory, and its methods are called when events occur. Scripting offers many advantages: you can declare classes (they will become inner classes), variables, methods, etc. A variable declared in one script is accessible in another script, because it is, in fact, a variable of the temporary class.

The Script References property of the Report object specifies the full paths (including the file names) to the assemblies that are referenced in the scripts used in a report. These paths should be specified for all the assemblies that are included in scripts via the using (C#), Imports (Visual Basic) or import (J#) directives. Usually, you don’t need include any assembles, because most standard assemblies that you may require in scripts are already referenced by the program.