动态加载树型列表


 


 

 public void FillTree( TreeNodeCollection tns, string strParentID)
    {
        TreeNode tmpNd;
        int intId;
        DataView dv = new DataView();
        dv.Table = ds.Tables[0];
        dv.RowFilter="upId='"+strParentID+"'";

        foreach (DataRowView drv in dv)
        {

            tmpNd = new TreeNode();
            intId =Convert.ToInt32(drv["id"].ToString());
            tmpNd.Text = drv["className"].ToString();
            tmpNd.NavigateUrl = "newsList.aspx?classId=" + intId;
            tmpNd.Target = "mainFrame";
            tns.Add(tmpNd);
            FillTree(tmpNd.ChildNodes, intId.ToString());
        }
    } 


    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=news;Persist Security Info=True;User ID=sa");
            conn.Open();
            string sqlStr;
            sqlStr = "select * from newsClass order by id";
            SqlDataAdapter adp = new SqlDataAdapter(sqlStr, conn);
            ds = new DataSet();
            adp.Fill(ds);
            conn.Close(); 
            FillTree(TreeView1.Nodes, "0");
        }
    }

posted @ 2007-06-11 15:53  代码缔造的帝国  阅读(305)  评论(0编辑  收藏  举报