后台代码:
protected void Page_Load(object sender, EventArgs e)//页面打开时执行
{
if (!IsPostBack)
{
PopulateNodes(TreeView1.Nodes, 0);
}
}
public DataTable GetTreeTable()//获得表
{
dbClient db = new dbClient();
DataTable dt = HttpContext.Current.Cache["Treeview"] as DataTable;//缓存表,保证速度
string sql = "select id,pid,yhm from users";
dt = db.getSQLTable(sql, "dt"); //获得表
return dt;
}
public void PopulateNodes(TreeNodeCollection nodes, Int32 intParentID)//节点展开
{
DataTable dt = GetTreeTable();
String strExpression = String.Empty;
strExpression = "[pid]=" + intParentID;
System.Data.DataRow[] dr;
dr = dt.Select(strExpression);
for (Int32 i = 0; i <= dr.GetUpperBound(0); i++)
{
TreeNode tn = new TreeNode();
tn.Text = dr[i]["yhm"].ToString().Trim();
tn.Value = dr[i]["id"].ToString();
tn.NavigateUrl = "Detail.aspx?id=" + tn.Value.ToString();//设置节点URL属性
tn.Target = "mainFrame";//设置节点Target属性
DataRow[] dr1;
dr1 = dt.Select("[pid]=" + tn.Value);
if (dr1.GetUpperBound(0) > -1)
{
tn.PopulateOnDemand = true;
}
nodes.Add(tn);
}
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)//触发节点展开
{
PopulateNodes(e.Node.ChildNodes, Int32.Parse(e.Node.Value));
}