MyTree樹控件_Ajax實現異步加載節點

最近一直在尋找一個比較方便好用的樹控件,MyTree樹控件並沒有提供很多其他的功能,但在Ajax異步加載節點上實現的很簡潔,留作備忘。

MyTree的下載地址:http://www.itplus.com.cn/myTree/

 

下載後  DemoAjax異步加載例子

        Document:文檔

        myTree:放項目裏要引用的文件

 

MytreeTest.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MytreeTest.aspx.cs" Inherits="VistaToNewSchol.MytreeTest" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title></title>

    <script language="javascript" src="../myTree/myTree.js"></script>

     <script language="javascript" src="../myTree/bingo.js"></script>

     <script language="javascript">

     var tree;

     window.onload = function(){

     tree = new myTree("myFirstTree", "divTree", "../myTree/", "Blue");

         tree.dataFrom = "MyTreeGetNode.aspx"

         tree.load();

     }

 

     function myFirstTree_onClick(prmNodeId){

         tree.load(prmNodeId);

         tree.expandNode(prmNodeId);

     }

     </script>

    

</head>

<body>

    <form id="form1" runat="server">

    <div id="divTree"></div>

 

 

<div id="divStr"></div>

    </form>

</body>

</html>

 

 

 

 

MyTreeGetNode.aspx.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using System.Data;

using System.Data.SqlClient;

using System.Text;

 

namespace VistaToNewSchol

{

    public partial class MyTreeGetNode : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                int intId =-1;

                if (Request.QueryString["parentId"] != null)  //此處用parentId才能取的到值,應該是前臺js爲parentId賦的值,直接取出來就可以用了。

                {

                    intId = Convert.ToInt32(Request.QueryString["parentId"].ToString());

                }

               

               

                string html = getNode(intId);

                Response.Write(html);

 

                Response.End();

            }

        }

 

 

        //得到節點

        public string getNode(int parentID)

        {

            string html = "";

            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * ,(select count(metaId) from metadata where parentId=DB.metaId) AS SUBCOUNT ");

            strSql.Append(" from dbo.metadata AS DB ");

            strSql.Append(" where parentid=" + parentID + " ");

            DataSet ds = VistaToNewSchol.DAL.DbHelperSQL.Query(strSql.ToString());

 

            html += "[";

            if (ds.Tables[0].Rows.Count > 0)

            {

                int count = 0;

                foreach (DataRow row in ds.Tables[0].Rows)

                {

                    if (count == 0)

                    {

 

                    }

                    else

                    {

                        html += ",";

                    }

 

 

 

                    html += "{";

 

                    html += "nodeId:'" + row["metaId"].ToString() + "',";

                    html += "caption:'" + row["metaName"].ToString() + "' ,";

                    html += "attach:{";

                    html += "subCount: '" + row["SUBCOUNT"].ToString() + "',";//子節點個數

                    html += "url:'#n' ";

                    html += "}";

                 

                    html += "}";

 

                    count++;

                }

            }

 

            html += "]";

            return html;

 

 

        }

 

 

 

    }

}

發佈了27 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章