下面的代碼顯示了頁面的布局:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> 。紅itle>Focus Tree Node on Postback</title> </head> <body onload="LoadEvent()"> 。糵orm id="form1" runat="server"> 。糳iv> 。糰sp:Panel ID="Panel1" runat="server" Height="200px" Width="200px" ScrollBars="Both"> 。糰sp:TreeView ID="TreeView1" runat="server"> 。糞electedNodeStyle BackColor="#8080FF" /> 。/asp:TreeView> 。/asp:Panel> 。/div> 。/form> </body> </html> |
下圖顯示了本例子運行的結果
![]() |
最后,下面的代碼顯示了javascript的注入方式:
<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> |
用Javascript定義的LoadEvent函數將查找隱藏字段,我們利用TreeView控件的ClientID 查找所有元素,不過,在嵌套多個TreeView控件后,名稱將變得非常長.找到單元格的值后,我使用scrollIntoView方法.scrollLeft屬性將讓滾動條滾動當前位置.
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/