xtrareports-8470-feature-guide-to-devexpress-reports-reporting-api-use-report-scripts-handle-script-events-at-runtime.md
This example demonstrates how to handle script events at runtime.
Important
Report scripts are not secure and are disabled by default. 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.ComponentModel.CancelEventArgs 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;
}
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms
Imports DevExpress.XtraReports.UI
Namespace WindowsApplication1
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles button1.Click
CType(New ReportPrintTool(CreateReport()), ReportPrintTool).ShowPreviewDialog()
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles button2.Click
CType(New ReportDesignTool(CreateReport()), ReportDesignTool).ShowDesignerDialog()
End Sub
Private Function CreateReport() As XtraReport
' Create a new report.
Dim report As New XtraReport1()
' Add a script to a report's collection of scripts.
report.ScriptsSource &= "private void xrLabel1_BeforePrint(object sender, " & _
"System.ComponentModel.CancelEventArgs e) {" & Constants.vbCrLf & " " & _
"((XRLabel)sender).BackColor = Color.Aqua;" & Constants.vbCrLf & "}"
' Assign this script to a label's BeforePrint script.
report.xrLabel1.Scripts.OnBeforePrint = "xrLabel1_BeforePrint"
Return report
End Function
End Class
End Namespace
See Also