The HTML5-based document editor, that is part of the product TX Text Control .NET Server for ASP.NET, comes with a JavaScript API to manipulate the most typical elements in a document.

This JavaScript API can be used with the ASP.NET WebForms, MVC and also the language independent Widget version which is currently in beta. This article gives an overview of how to insert, remove and manipulate merge fields programmatically.

Inserting Merge Fields

In order to insert a merge field, a new TXTextControl.MergeField object needs to be created:

Loading...

This field can be added to the document at the current input position using the Javascript: TXTextControl.addMergeField method:

Loading...

If you would like to insert the merge field at a specific location, you can set the input position using the TXTextControl.selection object first:

Loading...

Deleting Merge Fields

Each time, merge fields (or any text field) should be manipulated, a new synchronized list of fields needs to be retrieved from Text Control. This is done using the Javascript: TXTextControl.getTextFields method which returns a collection of either all text fields or only the text field at the current input position.

The following code deletes the merge field at the current input position:

Loading...

Manipulating Merge Fields

The following code returns all available merge fields in the current text part. The method getTextFields returns all text fields. In order to filter the merge fields, the type and the typeName properties need to be checked:

Loading...

Now, these fields can be manipulated. The following code sets the text of the first field in the created array of fields:

Loading...

The following sample uses the Widget to show how a merge field can be added and removed from the document: