zoukankan      html  css  js  c++  java
  • DropDownlist显示树状

    啥都不说,直接上码...

     /// <summary>
        
    /// 绑定树视图
        
    /// </summary>
        
    /// <param name="dt">数据源</param>
        private void BindTreeView(DataTable dt)
        {
            ddlCategorys.Items.Clear(); //清除数据
            ddlCategorys.Items.Add(new ListItem("全部""0"));
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow[] nodeList = dt.Select("ParentID='0'");
                for (int i = 0; i < nodeList.Length; i++)
                {
                    ddlCategorys.Items.Add(new ListItem(nodeList[i][ddlCategorys.DataTextField].ToString(), nodeList[i][ddlCategorys.DataValueField].ToString()));
                    BindChildNode(dt, nodeList[i][ddlCategorys.DataValueField].ToString());
                }
            }
        }

        /// <summary>
        
    /// 绑定子节点
        
    /// </summary>
        
    /// <param name="dt">数据源</param>
        
    /// <param name="parentValue">父值</param>
        
    /// <param name="strBlank">分隔符</param>
        private void BindChildNode(DataTable dt, string parentValue)
        {
            DataRow[] nodeList = dt.Select(string.Format(" {0} = '{1}' ""ParentID", parentValue));
            for (int i = 0; i < nodeList.Length; i++)
            {
                string strBlank = StringOfChar(int.Parse(nodeList[i]["Level"].ToString()), "&nbsp;&nbsp;");
                ddlCategorys.Items.Add(new ListItem(HttpUtility.HtmlDecode(strBlank) + nodeList[i][ddlCategorys.DataTextField].ToString(), nodeList[i][ddlCategorys.DataValueField].ToString()));
                BindChildNode(dt, nodeList[i][ddlCategorys.DataValueField].ToString());
            }
        }

        private string StringOfChar(int strLong, string str)
        {
            string ReturnStr = string.Empty;
            if (strLong > 1)
            {
                for (int i = 1; i < strLong; i++)
                {
                    ReturnStr += str;
                }
                ReturnStr += "";
            }
            return ReturnStr;
        }

     效果图:

     数据源表结构:

     

  • 相关阅读:
    js防止按钮被多次点击
    jQuery:localStorage用法
    jQuery Ajax 前端和后端数据交互的问题
    <img>总结: 动态创建等问题
    jQuery
    Echarts 饼图(series)标题文字太长的换行设置
    echarts 为x轴、y轴添加滚动条
    video.js的应用
    关于跨域问题的详解
    麻省理工18年春软件构造课程阅读04“代码评审”
  • 原文地址:https://www.cnblogs.com/cr7/p/2661740.html
Copyright © 2011-2022 走看看