dropdownlist无限分类

 private string toadd = "├";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!this.IsPostBack)
        //{
        //    this.buildTree();
        //}
        if (!IsPostBack)
        {
            this.GetArticleCategory("根列表");
        }
        this.DropDownList1.Items.Insert(0,new ListItem ("Search all","all"));
        this.display("根结点");
    }
    private void GetArticleCategory(string pid)
    {
        SqlConnection con = SQL.connection();
        string sql = "select n_id,n_name from news where n_fathername=@pid";
        SqlCommand cmd = new SqlCommand(sql,con );
        cmd.Parameters.AddWithValue("@pid",pid );
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            this.DropDownList1.Items.Add(new ListItem (toadd +""+reader .GetValue (1).ToString (),reader .GetValue (0).ToString ()));
            toadd += "─┴";
            this.GetArticleCategory(reader .GetValue (1).ToString ());
            toadd = toadd.Substring(0,toadd .Length -2);
        }
        reader.Close();
        con.Close();
    }
    private void display(string pname)
    {
        SqlConnection con = SQL .connection ();
        string sql = "select n_id,n_name from news where n_fathername=@pid";
        SqlCommand cmd = new SqlCommand(sql,con );
        cmd.Parameters.AddWithValue("@pid",pname );
        SqlDataAdapter ada = new SqlDataAdapter();
        ada.SelectCommand = cmd;
        DataSet ds = new DataSet();
        ada.Fill(ds, "tree");
        DataTable dt=ds.Tables ["tree"];
        if(dt.Rows.Count >0)
        {
            foreach (DataRow dr in dt.Rows )
            {
                display(dr["n_name"].ToString ());

            }
        }
    }

你可能感兴趣的:(list)