在DropDownList里显示多级分类
protected void ddlBind() { DataTable dt = new DataTable(); ddlCategoryId.DataSource = getList("0", dt); ddlCategoryId.DataTextField = "title"; ddlCategoryId.DataValueField = "id"; ddlCategoryId.DataBind(); ddlCategoryId.Items.Insert(0, new ListItem("请选择类别", "-1")); } public DataTable getList(string pid, DataTable list) { BLL.article_category bll = new BLL.article_category(); DataTable dt; if (pid == "0") { dt = bll.GetList("parent_id='0'").Tables[0]; list = dt.Clone(); } else { dt = bll.GetList(" parent_id='" + pid + "'").Tables[0]; } if (dt.Rows.Count > 0) { foreach (DataRow row in dt.Rows) { int layer = Convert.ToInt32(row["class_layer"].ToString());//深度 string Title = row["title"].ToString().Trim(); if (layer > 1) { Title = "├ " + Title; Title = StringOfChar(layer - 1, " ") + Title; } row["title"] = Title; list.ImportRow(row); getList(row["id"].ToString(), list); } } return list; }
/// <summary> /// 生成指定长度的字符串,即生成strLong个str字符串 /// </summary> /// <param name="strLong">生成的长度</param> /// <param name="str">以str生成字符串</param> /// <returns></returns> public static string StringOfChar(int strLong, string str) { string ReturnStr = ""; for (int i = 0; i < strLong; i++) { ReturnStr += str; } return ReturnStr; } /// <summary> /// 获得数据列表 /// </summary> public DataSet GetList(string strWhere) { StringBuilder strSql=new StringBuilder(); strSql.Append("select * "); strSql.Append(" FROM article_category"); if(strWhere.Trim()!="") { strSql.Append(" where "+strWhere); } return OracleHelper.ExecuteDataset(strSql.ToString()); }
id | title | parent_id | class_layer |
1 | IT资讯 | 0 | 1 |
2 | IT业界 | 1 | 2 |