MailMerge: Rendering Conditional Table Rows
The MailMerge class supports repeating merge blocks that are repeated based on the given data rows. Sub-blocks can be rendered conditionally based on value comparisons of the parent data table. This article shows how to add such a condition.

Typically, a merge block is used to repeat content such as table rows or paragraphs. Another purpose of a merge block is to control the rendering of a complete sub-block such as a table row. Those sub-blocks can be rendered conditionally based on value comparisons of the parent data table.
Consider the following JSON data that is merged into the template that is shown further below:
[
{
"order_id": 123,
"products": [
{
"name": "Product A",
"price": 123.12,
"available": false,
"outofstock": [
{
"leadtime": 5
}
]
},
{
"name": "Product B",
"price": 223.00,
"available": false,
"outofstock": [
{
"leadtime": 10
}
]
},
{
"name": "Product C",
"price": 555.00,
"available": true,
"outofstock": [
{
"leadtime": 7
}
]
}
]
}
]
The following screenshot shows the template with 2 nested merge blocks that are highlighted in blue and purple.
The blue merge block shall be rendered in case the available value equals false. Otherwise the complete merge block should be removed.
Based on the above JSON data, the following result should be rendered:
The outofstock merge block is rendered for the first 2 rows and not rendered for the last row.
To show how to add the conditions programmatically, the template contains both merge blocks without the condition. The following code shows how to add the condition to the merge block outofstock:
// load the template
textControl1.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
// get the "outofstock" SubTextPart
SubTextPart subTextPart = textControl1.SubTextParts.GetItem("txmb_outofstock");
// create a MergeBlockInfo object
MergeBlockInfo mergeBlockInfo = new MergeBlockInfo(subTextPart);
// add the condition
mergeBlockInfo.BlockMergingCondition =
new List<TXTextControl.DocumentServer.DataShaping.FilterInstruction>()
{
new TXTextControl.DocumentServer.DataShaping.FilterInstruction(
"available",
TXTextControl.DocumentServer.DataShaping.RelationalOperator.Equals,
false
)
};
// store the condition in the SubTextPart
mergeBlockInfo.Apply();
The Merge
Finally, the settings are applied and stored in the SubTextPart.
These conditions are persistently stored when the template is saved and are used automatically by the Mail
string data = System.IO.File.ReadAllText("data.json");
using (MailMerge mm = new MailMerge()) {
mm.TextComponent = textControl1;
mm.MergeJsonData(data);
}
Additionally, the merge field leadtime of the conditional block is populated with values from the outofstock table.
Also See
This post references the following in the documentation:
- TXText
Control. Document Server. Data Shaping. Filter Instruction Class - TXText
Control. Document Server. Data Sources. Merge Block Info. Block Merging Condition Property - TXText
Control. Document Server. Data Sources. Merge Block Info Class - TXText
Control. Document Server. Mail Merge Class - TXText
Control. Sub Text Part Class
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
Merging Form Fields using the MailMerge Class
Usually, the MailMerge class is used to merge data into document templates to replace merge fields. Since version 30.0, it is possible to merge data into form fields.
TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format
Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…
Expert Implementation Services for Legacy System Modernization
We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.