Skip to main content
All docs
V24.2

Document.BeginTransaction() Method

Along with the EndTransaction() method, allows you to accumulate document changes on the server and send them to the client in a single transaction.

Namespace: DevExpress.Blazor.RichEdit

Assembly: DevExpress.Blazor.RichEdit.v24.2.dll

NuGet Package: DevExpress.Blazor.RichEdit

Declaration

public void BeginTransaction()

Remarks

The Rich Text Editor sends data from the server to the client every time you make changes to a document. Enclose your code in the BeginTransaction() - EndTransaction() method calls to lock the RichEdit’s data transmission to the client when you make multiple changes to a document. Such transactions allow you to send a batch of changes to the client and improve the component’s performance.

Each BeginTransaction() method call should be paired with the EndTransaction() method call. The BeginTransaction() method marks the starting point of a server-client transaction and locks the component to prevent constant data transmission. The EndTransaction() method unlocks the component and sends all accumulated changes to the client.

Run Demo: Document API

Invalid Access Exception

During transactions (batch document updates on the server), the Rich Text Editor operates with mock objects that do not exist on the client yet. As the result, some API members of these objects become inaccessible. If code enclosed in a transaction contains inaccessible APIs, the component throws an InvalidAccessException and displays the following error message:

The {propertyName}/{methodName} property/method is not accessible in the transaction.

The message text contains the name of the first inaccessible property/method found within the transaction.

async Task InitializeDocument() {
    Document documentAPI = richEdit.DocumentAPI;

    documentAPI.BeginTransaction();
    await CreateTitleParagraph(documentAPI);
    // ...
    await documentAPI.EndTransaction();
}

async Task CreateTitleParagraph(Document documentAPI) {
    Paragraph titleParagraph = await documentAPI.Paragraphs.CreateAsync(0);
    var alignment = titleParagraph.Alignment;
    // Alignment is an inaccessible property. When you attempt to access it, the following error message appears: 
    // "The Alignment property is not accessible in the transaction."
    ...
}

Access Properties

You cannot access an object’s properties during transactions, except in the following cases:

  • You use them in the corresponding ChangePropertiesAsync() method.
  • You use an object’s Interval property. Note that Interval class members are inaccessible.
  • You use a table’s Rows property (for tables declared in the same transaction). Note that the Rows property becomes inaccessible if you use it after a MergeCellsAsync or SplitAsync method call in the same server-client transaction.

Access Methods

During transactions, you cannot call methods that return collections of objects or do not return API objects:

Example

The following code snippet accumulates paragraph changes on the server and sends them to the client in a single transaction:

<DxRichEdit @ref="richEdit" CssClass="w-100 ch-720"/>
@code {
    DxRichEdit richEdit;
    protected override async Task OnAfterRenderAsync(bool firstRender) {
        if(firstRender) {
            try {
                await InitializeDocument();
            } catch(TaskCanceledException) { }
        }

        await base.OnAfterRenderAsync(firstRender);
    }
    async Task InitializeDocument() {
        Document documentAPI = richEdit.DocumentAPI;
        documentAPI.BeginTransaction();
        await CreateBodyParagraph(documentAPI);
        await CreateTitleParagraph(documentAPI);
        await documentAPI.EndTransaction();
    }
    async Task CreateTitleParagraph(Document documentAPI) {
        Paragraph titleParagraph = await documentAPI.Paragraphs.CreateAsync(0);
        await titleParagraph.ChangePropertiesAsync(properties => { properties.SpacingAfter = 400; });
        TextSpan titleParagraphTextSpan = await documentAPI.AddTextAsync(0, "Albert Einstein");
        await titleParagraphTextSpan.ChangePropertiesAsync(properties => {
            properties.FontName = "Arial";
            properties.FontSize = 24;
            properties.FontBold = true;
        });
    }

    async Task CreateBodyParagraph(Document documentAPI) {
        Paragraph bodyParagraph = await documentAPI.Paragraphs.CreateAsync(0);
        await bodyParagraph.ChangePropertiesAsync(properties => {
            properties.FirstLineIndentType = FirstLineIndentType.Indented;
            properties.FirstLineIndent = 500;
        });
        string bodyParagraphText = "Albert Einstein (14 March 1879 - 18 April 1955) was a German-born theoretical physicist, widely acknowledged to be one of the greatest physicists of all time. Einstein is known for developing the theory of relativity, but he also made important contributions to the development of the theory of quantum mechanics.";
        TextSpan bodyParagraphTextSpan = await documentAPI.AddTextAsync(0, bodyParagraphText);
        await bodyParagraphTextSpan.ChangePropertiesAsync(properties => {
            properties.FontName = "Segoe UI";
            properties.FontSize = 12;
        });
        TextSpan boldTextSpan = await documentAPI.GetTextSpanAsync(0, 15);
        await boldTextSpan.ChangePropertiesAsync(properties => { properties.FontBold = true; });
        TextSpan italicTextSpan = await documentAPI.GetTextSpanAsync(16, 31);
        await italicTextSpan.ChangePropertiesAsync(properties => { properties.FontItalic = true; });
        await documentAPI.Hyperlinks.CreateAsync(bodyParagraphText.IndexOf("quantum mechanics"), "quantum mechanics".Length, url: "https://en.wikipedia.org/wiki/Quantum_mechanics");
        await documentAPI.Hyperlinks.CreateAsync(bodyParagraphText.IndexOf("theory of relativity"), "theory of relativity".Length, url: "https://en.wikipedia.org/wiki/Theory_of_relativity");
        await documentAPI.Hyperlinks.CreateAsync(bodyParagraphText.IndexOf("theoretical physicist"), "theoretical physicist".Length, url: "https://en.wikipedia.org/wiki/Theoretical_physics");
    }

}
See Also