Search code examples
c#asp.netpostbackmaintainscrollpositiononasp.net-4.6

maintainScrollPositionOnPostBack clashing with focus method of textobox


I have an ASP.NET Page where in web.config I have added this setting :

<pages maintainScrollPositionOnPostBack="true">

Now in a aspx page, I have a textbox (txtTop) on top of the form & after user scrolls down (there are lot of controls in between) there is a button.

Inside this button Click event on Server, I have added this code.

  txtTop.focus();

I expect that the focus should be on the textbox as well as the scroll position to be pointed towards the textbox. but it is not happening. The focus is there on the textbox but it is not getting shown i have to press some key to put the textbox into the scroll position.
These two properties are clashing & I am unable to resolve it.

FYI: There are no update panels..


Solution

  • I created a dirty hack to resolve this.Without setTimeout it was not working neither was document.ready.

    $(window).load(function () {
    
        setTimeout(function () {
            document.activeElement.scrollIntoView(true);
        }, 1);
    });