Fork me on GitHub
无限级分类1

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HyeyBussiness.NewInvestment
{
    public partial class InvestmentSearch : System.Web.UI.UserControl
    {
        #region 初始化
        DataView dv;
        /// <summary>
        /// 层次分割符
        /// </summary>
        const string STR_TREENODE = "┆┄";
        /// <summary>
        /// ID
        /// </summary>
        const string STR_ID = "ID";
        /// <summary>
        /// Parent ID
        /// </summary>
        const string STR_PARENTID = "PID";
        /// <summary>
        /// Type Name 
        /// </summary>
        const string STR_DISPLAYNAME = "Name";
        #endregion

        #region 加载页面
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Page.IsPostBack == false)
            {
                #region 初始化下拉列表
                DataTable dt = new DataTable();
                dt = null;
                if (this.Query_ID != null)
                {
                    dt = ClassNameSource(this.Query_ID);
                }
                else
                {
                    dt = ClassNameSource("");
                }

                dv = new DataView(dt);
                if (dt != null)
                {
                    dv.Sort = STR_PARENTID;
                }
                string schar = STR_TREENODE;
                if (this.Query_ID == null || this.Query_ID == string.Empty)
                {
                    if (this.drpListTree.Items.FindByValue("-1") == null)
                        this.drpListTree.Items.Insert(0, new ListItem("--请选择--", "-1"));
                }
                if (dt != null && dv.Table.Rows.Count > 0)
                {
                    RecursBind(0, ref schar);
                }
                #endregion
            }
        }
        #endregion

        #region 下拉列表

        /// <summary>
        /// 递归绑定                
        /// DropDownList
        /// hyey.wl 
        /// 2010-07-29
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="schar"></param>
        private void RecursBind(int pid, ref string schar)
        {
            DataRowView[] rows = dv.FindRows(pid);
            if (rows.Length == 0) schar = STR_TREENODE;
            foreach (DataRowView row in rows)
            {
                if (pid != 0)
                {
                    schar += STR_TREENODE;
                }
                this.drpListTree.Items.Add(new ListItem(schar + row[STR_DISPLAYNAME].ToString(), row["Code"].ToString()));
                RecursBind(Convert.ToInt32(row[STR_ID]), ref schar);

            }
            schar = STR_TREENODE;

        }


        /// <summary>
        /// Class Name
        /// </summary>
        /// <param name="strChannel">ID</param>
        /// <returns>Data Table Source</returns>
        private DataTable ClassNameSource(string strID)
        {
            string strSql = string.Empty;

            if (strID.Trim().Length > 0)
            {
                strSql = "SELECT ID ,PID,Name,Code  FROM dbo.typetab where PID != -1 AND PID  = '" + strID.Replace("'", "").Replace("-", "") + "'";
            }
            else
            {
                strSql = "SELECT ID ,PID,Name,Code  FROM dbo.typetab  where PID !=-1  ";
            }
            DataSet ds = HyeyClass.DALFactory.DataAccess.CreateComm().GetList(strSql);
            DataTable dt = ds.Tables[0];
            if (ds != null && ds.Tables[0].Rows.Count > 0)
                return dt;
            else
                return null;
        }
        #endregion

        #region 查询的参数
        /// <summary>
        /// 类型编号
        /// </summary>
        protected string Query_ID
        {
            get
            {
                if (Request.Params["ID"] != null && Request.Params["ID"].ToString() != string.Empty)
                {
                    return this.Request.Params["ID"].Trim();
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion
    }
}
Data:

posted on 2010-07-30 16:46  HackerVirus  阅读(196)  评论(0编辑  收藏  举报