首页 > 代码库 > 在DropDownList里显示多级分类
在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()); }
在DropDownList里显示多级分类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。