asp.net使用dorpdownlist绑定无限级分类

时间:2023-03-09 23:17:30
asp.net使用dorpdownlist绑定无限级分类
       /// <summary>
        /// 绑定父项分类
        /// </summary>        
     protected void DDLBindClass()
{
DataSet ds = new BLL_Cpgl_cpfl().GetAllList();
DataTable dt = ds.Tables[0];
DataRow[] dr = dt.Select("Parentid=0");
for (int i = 0; i < dr.Length; i++)
{
ddlParent.Items.Add(new ListItem(dr[i]["name"].ToString(), dr[i]["id"].ToString()));
BindChild(dt, dr[i]["id"].ToString(), 1);
}
this.ddlParent.Items.Insert(0, new ListItem("--请选择--", "0"));
}       
     /// <summary>       
     /// 绑定子分类信息
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="parentID">父分类ID </param>
        /// <param name="count">分级级别</param>
private void BindChild(DataTable dt, string parentID, int count)
{
DataRow[] dr = dt.Select("Parentid= '" + parentID + "'");
string strindex = "--";
for (int k = 0; k < count; k++)
{
strindex = System.Web.HttpUtility.HtmlDecode("&nbsp;") + System.Web.HttpUtility.HtmlDecode(" ") + strindex;
}
for (int i = 0; i < dr.Length; i++)
{
ddlParent.Items.Add(new ListItem(strindex + dr[i]["name"].ToString(), dr[i]["id"].ToString()));
BindChild(dt, dr[i]["id"].ToString(), count + 1);
} }