Skip to main content
A newer version of this page is available. .

Handle Script Events at Runtime

  • 2 minutes to read

This example demonstrates how to handle script events at runtime.

Important

Report scripts are not secure. We recommend that you use expression bindings instead.

To accomplish this task, it is first necessary to add the required code to the collection of all scripts, stored in the XtraReport.ScriptsSource property. Then, assign the name of the script to the control’s XRControlEvents.OnBeforePrint property, and this will result in executing this code when an appropriate event is raised by the report generation engine.

using System;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;

namespace WindowsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            new ReportPrintTool(CreateReport()).ShowPreviewDialog();
        }

        private void button2_Click(object sender, EventArgs e) {
            new ReportDesignTool(CreateReport()).ShowDesignerDialog();
        }

        private XtraReport CreateReport() {
            // Create a new report.
            XtraReport1 report = new XtraReport1();

            // Add a script to a report's collection of scripts.
            report.ScriptsSource += "private void xrLabel1_BeforePrint(object sender, " +
                                    "System.Drawing.Printing.PrintEventArgs e) {\r\n  " +
                                    "((XRLabel)sender).BackColor = Color.Aqua;\r\n}";

            // Assign this script to a label's BeforePrint script.
            report.xrLabel1.Scripts.OnBeforePrint = "xrLabel1_BeforePrint";

            return report;
        }
    }
}
See Also