Using Events

Add events to layout elements to provide custom event-driven behavior. Two common uses of events used while creating reports are the PlaceHolder event handler and the DocumentLayout class's ReportDataRequired event handler. The PlaceHolder class dynamically places items such as barcodes or images on a document, while the ReportDataRequired dynamically adds data to a document.

PlaceHolder

The PlaceHolder class adds items such as barcodes or images to a details section of a report based on the current layout data. For example, the following code demonstrates adding an event handler using the LaidOut event to dynamically place a barcode.

public void ReportExample()
{
    // Create the document's layout from a DLEX template
    DocumentLayout layout = new DocumentLayout("Placeholder.dlex");

    //  Retrieve the place holder and attach the event handler
    PlaceHolder barcodePlaceHolder = (PlaceHolder)layout.GetElementById("BarcodePlaceholder");
    barcodePlaceHolder.LaidOut += new PlaceHolderLaidOutEventHandler(BarcodePlaceHolder_LaidOut);

    // Specify the data to be used
    LayoutData layoutData = new LayoutData();
    layoutData.Add("PreparedFor", "Alex Smith");
    layoutData.Add("Orders", listOfOrders);

    // Layout the document and save the PDF
    Document document = layout.Layout(layoutData);
    document.Draw(outputFilePath);
}

public void BarcodePlaceHolder_LaidOut(object sender, PlaceHolderLaidOutEventArgs e)
{
    Code128 barcode = new Code128(e.LayoutWriter.Data["OrderID"].ToString(), 0, 0, e.ContentArea.Height);
    barcode.X += (e.ContentArea.Width - barcode.GetSymbolWidth()) / 2;
    barcode.ShowText = false;
    e.ContentArea.Add(barcode);
}
Public Sub void PlaceHolderExample()
    ' Create the document's layout from a DLEX template
    Dim MyLayout As DocumentLayout = New DocumentLayout("Placeholder.dlex")

    ' Retrieve the place holder And attach the event handler
    Dim MyBarcodePlaceHolder As PlaceHolder = MyLayout.GetElementById("BarcodePlaceholder")
    AddHandler MyBarcodePlaceHolder.LaidOut, AddressOf BarcodePlaceholder_LaidOut

    ' Specify the data to be used
    Dim MyLayoutData As LayoutData = New LayoutData()
    MyLayoutData.Add("PreparedFor", "Alex Smith")
    MyLayoutData.Add("Orders", MyListOfOrders)

    ' Layout the document And save the PDF
    Dim MyDocument As Document = MyLayout.Layout(MyLayoutData)
    MyDocument.Draw(outputFilePath)
End Sub

Private Sub BarcodePlaceholder_LaidOut(ByVal sender As Object, ByVal e As PlaceHolderLaidOutEventArgs)
    Dim MyBarcode As Code128 = New Code128(e.LayoutWriter.Data("OrderID").ToString(), 0, 0, e.ContentArea.Height)
    MyBarcode.X += (e.ContentArea.Width - MyBarcode.GetSymbolWidth()) / 2
    MyBarcode.ShowText = False
    e.ContentArea.Add(MyBarcode)
End Sub

The sample code first retrieves the layout element with the id of BarcodePlaceholder as a PlaceHolder. It then attaches the PlaceHolder_LaidOut callback function to the LaidOut event. Then, while the document is being created or "laid out", the event is called, and the code in the fired event creates the barcode and places it in the correct document location.

ReportDataRequired

The ReportDataRequired event handler triggers an event and executes a callback function every time a report or sub-report element is parsed in a DLEX file.

In this topic