@zhenyu.wang
You can populate a Drop‑Down List Content Control in a Word template that is later processed by the LINQ Reporting Engine, but the engine itself cannot create the list items – they must exist in the document before the report is executed.
Typical workflow
- Create the drop‑down list in the template (or add it programmatically before the report runs).
- Give the control a tag (e.g.
DropDown1). The tag is the field name that the LINQ Reporting Engine will bind to.
- Run the LINQ Reporting Engine – it will set the selected value of the control based on the data source.
1. Add the list items with DocumentBuilder
using Aspose.Words;
using Aspose.Words.BuildingBlocks;
using Aspose.Words.Markup;
// load a blank template or an existing .docx
Document doc = new Document("Template.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a drop‑down list content control
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.DropDownList, MarkupLevel.Block);
sdt.Tag = "DropDown1"; // <-- this tag is used by LINQ Reporting Engine
sdt.Title = "Select an option";
// Add the list items
sdt.ListItems.Add(new ListItem("Item A", "A"));
sdt.ListItems.Add(new ListItem("Item B", "B"));
sdt.ListItems.Add(new ListItem("Item C", "C"));
// Place the control where you need it
builder.InsertNode(sdt);
builder.Writeln(); // optional line break after the control
doc.Save("TemplateWithDropDown.docx");
Result: TemplateWithDropDown.docx now contains a drop‑down list with three items and a tag DropDown1.
2. LINQ Reporting Engine – set the selected value
using Aspose.Words.Reporting;
using System.Collections.Generic;
// data source – the property name must match the tag of the control
var data = new
{
DropDown1 = "B" // value that matches one of the ListItem values ("A","B","C")
};
ReportingEngine engine = new ReportingEngine();
engine.BuildReport("TemplateWithDropDown.docx", "Result.docx", data);
When the report is built, the drop‑down list will show Item B as the selected entry.
What you cannot do with the LINQ Reporting Engine
- Dynamically add or remove list items at run‑time.
- Populate the list from a collection directly – the list must be defined in the template (or added with
DocumentBuilder before BuildReport is called).
If you need the list items to be generated from a data source, create the control programmatically before invoking the reporting engine, e.g.:
var items = GetItemsFromDatabase(); // returns IEnumerable<KeyValuePair<string,string>>
foreach (var kvp in items)
sdt.ListItems.Add(new ListItem(kvp.Key, kvp.Value));
Then run the LINQ Reporting Engine as shown above to set the selected value.
Next steps / checks
- Verify you are using a recent version of Aspose.Words for .NET (the API shown works from v20.9 onward).
- Ensure the tag name in the template (
DropDown1 in the example) exactly matches the property name in the data object passed to BuildReport.
If you still encounter issues, please provide:
- Aspose.Words version you are using.
- A minimal template (or the part of the template that contains the control).
- The exact code you use to invoke the LINQ Reporting Engine.
With that information we can pinpoint any version‑specific quirks.