【Vegas原创】更加快速的方法:HashTable遍历TreeView
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class treeview : System.Web.UI.Page
{
DBAccess db = new DBAccess();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
hashTable();
}
}
public DataTable GetTreeTable()
{
DataTable dt = db.GetDataTable("select * from trvAdmin order by id");
return dt;
}
void hashTable()
{
DataTable dtregion = GetTreeTable();
Hashtable httree = new Hashtable();
foreach (DataRow myrow in dtregion.Rows)
{
TreeNode newnode = new TreeNode();
newnode.Value = myrow["ID"].ToString();
newnode.Text = myrow["TableName"].ToString();
newnode.Target = myrow["Target"].ToString();
newnode.NavigateUrl = myrow["url"].ToString();
newnode.ImageToolTip = myrow["ParentID"].ToString();
newnode.Expanded = bool.Parse(myrow["expanded"].ToString());
httree.Add(newnode.Value, newnode);
}
foreach (DataRow treerow in dtregion.Rows)
{
TreeNode newnode = (TreeNode)httree[treerow["ID"].ToString()];
TreeNode parentnode = (TreeNode)httree[newnode.ImageToolTip];
if (parentnode != null)
{
parentnode.ChildNodes.Add(newnode);
}
else
{
this.TreeView1.Nodes.Add(newnode);
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class treeview : System.Web.UI.Page
{
DBAccess db = new DBAccess();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
hashTable();
}
}
public DataTable GetTreeTable()
{
DataTable dt = db.GetDataTable("select * from trvAdmin order by id");
return dt;
}
void hashTable()
{
DataTable dtregion = GetTreeTable();
Hashtable httree = new Hashtable();
foreach (DataRow myrow in dtregion.Rows)
{
TreeNode newnode = new TreeNode();
newnode.Value = myrow["ID"].ToString();
newnode.Text = myrow["TableName"].ToString();
newnode.Target = myrow["Target"].ToString();
newnode.NavigateUrl = myrow["url"].ToString();
newnode.ImageToolTip = myrow["ParentID"].ToString();
newnode.Expanded = bool.Parse(myrow["expanded"].ToString());
httree.Add(newnode.Value, newnode);
}
foreach (DataRow treerow in dtregion.Rows)
{
TreeNode newnode = (TreeNode)httree[treerow["ID"].ToString()];
TreeNode parentnode = (TreeNode)httree[newnode.ImageToolTip];
if (parentnode != null)
{
parentnode.ChildNodes.Add(newnode);
}
else
{
this.TreeView1.Nodes.Add(newnode);
}
}
}
}
数据库:
USE [Vegas]
GO
/****** 对象: Table [dbo].[trvadmin] 脚本日期: 04/17/2008 16:26:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[trvadmin](
[id] [int] NULL,
[parentID] [int] NULL,
[tableName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[url] [nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
[target] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[expanded] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
/****** 对象: Table [dbo].[trvadmin] 脚本日期: 04/17/2008 16:26:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[trvadmin](
[id] [int] NULL,
[parentID] [int] NULL,
[tableName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[url] [nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
[target] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[expanded] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
喜欢请赞赏一下啦^_^