代码如下:为什么我点击没有子节点的节点时,会添加整个节点:如上图:我点击IE就会添加整个IE目录,点击IE/dd又会添加整个IE。请大虾帮帮手。
namespace WebApplication2
{ public partial class DeptFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ DataView dv = drv("select DirID,Name,Parentid from directory where parentid=(select dirid from directory where name='"+Request.QueryString["txtDept"]+"')");
TreeNode n = new TreeNode();
n.Text = Request.QueryString["txtDept"];
this.TreeView1.Nodes.Add(n); foreach (DataRowView datav in dv)
{
TreeNode tn = new TreeNode();
tn.Text = datav["name"].ToString();
tn.Value = datav["dirid"].ToString();
n.ChildNodes.Add(tn); AddChildNodes(tn);
tn.SelectAction = TreeNodeSelectAction.Expand; }
TreeView1.ExpandAll();
} private DataView drv(string sql)
{
MySqlConnection con = new MySqlConnection("server=xxx;database=xxx;user id =xxx; password =xxx; ");
MySqlDataAdapter sda = new MySqlDataAdapter(sql, con);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt.DefaultView;
} private void AddChildNodes(TreeNode t)
{
int dirid = Convert.ToInt32(t.Value.ToString());
DataView d = drv("select * from directory where parentid=" + dirid);
foreach (DataRowView datav in d)
{
TreeNode node = new TreeNode();
node.Text = datav["name"].ToString();
node.Value = datav["dirid"].ToString();
t.ChildNodes.Add(node);
}
}
}
}
if(!IsPostBack)
{}