看看我的代码~~
if(!Page.IsPostBack)
{
SqlConnection CN = new SqlConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]);
try 
{
SqlDataAdapter adp = new SqlDataAdapter("select CategoryId,Description,ParentCategotyId from Categories",CN);
DataSet ds=new DataSet();
adp.Fill(ds);
this.ViewState["ds"]=ds; 
}
catch (Exception ex)
{
Session["Error"] = ex.ToString();
Response.Redirect("error.aspx");       //?跳转程序的公共错误处理页面
}
finally 
{
CN.Close();
}
//调用递归函数,完成树形结构的生成
AddTree(0, (TreeNode)null);
}public void AddTree(int ParentCategotyId,TreeNode pNode) //ParentID
{
DataSet ds=(DataSet) this.ViewState["ds"]; 
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter =  "[ParentCategotyId] = " + ParentCategotyId;
 
foreach(DataRowView Row in dvTree) 
{
TreeNode Node=new TreeNode() ;
if(pNode == null) 
{    //添加根节点
Node.Text = Row["Description"].ToString();//ConText
TreeView1.Nodes.Add(Node);
Node.Expanded=true;
AddTree(Int32.Parse(Row["CategoryId"].ToString()), Node);    //再次递归   ID

else 
{   //?添加当前节点的子节点
Node.Text = Row["Description"].ToString();
pNode.Nodes.Add(Node);
Node.Expanded = true;
AddTree(Int32.Parse(Row["CategoryId"].ToString()),Node);     //再次递归
}
}                   
}谁知道treeview加载时默认不要展开根节点??只有父节点,而没有根节点和子节点?点击父节点才展开!!