用递归在Dropdownlist中显示树状结构(连数据库)

2010-08-28 10:50:27来源:西部e网作者:

protected void Page_Load(object sender, EventArgs e)
        {
            Class.sqlconn.Open();
          
            SqlDataAdapter da = new SqlDataAdapter("select so_id,so_name,so_parentid from en_sort order by so_order asc", Class.sqlconn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Class.sqlconn.Close();
            BindData(dt, 0,"");

        }

        private void BindData(DataTable dt, int id,string blank)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "so_parentid = " + id.ToString();

            if (id != 0)
            {
                blank += "|─";
            }
            foreach (DataRowView drv in dv)
            {
            
                ListItem li = new ListItem();
                li.Text = blank + drv["so_name"].ToString();
                li.Value = drv["so_id"].ToString();
                this.DropDownList1.Items.Add(li);
                BindData(dt, Convert.ToInt32(drv["so_id"]),blank);
            }
        }

关键词:ASP.NET

赞助商链接: