<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" SmartNavigation="true" %>
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" MaintainScrollPositionOnPostback="true" %>
<input type="hidden" name="TreeView1_SelectedNode" id="TreeView1_SelectedNode" value="TreeView1t54" />
Imports System.Collections.Generic
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
InjectLoadEvent()
If (IsPostBack) Then Return
TreeView1.Nodes.Clear()
Dim chicken As New TreeNode("Chicken")
TreeView1.Nodes.Add(chicken)
Dim beef As New TreeNode("Beef")
TreeView1.Nodes.Add(beef)
Dim pork As New TreeNode("Pork")
TreeView1.Nodes.Add(pork)
Dim fish As New TreeNode("Fish")
TreeView1.Nodes.Add(fish)
chicken.ChildNodes.Add(New TreeNode("Crepes Florentine with Buffalo Chicken"))
fish.ChildNodes.Add(New TreeNode("Linguine with White Clam Sauce"))
pork.ChildNodes.Add(New TreeNode("Pork Loin with Peanut and Madarin Orange Sauce"))
beef.ChildNodes.Add(New TreeNode("Standing Rib Roast with Fennel and Blue Cheese Potatoes"))
' We need a bunch of stuff here so we will add some stubs
Dim I As Integer
For I = 1 To 50
chicken.ChildNodes.Add(New TreeNode("Placeholder " + I.ToString()))
fish.ChildNodes.Add(New TreeNode("Placeholder " + I.ToString()))
pork.ChildNodes.Add(New TreeNode("Placeholder " + I.ToString()))
beef.ChildNodes.Add(New TreeNode("Placeholder " + I.ToString()))
Next
TreeView1.CollapseAll()
End Sub
Public Sub InjectLoadEvent()
Dim script As String = _
"function LoadEvent()" + _
"{{" + _
" try" + _
" {{" + _
" var elem = document.getElementById('{0}_SelectedNode');" + _
" if(elem != null )" + _
" {{" + _
" var node = document.getElementById(elem.value);" + _
" if(node != null)" + _
" {{" + _
" node.scrollIntoView(true);" + _
" {1}.scrollLeft = 0;" + _
" }}" + _
" }}" + _
" }}" + _
" catch(oException)" + _
" {{}}" + _
"}}"
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "LoadEvent", _
String.Format(script, TreeView1.ClientID, Panel1.ClientID), True)
End Sub
End Class
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>Focus Tree Node on Postback</title>
</head>
<body onload="LoadEvent()">
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" Height="200px" Width="200px" ScrollBars="Both">
<asp:TreeView ID="TreeView1" runat="server">
<SelectedNodeStyle BackColor="#8080FF" />
</asp:TreeView>
</asp:Panel>
</div>
</form>
</body>
</html>
<script>
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>