Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Xamarin | UI for WinUI | UI for ASP.NET Core

New to Telerik Document Processing? Download free 30-day trial

Working with Content Controls

This article shows some examples of how you can insert content controls, access existing and modify their properties from the code.

Manipulate Existing Content Controls

Get the Content Controls

The content controls can be retrieved by using the EnumerateChildrenOfType() method of any document element.

Example 1: Get all content controls

IEnumerable<SdtRangeStart> content_controls = document.EnumerateChildrenOfType<SdtRangeStart>(); 
foreach (SdtRangeStart item in content_controls) 
{ 
    Console.WriteLine("Type: {0} ID:{1}", item.SdtProperties.Type, item.SdtProperties.ID); 
} 

Set Content Controls Properties

This example shows how one can iterate the items and add an item to an existing ComboBox content control.

Example 2: Adding items to a ComboBox or a DropDownList

foreach (SdtRangeStart item in content_controls) 
{ 
    if (item.SdtProperties.Type == SdtType.ComboBox) 
    { 
        ComboBoxProperties properties = item.SdtProperties as ComboBoxProperties; 
 
        ListItem newItem = new ListItem(); 
        newItem.DisplayText = "New Item Text"; 
 
        properties.Items.Add(newItem); 
    } 
} 

Insert or Remove Content Controls

New content controls can be inserted through the InsertStructuredDocumentTag method of RadFlowDocumentEditor.The method has several overloads that allow you to insert the content control by passing its type, properties, and relative document elements.

If adding content controls with the InsertStructuredDocumentTag(SdtType) method without specifying the content, the resulting document will only have the annotation range start and end of the control. The desired content should be manually added afterwards.

Example 3: Inserting a content control using content control type

RadFlowDocument document = new RadFlowDocument(); 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
var dateContentControl = editor.InsertStructuredDocumentTag(SdtType.Date); 
 
editor.MoveToInlineStart(dateContentControl.End); 
editor.InsertText(DateTime.Now.ToString()); // Insert content 

Example 4: Inserting a content control using content control properties

SdtProperties sdtProperties = new SdtProperties(SdtType.RichText) 
{ 
    Alias = "AliasName", 
    Lock = Lock.SdtContentLocked, 
}; 
 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
editor.InsertStructuredDocumentTag(sdtProperties); 

Example 5: Remove a content control

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
editor.RemoveStructuredDocumentTag(contentControl); //this will delete the entire content control along with the value 
// or  
editor.RemoveStructuredDocumentTag(contentControl, false); //this will preserve the value  

Example 6: Insert a content control to a specific position

When using the InsertStructuredDocumentTag() method and passing start and end elements, make sure that the elements are not already part of a content control. An exception to the rule are the rich text and repeating section content controls, which can fully contain other controls, with the restriction that they cannot intersect their ranges.

Figure 1: Examples of correct/incorrect adding of content controls

working-with-content-controls001

RadFlowDocument document = new RadFlowDocument(); 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
 
var paragrpah1 = editor.InsertParagraph(); 
editor.InsertText("Content Control"); 
var paragraph2 = editor.InsertParagraph(); 
 
SdtProperties sdtProperties = new SdtProperties(SdtType.RichText) 
{ 
    Alias = "AliasName", 
    Lock = Lock.SdtContentLocked, 
}; 
 
editor.InsertStructuredDocumentTag(sdtProperties, paragrpah1, paragraph2); 

See Also

In this article
Not finding the help you need? Improve this article