日期:2014-05-18 浏览次数:20475 次
DataTable DataC = new DataTable(); public void CreateNodesOfParent(int iParent,TreeNode pNode) { DataView dvwData = new DataView(DataC); dvwData.RowFilter = "[UpperID] = " + iParent; TreeView1.AutoSelect = false; foreach(DataRowView Row in dvwData) { if(pNode == null) { TreeNode zNode= new TreeNode(); zNode.Text=Row["Name"].ToString(); zNode.ID =Row["ID"].ToString(); zNode.NavigateUrl ="CategoryList.aspx?parent_id="+Row["ID"].ToString()+ "&parent_name="+Row["Name"].ToString(); zNode.Target ="right"; zNode.ImageUrl="../images/folder.gif"; zNode.Expanded=true; TreeView1.Nodes.Add (zNode); CreateNodesOfParent(Int32.Parse(Row["ID"].ToString()),zNode); } else { TreeNode zNode= new TreeNode(); zNode.Text=Row["Name"].ToString (); zNode.ID =Row["ID"].ToString (); zNode.NavigateUrl ="CategoryList.aspx?parent_id="+Row["ID"].ToString()+ "&parent_name="+Row["Name"].ToString(); zNode.Target ="right"; zNode.ImageUrl="../images/folder.gif"; pNode.Nodes.Add(zNode); CreateNodesOfParent(Int32.Parse(Row["ID"].ToString()),zNode); } } } CreateNodesOfParent(0,(TreeNode)null);
------解决方案--------------------
string strs; string[] str=new string[4]; TreeNode tn; TreeNode tn1; TreeNode tn2; TreeNode tn3; private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { using(SqlConnection conn=new SqlConnection("server=.;database=master;uid=sa;pwd=sa")) { SqlDataAdapter da=new SqlDataAdapter("select a from p order by a",conn); DataSet ds=new DataSet(); da.Fill(ds); for(int i=0;i<ds.Tables[0].Rows.Count;i++) { strs=ds.Tables[0].Rows[i]["a"].ToString(); str=strs.Split(",".ToCharArray()); if(ViewState["str0"]==null||ViewState["str0"].ToString()!=str[0]) { ViewState["str0"]=str[0]; tn=new TreeNode(); tn.Text=str[0]; tn.Target=str[0]; TreeView1.Nodes.Add(tn); if(ViewState["str1"]==null||ViewState["str1"].ToString()!=str[1]) { ViewState["str1"]=str[1]; tn1=new TreeNode();