主题:[讨论]C#有关TreeView动态生成根节点、一级子节点以及点击生成二级子节点问题
我在Page_load事件中生成根节点和一级子节点,想实现点击某个一级子节点则会出现与之对应的二级子节点。但现在我的问题是点击后不但出现二级子节点,连根节点和一级子节点也同时又生成了。应该怎样解决这个问题?谢谢指点!
代码如下:
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;
using System.Data.SqlClient;
public partial class test1 : System.Web.UI.Page
{
private DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
dt = new DataTable();
gettable(dt);
//动态生成根节点
DataRow[] rootrow = dt.Select("parentid='a'");
string strid = rootrow[0]["parentid"].ToString();
string strname = rootrow[0]["name"].ToString();
TreeNode TN = new TreeNode(strid);
TN.Text = strname;
TN.Value = strid;
this.TreeView1.Nodes.Add(TN);
//动态生成子节点
DataRow[] noderows = dt.Select("parentid='" + strname + "'");
foreach (DataRow d in noderows)
{
TreeNode PN = new TreeNode();
string name = d["name"].ToString();
string values = d["id"].ToString();
PN.Text = name;
PN.Value = values;
PN.Collapse();
TN.ChildNodes.Add(PN);
}
}
//得到一个由id,name,parentid组成的新表
protected void gettable(DataTable table)
{
SqlConnection conn = new SqlConnection("Data Source=HDN;Initial Catalog=card;Integrated Security=True");
conn.Open();
DataRow dr;
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("parentid", typeof(string));
SqlCommand comm = new SqlCommand("select ID,Name,ParentID from test1", conn);
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
dr = dt.NewRow();//用DataTable的NewRow方法新建一个DataRow对象的实例dr
dr[0] = reader["ID"].ToString();//数据库中的id字段填入这一行的第一列
dr[1] = reader["Name"].ToString();//数据库中的name字段填入这一行的第二列
dr[2] = reader["ParentID"].ToString();
dt.Rows.Add(dr);//把这一行插入到到表格dt中
}
reader.Close();
return;
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
dt = new DataTable();
gettable(dt);
//考虑到有可能名称同名,所以用ID来查询是最完善的做法
//string str = TreeView1.SelectedValue.ToString();
DataRow[] row = dt.Select("id='" + TreeView1.SelectedValue + "'");
string parentid = row[0]["name"].ToString();
DataRow[] leafrow = dt.Select("parentid='" + parentid + "'");
foreach (DataRow d in leafrow)
{
TreeNode LeafNode = new TreeNode();
string name = d["name"].ToString();
string values = d["id"].ToString();
LeafNode.Text = name;
LeafNode.Value = values;
TreeView1.SelectedNode.ChildNodes.Add(LeafNode);
}
}
}
代码如下:
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;
using System.Data.SqlClient;
public partial class test1 : System.Web.UI.Page
{
private DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
dt = new DataTable();
gettable(dt);
//动态生成根节点
DataRow[] rootrow = dt.Select("parentid='a'");
string strid = rootrow[0]["parentid"].ToString();
string strname = rootrow[0]["name"].ToString();
TreeNode TN = new TreeNode(strid);
TN.Text = strname;
TN.Value = strid;
this.TreeView1.Nodes.Add(TN);
//动态生成子节点
DataRow[] noderows = dt.Select("parentid='" + strname + "'");
foreach (DataRow d in noderows)
{
TreeNode PN = new TreeNode();
string name = d["name"].ToString();
string values = d["id"].ToString();
PN.Text = name;
PN.Value = values;
PN.Collapse();
TN.ChildNodes.Add(PN);
}
}
//得到一个由id,name,parentid组成的新表
protected void gettable(DataTable table)
{
SqlConnection conn = new SqlConnection("Data Source=HDN;Initial Catalog=card;Integrated Security=True");
conn.Open();
DataRow dr;
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("parentid", typeof(string));
SqlCommand comm = new SqlCommand("select ID,Name,ParentID from test1", conn);
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
dr = dt.NewRow();//用DataTable的NewRow方法新建一个DataRow对象的实例dr
dr[0] = reader["ID"].ToString();//数据库中的id字段填入这一行的第一列
dr[1] = reader["Name"].ToString();//数据库中的name字段填入这一行的第二列
dr[2] = reader["ParentID"].ToString();
dt.Rows.Add(dr);//把这一行插入到到表格dt中
}
reader.Close();
return;
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
dt = new DataTable();
gettable(dt);
//考虑到有可能名称同名,所以用ID来查询是最完善的做法
//string str = TreeView1.SelectedValue.ToString();
DataRow[] row = dt.Select("id='" + TreeView1.SelectedValue + "'");
string parentid = row[0]["name"].ToString();
DataRow[] leafrow = dt.Select("parentid='" + parentid + "'");
foreach (DataRow d in leafrow)
{
TreeNode LeafNode = new TreeNode();
string name = d["name"].ToString();
string values = d["id"].ToString();
LeafNode.Text = name;
LeafNode.Value = values;
TreeView1.SelectedNode.ChildNodes.Add(LeafNode);
}
}
}