淘先锋技术网

首页 1 2 3 4 5 6 7

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

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

其中数据有:

1101书籍00
2101001计算书1010
3101002英语1010
4101003物理1010
5101004高数11010
6101005高数21010
7102电脑00
8102001联想电脑1020
9102002长城电脑1020
10102003tcl电脑1020
11102004ibm电脑1020
12103手机00
13103001诺基亚1030
14103002三星1030
15103003oppl1030
16103004tcl1030

前台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论坛中的树控件,用这种方法加载树控件时速度比较快。