# Reporting: Conditional Formatted Text Blocks

> Conditional text blocks in TX Text Control templates use paired IF fields with StartIF and EndIF markers to show or hide formatted content during mail merge. The DataRowMerged event removes content between markers when conditions are met, supporting tables, images, and nested fields.

- **Author:** Bjoern Meyer
- **Published:** 2015-03-13
- **Modified:** 2026-07-17
- **Description:** Conditional text blocks in TX Text Control templates use paired IF fields with StartIF and EndIF markers to show or hide formatted content during mail merge. The DataRowMerged event removes content between markers when conditions are met, supporting tables, images, and nested fields.
- **3 min read** (512 words)
- **Tags:**
  - Reporting
  - Tutorial
- **Web URL:** https://www.textcontrol.com/blog/2015/03/13/reporting-conditional-formatted-text-blocks/
- **LLMs URL:** https://www.textcontrol.com/blog/2015/03/13/reporting-conditional-formatted-text-blocks/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2015/03/13/reporting-conditional-formatted-text-blocks/llms-full.txt

---

A conditional text block is a range of formatted text that is rendered, if a specified condition is fulfilled. This text block can consist of anything that is supported by TX Text Control: Formatted text, tables, images, other merge fields or sub-templates with IncludeText fields.

The advantage of conditional text blocks is that these blocks are part of the same template and not externalized to sub-templates. Template designers can design all possible content in the same template.

The data source of our sample application has a very simple structure:

