Search code examples
c#asp.nethtmlajaxmodalpopupextender

Cannot keep track of div position of treeview


I have implemented a method of keeping track of Div positions - This works fine on my webpage. Now I want to keep track of a div position on a modalpopupextender that contains a TreeView but its position is restored as 0. I think it may be due to the fact that the popup has not rendered yet as the second message box (alert(elem.scrollTop);) below opens before the popup is displayed.

The first value being saved is alerted as 860 but is always restored as 0. After the second alert the popup is displayed when I click OK which I believe is the problem as the page is not fully finished.

can you tell me please what i need to do to fix?

Javascript

<script type="text/javascript"> <!-- Script to manage scroll position -->    
$(document).ready(function () {
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);


    function BeginRequestHandler(sender, args) {

        try {
            var elem = document.getElementById('AddFiles_Div');
            scrollTop = elem.scrollTop; 
            // I am working correctly   
            alert(elem.scrollTop);    
       }
       catch (err) {}
    }


    function EndRequestHandler(sender, args) {
        try {    
            var elem = document.getElementById('AddFiles_Div');
            elem.scrollTop = scrollTop;
            // I am always zero   .................. 
            alert(elem.scrollTop);     
        }

        catch (err) {}  
    }   

    });    

</script>

C# Code that runs when a node is expanded .ie on Postback

// Does some work and then runs the next lin.....

AddFilesModal.Show();

Solution

  • what about this for a solution? - it works ok. i.e replace

    $(document).ready(function () { }); 
    

    with

    function pageLoad() {}
    

    Is this ok?

    Complete working Javascript

    <script type="text/javascript"> <!-- Script to manage scroll position -->
    
    function pageLoad() {
    
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    
    
        function BeginRequestHandler(sender, args) {
    
            try {
                var elem = document.getElementById('AddFiles_Div');
                scrollTop = elem.scrollTop;
           }
    
            catch (err) {
            }
    
        }
    
    
        function EndRequestHandler(sender, args) {
            try {
                var elem = document.getElementById('AddFiles_Div');
                elem.scrollTop = scrollTop;
    
            }
    
            catch (err) {
    
            }
        }   
    
       //});
     }
    
    </script>