给你个完整例子,希望你能看懂。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using UDS.Components;
namespace UDS
{
///
/// TreeView 的摘要说明。
///
public partial class LeftTree : System.Web.UI.Page
{
public string RealName;
public string userName;
protected DataTable dataTbl1;
protected void Page_Load(object sender, System.EventArgs e)
{
userName = Request.Cookies["UserName"].Value.ToString();
UDS.Components.Staff s = new UDS.Components.Staff();
int staffid = s.GetStaffIDByName(userName);
SqlDataReader dr = s.GetStaffInfo(staffid);
dr.Read();
RealName = dr["Position_Name"].ToString() + " " + dr["RealName"].ToString();
dr.Close();
if (!Page.IsPostBack)
{
LoadData();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
}
#endregion
private void LoadData()
{
try
{
DataTable table = new DataTable();
InitRootNodeDataTable(); //得到所有所有父节点,放到DataTable中,这里默认根节点的父节为0
BindRootNode(dataTbl1, treeCatalog.Nodes); //绑定所有的父节点
}
catch (Exception ex)
{
Response.Write("");
UDS.Components.Error.Log(ex.ToString());
}
}
#region 得到父节点的字节点,放到DataTable中
///
/// 初始化 RootNode DataTable
///
private void InitRootNodeDataTable()
{
Database data = new Database();
SqlDataReader dataReader = null;
SqlParameter[] prams = {
data.MakeInParam("@UserName", SqlDbType.VarChar , 20, userName)
};
try
{
data.RunProc("sp_GetShowClass", prams, out dataReader);
}
catch (Exception ex)
{
UDS.Components.Error.Log(ex.ToString());
}
dataTbl1 = Tools.ConvertDataReaderToDataTable(dataReader);
dataReader.Close();
dataTbl1.TableName = "TreeView";
}
///
/// 初始化 ChildNode DataTable
///
private DataTable GetChildNodeDataTable(int ClassParentID)
{
Database data = new Database();
SqlDataReader dataReader = null;
string sql = "select *, (select count(*) from uds_class where Display < 2 and classParentID = a.ClassID and classParentid <> ClassID) as childNum from uds_class a where Display < 2 and classParentid =" + ClassParentID + " and classParentid <> ClassID order by DocSort";
try
{
dataReader = data.ExSQLReDr(sql); ;
}
catch (Exception ex)
{
Response.Write(ex.ToString());
UDS.Components.Error.Log(ex.ToString());
}
DataTable t = Tools.ConvertDataReaderToDataTable(dataReader);
dataReader.Close();
t.TableName = "TreeView";
return t;
}
#endregion
#region 填充节点
private void BindRootNode(DataTable table, TreeNodeCollection node)
{
DataView dv = new DataView(table);
foreach (DataRowView drv in dv)
{
TreeNode NewNode = new TreeNode();
NewNode.Value = drv["ClassID"].ToString();
NewNode.Text = "" + drv["ClassName"].ToString() + "";
NewNode.ImageUrl = GetIcon(drv["ClassType"].ToString());
NewNode.NavigateUrl = "UnitiveDocument/Switch.aspx?Action=1&ClassID=" + NewNode.Value;
NewNode.Target = "MainFrame";
node.Add(NewNode);
//判断是否还有子节点,如果还有子节点,显示展开符号
if (Convert.ToInt32(drv["childNum"]) > 0)
NewNode.PopulateOnDemand = true;
}
}
private void BindNode(DataTable table, TreeNodeCollection node)
{
DataView dv = new DataView(table);
foreach (DataRowView drv in dv)
{
TreeNode NewNode = new TreeNode();
NewNode.Value = drv["ClassID"].ToString();
NewNode.Text = "" + drv["ClassName"].ToString() + "";
NewNode.NavigateUrl = "UnitiveDocument/Switch.aspx?Action=1&ClassID=" + NewNode.Value;
NewNode.Target = "MainFrame";
node.Add(NewNode);
//判断是否还有子节点,如果还有子节点,显示展开符号
if (Convert.ToInt32(drv["childNum"]) > 0)
NewNode.PopulateOnDemand = true;
}
}
#region 获取节点图标
///
/// 获取节点图标
///
private string GetIcon(string ClassType)
{
string rtnValue = "../DataImages/";
switch (ClassType)
{
case "0":
rtnValue += "flag.gif";
break;
case "1":
rtnValue += "myDoc.gif";
break;
case "2":
rtnValue += "mail.gif";
break;
case "3":
rtnValue += "page.gif";
break;
case "4":
rtnValue += "staff.gif";
break;
case "5":
rtnValue += "help_page.gif";
break;
case "6":
rtnValue += "MyTask.gif";
break;
case "7":
rtnValue += "mail.gif";
break;
case "8":
rtnValue += "myDoc.gif";
break;
case "9":
rtnValue += "DocFlow.gif";
break;
case "10":
rtnValue += "ClientManage.gif";
break;
case "11":
rtnValue += "myLinkman.gif";
break;
case "12":
rtnValue += "position.gif";
break;
case "13":
rtnValue += "roleManage.gif";
break;
case "14":
rtnValue += "kaoqin.gif";
break;
case "15":
rtnValue += "workadmin.gif";
break;
case "16":
rtnValue += "message.gif";
break;
case "17":
rtnValue += "usbkey.gif";
break;
default:
rtnValue += "red_ball.gif";
break;
}
return rtnValue;
}
#endregion
#endregion
#region 填充节点事件
protected void TreeCatalog_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
//if (e.Node.ChildNodes.Count == 0)
getDataNode(e.Node.Value, e.Node); //点加号展开时调用.得到数据并绑定.传入点击结点 ID,和点击节点对像.
}
#endregion
private void getDataNode(String ParentId, TreeNode Node) //向结点填充数据.
{
BindNode(GetChildNodeDataTable(Convert.ToInt16(ParentId)), Node.ChildNodes);
}
}
}