C# 递归TreeView 与根据选择的节点获得整个节点路径
private void loadtree()
{
GTree.Nodes.Clear();
ds = workdo.GetDataSet();
InitTree(GTree.Nodes, "0");
}
private void InitTree(TreeNodeCollection Nds, string parentid)
{
DataView dv = new DataView();
TreeNode tmpNd = null;
dv.Table = ds.Tables[0];
dv.RowFilter = String.Format("ParentID='{0}'", parentid);
foreach (DataRowView drv in dv)
{
tmpNd = new TreeNode();
tmpNd.Text = (string)drv["WorkName"];//节点名称
tmpNd.Value = (string)drv["ID"];
//tmpNd.NavigateUrl = String.Format("?id={0}", drv["ID"]);//节点URL
//tmpNd.ImageUrl = ""; //节点图片
if (parentid == "0")
tmpNd.Expanded = true;
else
tmpNd.Expanded = false;
Nds.Add(tmpNd);
InitTree(Nds[Nds.Count - 1].ChildNodes, drv["ID"].ToString());
}
}
/// <summary>
/// 節點變化時候,頁面顯示操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public event EventHandler SelectedNodeChanged;
protected void GTree_SelectedNodeChanged(object sender, EventArgs e)
{
this.NodeID = GTree.SelectedNode.Value;
// = Tree1.SelectedNode.ValuePath;//value完整路径
string str = "";
TreeNode parentNode = GTree.SelectedNode.Parent;
while (parentNode != null)
{
str = "/" + parentNode.Text + str;
parentNode = parentNode.Parent;
}
this.NodeName = (str + "/" + GTree.SelectedNode.Text).Substring(1); //Text完整路径名
txtContent_TextTree.Value = this.NodeName;
if (SelectedNodeChanged != null)
SelectedNodeChanged(sender, e);
}