[updatepanel , treeView , position , state , postBack , hiddenfiled]
put a treeView in a panel, it will show a scrollBar; when click then treeView, it will postBack,how to restore the position?
------------------------------------------------------------------------------------------------------------------------
1:in common asp page: aspx
<script language="javaScript">
function loadEvent()
{
//tree在postBack时,显示在页面中的位置会移动,注册客户端脚本处理。加在body onload="loadEvent()"
try
{ //hf_SelectedNode tv_Main_SelectedNode 自动生成的hiddenField
var s=""; //tv_Main_Data.selectedNodeID.value; //自动生成的java Object
if (s=="" || s==null) s="tv_Main_SelectedNode";
var elem = document.getElementById(s); //tv_Main_SelectedNode
if(elem != null )
{
var node = document.getElementById(elem.value);
if(node != null)
{
node.scrollIntoView(false); // true:页面顶部
//document.getElementById('div_Tree').scrollLeft = 0;
}
}
}
catch(oException)
{}
}
</script>
.....
<body onload="loadEvent()" >
...
<asp:Panel ID="panel_Tree" runat="server" ScrollBars="Both" Style="width:100%; height:100%"
<asp:TreeView ID="tv_Main" runat="server"
OnSelectedNodeChanged="tv_Main_SelectedNodeChanged" Width="300px">
<Nodes>
<asp:TreeNode Text="新建节点" Value="新建节点11"></asp:TreeNode>
<asp:TreeNode Text="新建节点" Value="新建节点12"></asp:TreeNode>
</nodes>
</asp:treeView>
</asp:panel>
------------------------------------------------------------------------------------------------------------------------
2:in ajax-updatePanel is hard, my way is record in client-cookie(thanks eric), aspx:
</script language="javaScript">
function loadEventCookie()
{
var strCook = document.cookie;
document.title=strCook;
if(strCook.indexOf("!~")!=0){
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.getElementById("panel_Tree").scrollTop = strPos;
}
}
function setDivPosition()
{
var intY = document.getElementById("panel_Tree").scrollTop;
document.title = intY;
document.cookie = "yPos=!~" + intY + "~!";
}
</script>
.........
<asp:Panel ID="panel_Tree" runat="server" ScrollBars="Both" Style="width:100%; height:100%"
onscroll="setDivPosition()" onmouseup="loadEventCookie()" onmouseover="loadEventCookie()" >
<asp:TreeView ID="tv_Main"
runat="server" OnSelectedNodeChanged="tv_Main_SelectedNodeChanged"
Width="300px">
<Nodes>
<asp:TreeNode Text="新建节点" Value="新建节点11"></asp:TreeNode>
<asp:TreeNode Text="新建节点" Value="新建节点12"></asp:TreeNode>
<asp:TreeNode Text="新建节点" Value="213"></asp:TreeNode>
</Nodes>
<SelectedNodeStyle BackColor="LimeGreen"/>
</asp:TreeView>
</asp:Panel>
it works good! haha!