# TextControl.Web: Restoring the Scroll Location

> When reloading documents after server-side processing in the TX Text Control HTML5 editor, the scroll position resets. The textViewLocationChanged event captures coordinates before the operation, and ScrollDelta restores the exact position after the modified document loads into the view.

- **Author:** Bjoern Meyer
- **Published:** 2019-07-22
- **Modified:** 2026-03-05
- **Description:** When reloading documents after server-side processing in the TX Text Control HTML5 editor, the scroll position resets. The textViewLocationChanged event captures coordinates before the operation, and ScrollDelta restores the exact position after the modified document loads into the view.
- **2 min read** (343 words)
- **Tags:**
  - ASP.NET
  - MVC
  - Scrolling
- **Web URL:** https://www.textcontrol.com/blog/2019/07/22/restoring-the-scroll-location/
- **LLMs URL:** https://www.textcontrol.com/blog/2019/07/22/restoring-the-scroll-location/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2019/07/22/restoring-the-scroll-location/llms-full.txt

---

The JavaScript API covers the most typical tasks such as inserting merge fields and setting edit modes of TX Text Control. Sometimes, more complex tasks require the usage of a server-side TXTextControl.ServerTextControl classto manipulate the document.

In order to insert a more complex document element into the text, the document can be saved locally and send to a server-side endpoint like a Web API method. After the document has been changed server-side, it can be returned and loaded using JavaScript. If the complete document is loaded and not only the selection is replaced, it is required to restore the old scroll location. By default, after a loading process, the TX Text Control scrolls to top left automatically.

```
var scrollLocation;
var freezedScrollLocation;
var dpiX = 15; // standard resolution server-side

function insertComplexElement() {

    // store the current scroll location
    freezedScrollLocation = scrollLocation;
    
    var serviceURL = "/api/TXDocument/InsertComplexElement";

    TXTextControl.saveDocument(TXTextControl.streamType.InternalUnicodeFormat, function (e) {
    
        $.ajax({
            type: "POST",
            url: serviceURL,
            contentType: 'application/json',
            data: JSON.stringify({
                Document: e.data
            }),
            success: successFunc,
            error: errorFunc
        });

        function successFunc(data, status) {

            //disable loading dialog
            TXTextControl.isLoadingDialogEnabled = false;
            TXTextControl.enableCommands(); // enable commands
            
            // code that loads a document that is returned by an endpoint
            TXTextControl.loadDocument(TXTextControl.streamType.InternalUnicodeFormat, data);

            // restore the scroll position
            TXTextControl.sendCommand(TXTextControl.Command.ScrollDelta,
                freezedScrollLocation.location.x * dpiX,
                freezedScrollLocation.location.y * dpiX)
        }

        function errorFunc() {
            console.log("error");
        }
    }
}

TXTextControl.addEventListener("textViewLocationChanged", function (e) {
    scrollLocation = e;
});
```

The event *textViewLocationChanged* returns the current scroll location which is stored in the variable *scrollLocation*. The dummy function *insertComplexElement* stores the current scroll location in the variable *freezedScrollLocation* before it is sending the document to the server. After the document has been retrieved from the server, it is loaded and the scroll location is restored using the command *ScrollDelta*. In order to use this code, it is required to turn on the hidden command interface which is done using the *enableCommands* method.

How to load and save documents from and to the MVC controller can be found in this article:

