asp.net无限分类绑定DropDownList

 数据结构 monitor_group

 

monitor_grp_id      monitor_grp_name     parent_id       level               childCount       orderby

[int,自动递增]           [nvarchar,not null]      [int,not null]  [int,not null]   [int,not null]     [int ,null]

                      1       数据库服务器                 0                    1                   2
                      2       应用服务器                    0                    1                   2
                      3       系统服务器                    0                    1                   0
                      4       WEB服务器                   1                    2                   0
                      5      邮件服务器                     1                    2                   0
                      6     代理服务器                      2                    2                   0
                      7     Ftp服务器                       2                    2                   0

 

 

/App_code/data.cs

 

using System;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
///common 的摘要说明
/// </summary>
///

namespace yihan
{

    namespace Data
    {
       
        public class myDataBind
        {
           
            public myDataBind()
            {
                //
                //TODO: 在此处添加构造函数逻辑
                //
            }

 

           public static void DropDownList_monitor_grp_id(DropDownList obj,DataTable dt,int parent_id)
            {
                //绑定DropDownList树
                //obj:绑定对象;dt:DataTable对象;parent_id:父ID;
                DataRow[] dr = dt.Select("parent_id=" + parent_id);
                int currentLenght = 0;         //当前次数
                foreach (DataRow row in dr)
                {

                   string treeLine = "";    //树线
                    currentLenght += 1;
                    for (var i = 1; i <= Convert.ToInt32(row["level"]); i++)
                    {
                        //计算treeLine
                        if (i == Convert.ToInt32(row["level"]))
                        {
                            if (currentLenght == dr.Length)     //判断当前次数是否与本次dr总数量相等
                            { treeLine += "└ "; }
                            else
                            { treeLine += "├ "; }

                        }
                        else
                        {
                            treeLine += "│ ";
                        }

                    }
                    obj.Items.Add(new ListItem(treeLine + row["monitor_grp_name"],row["monitor_grp_id"].ToString()));
                    DropDownList_monitor_grp_id(obj, dt, Convert.ToInt32(row["monitor_grp_id"]));
                }
            }// DropDownList_GetTree End

 

         }//myDataBind End

     }
}

 

class_list.aspx.cs

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using yihan.Data;



public partial class monitor_monitor_group_class_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            Bind_monitor_grp_id();
        }
    }

 

    protected void Bind_monitor_grp_id()
    {
        DataTable dt = new DataTable();
        string resultString = "";
        string sql = "select * from monitor_group  order by orderby desc,monitor_grp_id";
        DbConn conn = new DbConn();
        dt = conn.DataTable(sql);
        myDataBind.DropDownList_monitor_grp_id(monitor_grp_id, dt, 0);  //调用
        monitor_grp_id.Items.Insert(0, new ListItem("--监视器组--", ""));
        dt.Dispose();
        conn.Close();
    }   
}

 

class_list.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="class_list.aspx.cs" Inherits="monitor_monitor_group_class_list" %>

<body> 
    <form id="form1" runat="server">
    <asp:DropDownList ID="monitor_grp_id" runat="server">
    </asp:DropDownList>
    </form>
</body>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值