在回发期间维护更新面板中的MULTISELECT ListBox滚动位置,有点工作

时间:2011-07-07 20:21:13

标签: javascript asp.net listbox compatibility

我正在使用以下代码来维护ListBox在帖子后面的滚动位置。

<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 != null)
        {
            listBox.scrollLeft = xPos;
            listBox.scrollTop = yPos;
        }
    }

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

这在Firefox 5或Chrome中完美运行。虽然,在IE8 / IE9中,每当我在列表框中选择一个项目时,它都会发回一个帖子并且列表框保持滚动位置。问题是,如果我在框中完全滚动,或者单击箭头,回滚后,滚动位置会快速回到列表框的顶部!

在Firefox或Chrome中不会发生回滚滚动。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

可能已经过了很长时间,但却没有被观看过2071次:

  • s是id =&#34; mySelect&#34;多重选择的名称

  • fs是select

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

使用body onload =&#34; SetScrollTop(&#39; mySelect&#39;,14)&#34;来调用此函数。

IE在帖子背面将滚动顶部设置为零,因此您需要以某种方式跟踪所选项目(cookie或自制会话处理程序?),以便您可以使用&#39;选择& #39;所选项目的属性。然后在负载期间设置滚动顶部。

相关问题