关闭

c#中树结构的显示

1194人阅读 评论(0) 收藏 举报

1 以下根据一个表来做个小sample:

 假如表的字段有:id,classId,className,fatherId,flag

其中数据有:

1 101 书籍 0 0
2 101001 计算书 101 0
3 101002 英语 101 0
4 101003 物理 101 0
5 101004 高数1 101 0
6 101005 高数2 101 0
7 102 电脑 0 0
8 102001 联想电脑 102 0
9 102002 长城电脑 102 0
10 102003 tcl电脑 102 0
11 102004 ibm电脑 102 0
12 103 手机 0 0
13 103001 诺基亚 103 0
14 103002 三星 103 0
15 103003 oppl 103 0
16 103004 tcl 103 0

前台aspx页面上可以这样写:要先引入几个js文件:

<script language="javascript"  src="scripts/button/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script language="javascript" src="scripts/button/button.js" type="text/javascript"></script>
    <script src="scripts/jsframework.js" type="text/javascript" language="javascript"></script>

<div style="float:left;width:140px;margin-right:5px;border:0px #ccc solid">
            <div class="ClassTitle">分类</div>
            <div class="ClassBD">
              <ul id="tree" class="tree" style="width:125px; height:600px;overflow:auto;">
                 <script language="javascript" type="text/javascript">  
                    var intTarget="IFrm0";
                    <%=LoadTree()%>                    
                </script>
              </ul>
          </div>

后台.cs文件中可以这样写:

  public string LoadTree()
    {
        string sqlstr = "SELECT fatherId,classId, className FROM typeInfo WHERE (ClassID Like '10%') ";
       
        DataTable dt = new DataTable();

       string conn=ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
       DataSet ds = sql.GetDateSet(sqlstr,conn);
        dt = ds.Tables[0];
        string nodes = "";
        string nodes1 = "";
        string fatherId = string.Empty;
        nodes1 += "var data = {};\r\ndata[\"-1_0\"] = \"text: 类型;url:Default.aspx?ClassID = -1\";\r\n";
        foreach (DataRow dr in dt.Rows)
        {
             string ClassID = dr["classId"].ToString();
            fatherId = dr["fatherId"].ToString();
            if (dr["fatherId"].ToString() == "0")
            {
                nodes1 += "data[\'" + dr["fatherId"].ToString() + "_" + dr["classId"].ToString() + "\'] = \'text:" + dr["className"].ToString() + ";url:Default.aspx?ClassID=" + ClassID + "\';\r\n";
                  }
            else
            {
                nodes1 += "data[\'" + dr["fatherId"].ToString() + "_" + dr["classId"].ToString() + "\'] = \'text:" + dr["className"].ToString() + ";url:Default.aspx?ClassID=" + ClassID + "\';\r\n";
}

        }
        nodes = nodes1 ;
        string TreeViews = "Tree" + fatherId;
        nodes += "Using(\"System.Web.UI.WebControls.MzTreeView\");\r\n";
        nodes += "var " + TreeViews + " = new MzTreeView();\r\n";
        nodes += TreeViews + ".dataSource = data;\r\n";
        nodes += TreeViews + ".setJsDataPath(\"csdn/community/treedata/\");\r\n";
        nodes += TreeViews + ".setXmlDataPath(\"csdn/community/treedata/\");\r\n";
         nodes += TreeViews + ".autoSort=false;\r\n";
        nodes += TreeViews + ".useCheckbox=false;\r\n";
        nodes += TreeViews + ".canOperate = true;\r\n";
        nodes += "document.write(" + TreeViews + ".render());\r\n";
        nodes += TreeViews + ".expandLevel(1);\r\n";
        return nodes;
       
    }

 

 public static DataSet GetDateSet(string StrSql, string Conn)
    {
        SqlConnection conn = new SqlConnection(Conn);
        SqlCommand cm = new SqlCommand();
        cm.Connection = conn;
        cm.CommandText = StrSql;
        cm.CommandTimeout = 30;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cm;
        DataSet ds = new DataSet();
        try
        {
            conn.Open();
            da.Fill(ds);
        }
        catch (Exception error)
        {
            sys.ErrorMsg(error.Message);
            ds = null;
        }
        finally
        {
            conn.Dispose();
            conn.Close();
        }
        return ds;
    }

 

运行出的效果类似于csdn论坛中的树控件,用这种方法加载树控件时速度比较快。

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:282192次
    • 积分:2861
    • 等级:
    • 排名:第12470名
    • 原创:32篇
    • 转载:111篇
    • 译文:10篇
    • 评论:12条
    文章分类
    最新评论