现在的位置: 首页 > 综合 > 正文

TteeView在runat=”server”时防止滚动条自动滚到初始位置

2013年09月15日 ⁄ 综合 ⁄ 共 2133字 ⁄ 字号 评论关闭

 <div id="TreeDiv" style="width: 226px; height: 460px; overflow: auto;">
                                                <asp:TreeView ID="CtrlTree" runat="server" ShowLines="True" Width="90%" ExpandDepth="0"
                                                    EnableClientScript="False" PopulateNodesFromClient="False" OnTreeNodePopulate="CtrlTree_TreeNodePopulate">
                                                    <SelectedNodeStyle CssClass="TreeSelect" Font-Bold="True" />
                                                    <NodeStyle ImageUrl="~/Images/TreeTop/Cert.gif" />
                                                </asp:TreeView>
                                            </div>

 

    <script language="javascript">
        var sFirstEnter = 1;

        //  获取当前文件名
        function getFileName()
        {
            var url = this.location.href;
            var pos = url.lastIndexOf("/");
            if(pos == -1)
                pos = url.lastIndexOf("//");
            var filename = url.substr(pos+1);
            return filename;
        }

        function fnLoad()
        {
            with(window.document.all.TreeDiv)
            {
                try
                {
                    addBehavior ("#default#userData");    // 使得body元素可以支持userdate
                    load("scrollState" + getFileName());    // 获取以前保存在userdate中的状态
                    //alert(getAttribute("scrollLeft"));
                    //if (sFirstEnter=="0")
                    //{
                        scrollLeft = getAttribute("scrollLeft");    // 滚动条左位置
                        scrollTop = getAttribute("scrollTop");
                    //}
                }
                catch(e)
                {
                    //alert(e);
                };
            }
        }
        function fnUnload()
        {
            with(window.document.all.TreeDiv)
            {
                try
                {
                setAttribute("scrollLeft",scrollLeft);
                setAttribute("scrollTop",scrollTop);
                save("scrollState" + getFileName());    
                }
                catch(e)
                {
                    //alert(e);
                };
            }
        }

        window.onload = fnLoad;
        window.onunload = fnUnload;
    </script>

抱歉!评论已关闭.