I just looked extensively through that posting. Thank you for it. It's interesting but cannot possibly restore the tree to precisely the state it was in before the Postback. How can I say that definitively? Because there's no Javascript.
Imagine a treeview has assorted nodes open and is scrolled about 2/3 of the way down the tree. Now imagine the selected node in one of 3 positions:
- Toward the top of the [parent] panel
- In the middle of the panel
- Toward the bottom of the panel
In each case the top edge of the selected node is precisely 'Y' pixels below the top of the panel. If the code doesn't restore this distance exactly then the treeview will appear to jump around, which is disconcerting to the user.
From another article I found a solution that goes as far as maflones' does, but with much less code:
1. In the <@Page> tag, add: MaintainScrollPositionOnPostback="true"
2. Add this JavaScript code into the <head> section (where "treeView1" is the name of the treeview and "panel1" is the name of its parent panel) :
<script type="text/javascript">
function LoadEvent()
{
try
{
var elem =
document.getElementById('treeView1_SelectedNode');
if(elem != null )
{
var node = document.getElementById(elem.value);
if(node != null)
{
node.scrollIntoView(true);
panel1.scrollLeft = 0;
}
}
}
catch(oException)
{
}
}
</script>
3. Change the <body> tag to this: <body onload="LoadEvent()">
4. In the <asp:TreeView> definition, ensure that this is included: EnableClientScript="true"
And that's it. It works fine, albeit in the limited fashion that seems the best we can do for now. It's quite sad that Microsoft never thought to add the capability to precisely restore it between Postbacks.
Robert W.
Robert Werner
Vancouver, BC
www.pocketpollster.com