Preserving focus on AJAX postback

Thread is closed for posting
1 posts, 1 answers
  1. Answer
    Telerik Admin
    Telerik Admin avatar
    1572 posts
    Member since:
    Oct 2004

    Posted 23 Jul 2010 Link to this post


    RadControls version

    Q2 2010 and later
    .NET version 3.5 or later
    Visual Studio version

    2008 or later
    Programming language

    Browser support

    all browsers supported by RadControls

    Often rich client web applications are required to simulate user experience close to WinForms applications. One such requirement is that focus is preserved on the last focused input when an AJAX postback occurs. Although essential for providing seamless user experience, preserving the focus most of the time involves tedious, hacky and unstable coding approaches that are custom tailored to fit the individual scenario.

    This code library demonstrates a small client AJAX component that facilitates the common task of preserving focus after AJAX postbacks and tries to be generic enough to be applicable to a large set of business scenarios. The FocusPersister is a single instance AJAX component that does not require any setup. You simply reference the javascript source file through the ScriptManager and the FocusPersister is automatically instantiated for you.

    The FocusPersister handles the usual scenario of saving the last focused input's ID before an AJAX request and restoring focus to the newly refreshed input with the same ID after the AJAX content is updated. It can be configured to exclude buttons and links from tracking (they get the focus when clicked, which may not always be desired). Additionally, you can specify the type of selection that should be applied to the input that is re-focused after AJAX: caret should be at the beginning of the input text, at the end, all the text should be selected on focus, or no specific logic should be applied (defaulting to the browser's default behavior).

    A sample test project demonstrating the FocusPersister is attached. It can work with either regular ASP.NET TextBox controls, or any of the Telerik RadInput controls (RadTextBox, RadNumericTextBox, RadMaskedTextBox, RadDateInput) plus RadComboBox, and supports AJAX scenarios with UpdatePanels, RadAjaxPanels or RadAjaxManager.
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.