Example - Programming API

Web.TextControl provides the same interface to the TextParts such as the ServerTextControl. You can iterate through all TextParts including the MainText, TextFrames and HeaderFooter objects. These TextPart objects provide a Selection object that can be used to get and set formatted text and to interact with the content.

This example is part of the Sample Explorer project which includes all samples in one. The sources for this sample project can be found here:

Language Project Location
C# %USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\CSharp\Sample Explorer\
VB.NET %USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\VB.NET\Sample Explorer\

You will find the source code and the ASPX page of this sample in the Solution structure tree under: HTML5\selection.aspx

Example description

A temporary ServerTextControl is used to modify the content of Web.TextControl. The Selection is loaded into a ServerTextControl, modified and loaded back to Web.TextControl.

The following screenshot shows a custom TreeView control to insert merge fields and reusable text blocks programmatically.

The following code snipped shows how to insert a new merge field. A temporary ServerTextControl is created and a new merge field is inserted using the ApplicationFieldCollection. The content is temporary saved to a byte array and loaded into the current selection of the Web.TextControl. Using this approach, any element, text block or other word processing elements can be added to the Web.TextControl.

[C#]
protected void Button1_Click(object sender, EventArgs e)
{
	// insert a new text field into Web.TextControl
	using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
	{
		tx.Create();

		MergeField newField = new MergeField();
		newField.Name = TextBox2.Text;
		newField.Text = "Text Field: " + TextBox2.Text; ;

		tx.ApplicationFields.Add(newField.ApplicationField);

		byte[] data;
		tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
		TextControl1.Selection.Load(data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat);

		TextBox2.Text = "";
	}
}
[Visual Basic]
Protected Sub Button1_Click(sender As Object, e As EventArgs)
' insert a new text field into Web.TextControl
	Using tx As New TXTextControl.ServerTextControl()
		tx.Create()

		Dim newField As New MergeField()
		newField.Name = TextBox2.Text
		newField.Text = "Text Field: " + TextBox2.Text



		tx.ApplicationFields.Add(newField.ApplicationField)

		Dim data As Byte()
		tx.Save(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat)
		TextControl1.Selection.Load(data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat)

		TextBox2.Text = ""
	End Using
End Sub

Additionally, several events are available to interact with merge fields. This allows the insertion and manipulation of merge fields using your own interface.

<script type="text/javascript">

	// client-side Javascript events for merge fields

	TX.addEventListener('textFieldEntered', function (e) {
		fieldEntered(e.fieldName, e.fieldType);
	});

	TX.addEventListener('textFieldLeft', function (e) {
		fieldLeft();
	});

	function fieldEntered(fieldName, fieldType) {
		document.getElementById('TextBox1').value = fieldName;
	}

	function fieldLeft() {
		document.getElementById('TextBox1').value = '';
	}

</script>