ASP.NET 的擴展性是其核心優勢之一。微軟發布的 Internet Explorer Web Controls 提供了強大的 TreeView 控件,能以 Windows 檔案總管風格的樹狀結構展示層級數據。本文涵蓋環境配置、控件定義、節點操作與屬性詳解。
C:\Program Files\IE Web Controls)build.batxcopy /s /i .\build\Runtime C:\Inetpub\wwwroot\webctrl_client\1_0 /yIE WebControl.dll<%@ Import Namespace="Microsoft.Web.UI.WebControls" %>
<%@ Register TagPrefix="iecontrols"
Namespace="Microsoft.Web.UI.WebControls"
Assembly="Microsoft.Web.UI.WebControls" %>
<iecontrols:TreeView
id="tvBasic"
autoSelect="false"
showPlus="true"
showLines="true"
expandLevel="2"
runat="server">
</iecontrols:TreeView>
| 屬性 | 類型 | 說明 |
|---|---|---|
| autoSelect | Boolean | true:允許鍵盤方向鍵選中節點;false:禁用鍵盤導航選中 |
| showPlus | Boolean | true:顯示展開/摺疊的「+」號圖示;false:隱藏 |
| showLines | Boolean | true:顯示節點連接線條;false:隱藏線條 |
| expandLevel | Integer | 預設展開層級數(0=全部摺疊,2=展開前兩層) |
<iecontrols:TreeView id="tvWebsite" ... runat="server">
<iecontrols:TreeNode Text="我常去的站點">
<iecontrols:TreeNode Text="設計類站點">
<iecontrols:TreeNode Text="藍色理想" />
<iecontrols:TreeNode Text="有風的日子" />
<iecontrols:TreeNode Text="設計聯盟" />
</iecontrols:TreeNode>
<iecontrols:TreeNode Text="編程類站點">
<iecontrols:TreeNode Text="ASP編程">
<iecontrols:TreeNode Text="ASP COOL" />
<iecontrols:TreeNode Text="ASP.NET編程">
<iecontrols:TreeNode Text="Dotnet技術網" />
<iecontrols:TreeNode Text="asp.net" />
</iecontrols:TreeNode>
</iecontrols:TreeNode>
<iecontrols:TreeNode Text="程序員大本營" />
</iecontrols:TreeNode>
</iecontrols:TreeNode>
</iecontrols:TreeView>
<iecontrols:TreeNode Text="..."> ... </iecontrols:TreeNode><iecontrols:TreeNode Text="..." />實際專案中,樹狀數據通常來自資料庫。以下示範遞歸綁定:
// 假設資料表結構:Id, ParentId, Text, NavigateUrl
private void BindTreeView()
{
DataTable dt = GetTreeDataFromDB(); // 獲取扁平化數據
BuildTree(tvBasic.Nodes, null, dt); // 從根節點開始遞歸構建
}
private void BuildTree(TreeNodeCollection parentNodes, int? parentId, DataTable dt)
{
DataRow[] rows = dt.Select(parentId.HasValue ? $"ParentId = {parentId}" : "ParentId IS NULL");
foreach (DataRow row in rows)
{
TreeNode node = new TreeNode
{
Text = row["Text"].ToString(),
NavigateUrl = row["NavigateUrl"].ToString()
};
parentNodes.Add(node);
BuildTree(node.Nodes, Convert.ToInt32(row["Id"]), dt); // 遞歸綁定子節點
}
}
webctrl_client/1_0 目錄已正確部署至網站根目錄IE Web Controls 的 TreeView 雖屬早期技術,但其層級數據展示邏輯與節點管理思想仍具參考價值。理解其運作機制有助於掌握現代樹狀組件的核心概念。維護舊專案時,本文配置步驟可作為快速恢復環境的參考手冊。