开发者

Maintain MULTISELECT ListBox scroll position in update panel during postback, kinda works

开发者 https://www.devze.com 2023-03-18 15:29 出处:网络
I\'m using the following code to maintain a ListBox\'s scroll position across post backs. <script type=\"text/javascript\">

I'm using the following code to maintain a ListBox's scroll position across post backs.

<script type="text/javascript">
    // Helps maintain scroll position on the Specialty list box.
    var xPos, yPos;
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    function BeginRequestHandler(sender, args)
    {
        var listBox = $get('<%= Special.ClientID %>');

        if (listBox != null)
        {
            xPos = listBox.scrollLeft;
            yPos = listBox.scrollTop;
        }
    }

    function EndRequestHandler(sender, args)
    {
        var listBox = $get('<%= Special.ClientID %>');

        if (listBox !开发者_如何学Go= null)
        {
            listBox.scrollLeft = xPos;
            listBox.scrollTop = yPos;
        }
    }

    prm.add_beginRequest(BeginRequestHandler);
    prm.add_endRequest(EndRequestHandler);
</script>

This works perfectly in Firefox 5 or Chrome. Although, in IE8/IE9 whenever I select an item in the List Box it fires a post back and the List Box maintains the scroll position. The catch is that after the post back if I scroll at all in the box, or click the arrows, the scroll position snaps back up to the top of the list box!

The snapping back on scroll start does not occur in Firefox or Chrome.

Thanks for any help.


Probably long overdue but none-the-less viewed 2071 times:

  • s is the id="mySelect" name of the multiple select

  • fs is the size of the font for the select

    function SetScrollTop(s, fs)
    {
        if(document.getElementById(s) != null)
        {
            document.getElementById(s).scrollTop = (document.getElementById(s).selectedIndex * fs) + 1;
        }
    }
    

Call this function with the body onload="SetScrollTop('mySelect',14)".

IE sets the scroll top to zero on the post back so you will need to somehow keep track of the selected item(s) (a cookie or a self made session handler?) so you can use the 'selected' attribute on the item(s) selected. Then set the scroll top during the on load.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号