[MVC: Loading and Saving Documents Through Controller HttpPost Methods](https://www.textcontrol.com/blog/2015/12/08/mvc-loading-and-saving-documents-through-controller-httppost-methods/llms-full.txt)

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Getting Started: ServerTextControl and MailMerge with ASP.NET MVC (.NET Framework)](https://www.textcontrol.com/blog/2022/09/01/getting-started-servertextcontrol-and-mailmerge-with-aspnet-net-framework/llms.txt)
- [Getting Started: Document Viewer with ASP.NET MVC (.NET Framework)](https://www.textcontrol.com/blog/2022/09/01/getting-started-document-viewer-with-aspnet-framework/llms.txt)
- [Getting Started: Document Editor with ASP.NET MVC (.NET Framework)](https://www.textcontrol.com/blog/2022/09/01/getting-started-document-editor-with-aspnet-framework/llms.txt)
- [Detect Toggle Button Changes Using a MutationObserver](https://www.textcontrol.com/blog/2021/11/11/detect-toggle-button-changes-using-a-mutationobserver/llms.txt)
- [Using the MVC DocumentViewer in ASP.NET Web Forms](https://www.textcontrol.com/blog/2021/08/12/using-the-mvc-documentviewer-in-aspnet-web-forms/llms.txt)
- [Creation of Custom Electronic Signature Boxes](https://www.textcontrol.com/blog/2021/06/15/creation-of-custom-electronic-signature-boxes/llms.txt)
- [DocumentViewer Preview: Completing Form Fields](https://www.textcontrol.com/blog/2021/04/15/document-viewer-preview-completing-form-fields/llms.txt)
- [Implementing Conditional Table Cell Colors with MailMerge](https://www.textcontrol.com/blog/2020/10/08/implementing-conditional-table-cell-colors-with-mailmerge/llms.txt)
- [DocumentViewer for .NET Core 3.1 Released](https://www.textcontrol.com/blog/2020/04/08/documentviewer-for-dotnet-core-released/llms.txt)
- [Document Collaboration: Implementing Comments with Document Targets](https://www.textcontrol.com/blog/2020/04/06/implementing-comments-with-document-targets/llms.txt)
- [MVC NuGet Packages for X18 Published](https://www.textcontrol.com/blog/2020/03/17/mvc-nuget-packages-for-x18-published/llms.txt)
- [Configuring ASP.NET and IIS for Larger Requests](https://www.textcontrol.com/blog/2019/12/20/documentviewer-configuring-aspnet-for-larger-requests/llms.txt)
- [Updated MVC DocumentViewer: Session-less and Improved Image Quality](https://www.textcontrol.com/blog/2019/12/12/updated-mvc-documentviewer-session-less-and-improved-image-quality/llms.txt)
- [Using Multiple Electronic Signatures on a Document](https://www.textcontrol.com/blog/2019/10/23/using-multiple-electronic-signatures/llms.txt)
- [MVC DocumentViewer Update: Printing, Resources and Mobile-Friendly Document Signing](https://www.textcontrol.com/blog/2019/10/18/mvc-documentviewer-update-printing-resources-mobile/llms.txt)
- [ReportingCloud .NET Core Quickstart Tutorial](https://www.textcontrol.com/blog/2019/07/24/reportingcloud-dotnet-core-quickstart-tutorial/llms.txt)
- [MVC: Loading and Saving Documents Directly Server-Side](https://www.textcontrol.com/blog/2019/07/23/mvc-loading-and-saving-documents-directly-server-side/llms.txt)
- [Loading and Saving User Dictionaries in TextControl.Web](https://www.textcontrol.com/blog/2019/07/19/loading-and-saving-user-dictionaries-in-textcontrol-web/llms.txt)
- [Using TX Text Control MVC in Partial Views](https://www.textcontrol.com/blog/2019/04/17/using-tx-text-control-mvc-in-partial-views/llms.txt)
- [MVC DocumentViewer: Loading Documents](https://www.textcontrol.com/blog/2018/07/03/mvc-documentviewer-loading-documents/llms.txt)
- [Deploying the MVC HTML5 Editor to Azure App Services](https://www.textcontrol.com/blog/2018/04/06/deploying-the-mvc-html5-editor-to-azure-app-services/llms.txt)
- [Using the ASP.NET MVC DocumentViewer JavaScript API](https://www.textcontrol.com/blog/2017/04/28/using-the-aspnet-mvc-documentviewer-javascript-api/llms.txt)
- [HTML5 Based MVC DocumentViewer Public Beta Program Launched](https://www.textcontrol.com/blog/2017/04/26/html5-based-mvc-documentviewer-public-beta-program-launched/llms.txt)
- [TX Text Control .NET Server: MVC Article Collection](https://www.textcontrol.com/blog/2016/01/28/tx-text-control-net-server-for-aspnet-mvc-article-collection/llms.txt)
- [MVC: Loading Files from the Backstage Menu](https://www.textcontrol.com/blog/2016/01/06/mvc-loading-files-from-the-backstage-menu/llms.txt)
