Search code examples
.netasp.netlistboxautopostback

Listbox Scrolling to Top on autopostback


Why does asp.net listbox always scroll to top upon selecting an item when autopostback is on? How can I prevent this from happening?


Solution

  • I added the following jquery in javascript to fix the issue. I cannot remember where I found the solution but here it is. Just add the location of your target control - $get('YourDiv_YourPanel').

    <script type="text/javascript">
        //Maintain scroll position in given element or control
        var xInputPanel, yInputPanel;
        var xProductPanel, yProductPanel;
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_beginRequest(BeginRequestHandler);
        prm.add_endRequest(EndRequestHandler);
        function BeginRequestHandler(sender, args) {
            yInputPanel = $get('MainContent_InputPanel').scrollTop;
            yProductPanel = $get('MainContent_ProductPanel').scrollTop;
        }
        function EndRequestHandler(sender, args) {
            $get('MainContent_InputPanel').scrollTop = yInputPanel;
            $get('MainContent_ProductPanel').scrollTop = yProductPanel;
        }
    </script>