扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
<%@ 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" /> |
有了这些知识,你就知道该怎么做了,基本方法是隐藏的Input是一个textbox,我们要做的就是知道将来呈献的内容。一个TreeView最终呈现为HTML表格,节点的值被用来作为单元格的值,<TD>元素呈现节点名称,所以通过查找单元格ID并滚动到那里.
为了具体说明做法,我使用TreeView编写了一些代码,在Page_Load时间里加载一段脚本来找到需要的单元格(参考下表),在<body>的onload时间里调用该函数.
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <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> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者