码迷,mamicode.com
首页 > 其他好文 > 详细

在DropDownList里显示多级分类

时间:2014-09-04 16:24:29      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   ar   for   数据   2014   

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());
        }

bubuko.com,布布扣 

在DropDownList里显示多级分类

标签:style   blog   http   color   os   ar   for   数据   2014   

原文地址:http://www.cnblogs.com/liuswi/p/3956044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!