![Class structure](https://s1-www.textcontrol.com/assets/dist/blog/2015/03/13/a/assets/class_structure.webp "Class structure")

Based on 2 fields, conditional content should be rendered or not. If the field *country* equals "United States" in the current data record, a defined table should be rendered. Additionally, a specific sentence should be rendered in case the condition is fulfilled. In case the field *vip* is "True", a header should be inserted into the final report.

The sample data source has 3 records:

```
<customer>
    <customerID>1</customerID>
    <lastName>Jackson</lastName>
    <firstName>Paul</firstName>
    <country>United States</country>
    <vip>True</vip>
</customer>

<customer>
    <customerID>2</customerID>
    <lastName>Murphey</lastName>
    <firstName>Peter</firstName>
    <country>Germany</country>
    <vip>True</vip>
</customer>

<customer>
    <customerID>3</customerID>
    <lastName>Hollister</lastName>
    <firstName>Phyllis</firstName>
    <country>Italy</country>
    <vip>False</vip>
</customer>
```

The following illustration shows 3 different results created based on the same template with conditional text blocks.

[![Results of merge process with conditional text blocks](https://s1-www.textcontrol.com/assets/dist/blog/2015/03/13/a/assets/results.webp "Results of merge process with conditional text blocks")](https://s1-www.textcontrol.com/assets/dist/blog/2015/03/13/a/assets/results.webp)

In order to insert a conditional block, 2 IF-fields are required with a specific format.

```
{ IF "country" <> "United States" "%%StartIF%%" ""  }
```

```
{ IF "country" <> "United States" "%%EndIF%%" ""  }
```

The content between these 2 IF-fields is removed, if the condition is true. In our case: The content between these 2 fields is rendered, if *country* equals "United States".

The following screenshot shows the template that is used in this sample. The green-marked areas define conditional text blocks with various conditions.

![Template marked](https://s1-www.textcontrol.com/assets/dist/blog/2015/03/13/a/assets/template_marked.webp "Template marked")

Technically, the IF-fields render unique placeholders when a specific condition is fulfilled. The text between these markers is removed in the DataRowMerged event:

```
private void mailMerge1_DataRowMerged(object sender,
                TXTextControl.DocumentServer.MailMerge.DataRowMergedEventArgs e)
{
    byte[] data;
    string sPlaceholderStartIF = "%%StartIF%%";
    string sPlaceholderEndIF = "%%EndIF%%";

    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        tx.Create();
        tx.Load(e.MergedRow,
            TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        foreach (TXTextControl.IFormattedText part in tx.TextParts)
        {
            do
            {
                int start = part.Find(sPlaceholderStartIF, 0,
                    TXTextControl.FindOptions.NoMessageBox);
                int end = part.Find(sPlaceholderEndIF, start,
                    TXTextControl.FindOptions.NoMessageBox);

                if (start == -1)
                    continue;

                part.Selection.Start = start;
                part.Selection.Length = end - start + sPlaceholderEndIF.Length;
                part.Selection.Text = "";
            } while (part.Find(sPlaceholderStartIF, 0,
                TXTextControl.FindOptions.NoMessageBox) != -1);
        }

        tx.Save(out data,
            TXTextControl.BinaryStreamType.InternalUnicodeFormat);
    }

    e.MergedRow = data;
}
```

Those IF-blocks can be placed everywhere in the template including main text, headers, footers and text frames. Try this on your own and [download](https://s1-www.textcontrol.com/assets/dist/blog/2015/03/13/a/assets/tx_mailmerge_condIF.zip) the sample Visual Studio 2012 project. At least, a TX Text Control .NET for Windows Forms [trial version](https://www.textcontrol.com/product/tx-text-control-dotnet-winforms/download/) is required.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Creating Your First ASP.NET Reporting Application](https://www.textcontrol.com/blog/2020/01/01/creating-your-first-aspnet-reporting-application/llms.txt)
- [New Online Sample: Build your First Report](https://www.textcontrol.com/blog/2019/07/03/build-your-first-report/llms.txt)
- [Create your First Document with ReportingCloud](https://www.textcontrol.com/blog/2019/02/19/create-your-first-document-with-reportingcloud/llms.txt)
- [MailMerge: Starting Each Merge Block on a New Page](https://www.textcontrol.com/blog/2016/09/09/mailmerge-starting-each-merge-block-on-a-new-page/llms.txt)
- [Using MailMerge with JSON Data](https://www.textcontrol.com/blog/2015/08/04/using-mailmerge-with-json-data/llms.txt)
- [Merging Documents with RESTful Web API's](https://www.textcontrol.com/blog/2015/07/31/merging-documents-with-restful-web-apis/llms.txt)
- [Windows Forms: Printing Multiple Pages Per Sheet](https://www.textcontrol.com/blog/2015/07/24/windows-forms-printing-multiple-pages-per-sheet/llms.txt)
- [Inserting Watermark Images to All Pages Dynamically](https://www.textcontrol.com/blog/2015/07/17/inserting-watermark-images-to-all-pages-dynamically/llms.txt)
- [Reporting: Sorting Merge Block Rows by Column Name](https://www.textcontrol.com/blog/2015/07/15/reporting-sorting-merge-block-rows-by-column-name/llms.txt)
- [MailMerge with the Entity Framework Using Database First](https://www.textcontrol.com/blog/2015/06/24/mailmerge-with-the-entity-framework-using-database-first/llms.txt)
- [Checked and Unchecked Check Boxes with IF Fields](https://www.textcontrol.com/blog/2015/03/25/checked-and-unchecked-check-boxes-with-if-fields/llms.txt)
- [Reporting Best Practices and How-To Guides](https://www.textcontrol.com/blog/2015/02/05/reporting-best-practices-and-how-to-guides/llms.txt)
- [MailMerge: Master Table and Client Tables](https://www.textcontrol.com/blog/2015/01/28/mailmerge-master-table-and-client-tables/llms.txt)
- [MailMerge: Formatting Numeric Strings in Merge Fields](https://www.textcontrol.com/blog/2015/01/22/mailmerge-formatting-numeric-strings-in-merge-fields/llms.txt)
- [MailMerge: Merge CheckBoxes During the Merge Process](https://www.textcontrol.com/blog/2015/01/12/mailmerge-merge-checkboxes-during-the-merge-process/llms.txt)
- [MailMerge: Conditional INCLUDETEXT Fields](https://www.textcontrol.com/blog/2015/01/08/mailmerge-conditional-includetext-fields/llms.txt)
- [HTML5 Technical Considerations - The Concept Explained](https://www.textcontrol.com/blog/2014/10/16/html5-technical-considerations-the-concept-explained/llms.txt)
- [TX Text Control Web: Attaching Events to Ribbon Elements](https://www.textcontrol.com/blog/2014/10/07/tx-text-control-web-attaching-events-to-ribbon-elements/llms.txt)
- [TX Text Control Web: Customize the Ribbon Bar](https://www.textcontrol.com/blog/2014/10/03/tx-text-control-web-customize-the-ribbon-bar/llms.txt)
- [Text Control Web - Fundamental Concepts: The Data Source](https://www.textcontrol.com/blog/2014/10/02/text-control-web-fundamental-concepts-the-data-source/llms.txt)
- [Reporting: Removing Empty Table Rows](https://www.textcontrol.com/blog/2014/09/30/reporting-removing-empty-table-rows/llms.txt)
- [MS Word Content Controls Field Adapter Classes](https://www.textcontrol.com/blog/2014/08/05/ms-word-content-controls-field-adapter-classes/llms.txt)
- [Reporting: Merge Blocks and Structured Numbered Lists](https://www.textcontrol.com/blog/2014/07/29/reporting-merge-blocks-and-structured-numbered-lists/llms.txt)
- [Creating Avery Labels Using Text Control Reporting](https://www.textcontrol.com/blog/2014/07/22/creating-avery-labels-using-text-control-reporting/llms.txt)
- [New Online Samples for TX Text Control .NET Server](https://www.textcontrol.com/blog/2014/07/16/new-online-samples-for-tx-text-control-net-server-for-aspnet/llms.txt)
