首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网页制作-> HTML教程
ajax实现无刷新树_ajax教程
作者:网友供稿 点击:0
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

1.建立一个aspx页面
html代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>小山</title>
    <link type="text/css" href="../../Styles/tree_css/tree.css" rel="stylesheet">
</head>
<body>
    <form id="Form1" runat="server">
    <table width=100% cellpadding=0 cellspacing=0 border=0>
        <colgroup>
            <col width=180 />
            <col />
        </colgroup>
        <tr>
            <td>
                <div class="TreeMenu" id="CategoryTree" style="width: 100%; height: 489px">
                </div>
            </td>
            <td>
                <iframe id=furl height=20 style="height: 497px; width: 100%;"></iframe>
            </td>
        </tr>
    </table>   
                           
            <script language="jscript">
            function el(id)
            {
                return document.getElementById(id);               
            }
            function ExpandSubCategory(iCategoryID)
            {
                var li_father = el("li_" + iCategoryID);
                if (li_father.getElementsByTagName("li").length > 0) //分类已下载
                {
                    ChangeStatus(iCategoryID);
                    return;
                }
               
                li_father.className = "Opened";
               
                switchNote(iCategoryID, true);
                AjaxMethod.GetSubCategory(iCategoryID, GetSubCategory_callback);               
            }

            function GetSubCategory_callback(response)
            {
                var dt = response.value.Tables[0];
                if (dt.Rows.length > 0)
                {
                    var iCategoryID = dt.Rows[0].FatherID;
                }
                var li_father = el("li_" + iCategoryID);
                var ul = document.createElement("ul");
                for (var i = 0;i < dt.Rows.length;i++)
                {
                    if (dt.Rows[i].IsChild == 1) //叶子节点
                    {
                        var li = document.createElement("li");
                        li.className = "Child";
                        li.id = "li_" + dt.Rows[i].CategoryID;
                       
                        var img = document.createElement("img");
                        img.id = dt.Rows[i].CategoryID;
                        img.className = "s";
                        img.src = "../../Styles/tree_css/s.gif";
                       
                        var a = document.createElement("a");
                        var id = dt.Rows[i].CategoryID;
                        a.onmouseover = function()
                        {
                            PreviewImage(id);
                        };
                        a.href = "javascript:OpenDocument(" + dt.Rows[i].CategoryID + ");";
                        a.innerHTML = dt.Rows[i].CategoryName;
                    }
                    else
                    {
                        var li = document.createElement("li");
                        li.className = "Closed";
                        li.id = "li_" + dt.Rows[i].CategoryID;
                       
                        var img = document.createElement("img");
                        img.id = dt.Rows[i].CategoryID;
                        img.className = "s";
                        img.src = "../../Styles/tree_css/s.gif";
                        img.onclick = function () {
                            ExpandSubCategory(this.id);
                        };
                        img.alt = "展开/折叠";
                       
                        var a = document.createElement("a");
                        a.href = "javascript:ExpandSubCategory(" +
                            dt.Rows[i].CategoryID + ");";
                        a.innerHTML = dt.Rows[i].CategoryName;
                    }
                    li.appendChild(img);
                    li.appendChild(a);
                    ul.appendChild(li);   
                }
                li_father.appendChild(ul);
               
                switchNote(iCategoryID, false);
            }
           
            // 叶子节点的单击响应函数
            function OpenDocument(iCategoryID)
            {
                // 预加载信息
                PreloadFormUrl(iCategoryID);
            }
           
            function PreviewImage(iCategoryID)
            {
               
            }

            function ChangeStatus(iCategoryID)
            {
                var li_father = el("li_" + iCategoryID);
                if (li_father.className == "Closed")
                {
                    li_father.className = "Opened";
                }
                else
                {
                    li_father.className = "Closed";
                }               
            }

            function switchNote(iCategoryID, show)
            {
                var li_father = el("li_" + iCategoryID);
                if (show)
                {
                    var ul = document.createElement("ul");
                    ul.id = "ul_note_" + iCategoryID;
                   
                    var note = document.createElement("li");
                    note.className = "Child";
                   
                    var img = document.createElement("img");
                    img.className = "s";
                    img.src = "../../Styles/tree_css/s.gif";
                   
                    var a = document.createElement("a");
                    a.href = "javascript:void(0);";
                    a.innerHTML = "请稍候";
                   
                    note.appendChild(img);
                    note.appendChild(a);
                    ul.appendChild(note);
                    li_father.appendChild(ul);
                }
                else
                {
                    var ul = el("ul_note_" + iCategoryID);
                    if (ul)
                    {
                        li_father.removeChild(ul);
                    }               
                }
            }

            // 加载根节点
            var tree = el("CategoryTree");
            var root = document.createElement("li");
            root.id = "li_0";
            tree.appendChild(root);
           
            // 加载页面时显示第一级分类
            ExpandSubCategory(0);
           
            function PreloadFormUrl(iCategoryID)
            {
                // 采用同步调用的方式获取图片的信息               
                var ds = AjaxMethod.GetFormsList(iCategoryID).value;
                // 如果返回了结果
                if (ds)
                {
                    // 判断数据表是否不为空
                    if (ds.Tables[0].Rows.length > 0)
                    {
                        // 返回的信息数据表
                        dt = ds.Tables[0];
                        el("furl").src = dt.Rows[0].FormUrl;                                   
                    }
                    else // 分类下没有
                    {                       
                    }
                }               
            }
            </script>           
    </form>
</body>
</html>2.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;

public partial class Pages_Home_HomePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Utility.RegisterTypeForAjax(typeof(AjaxMethod));
    }
}3.建立一个tree.css的css样式
a
{}{
    text-decoration:none;
}
a,a:visited
{}{
    color:#000;
    background:inherit;
}
body
{}{
    margin:0;
    padding:20px;
    font:12px tahoma,宋体,sans-serif;
}
dt
{}{
    font-size:22px;
    font-weight:bold;
    margin:0 0 0 15px;
}
dd
{}{
    margin:0 0 0 15px;
}
h4
{}{
    margin:0;
    padding:0;
    font-size:18px;
    text-align:center;
}
p
{}{
    margin:0;
    padding:0 0 0 18px;
}
p a,p a:visited
{}{
    color:#00f;
    background:inherit;
}

.TreeMenu img.s
{}{
    cursor:hand;
    vertical-align:middle;
}
.TreeMenu ul
{}{
    padding:0;
}
.TreeMenu li
{}{
    list-style:none;
    padding:0;
}
.Closed ul
{}{
    display:none;
}
.Child img.s
{}{
    background:none;
    cursor:default;
}

#CategoryTree ul
{}{
    margin:0 0 0 17px;
}
#CategoryTree img.s
{}{
    width:34px;
    height:18px;
}
#CategoryTree .Opened img.s
{}{
    background:url(skin3/opened.gif) no-repeat 0 1px;
}
#CategoryTree .Closed img.s
{}{
    background:url(skin3/closed.gif) no-repeat 0 1px;
}
#CategoryTree .Child img.s
{}{
    background:url(skin3/child.gif) no-repeat 13px 2px;
}

#CategoryTree
{}{
    float:left;
    width:249px;
    border:1px solid #99BEEF;
    background:#D2E4FC;
    color:inherit;
    margin:3px;
    padding:3px;
    height:600px;
}
4.建立一个类AjaxMethod
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;

/**//// <summary>
/// Summary description for AjaxMethod
/// </summary>
public class AjaxMethod
{}{
    public AjaxMethod()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    [AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
    public static DataSet GetSubCategory(int iCategoryID)
    {}{
        string sql = string.Format("SELECT CategoryID, CategoryName, FatherID, dbo.IsLeaf(CategoryID) as IsChild FROM Category WHERE FatherID = {0}", iCategoryID);
        return GetDataSet(sql);
    }

    [AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
    public static DataSet GetFormsList(int iCategoryID)
    {}{
        string sql = string.Format("SELECT * FROM forms WHERE form_category_id = {0}", iCategoryID);
        return GetDataSet(sql);
    }
public static string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"].ToString();

        public static DataSet GetDataSet(string sql)
        {}{
            SqlDataAdapter sda = new SqlDataAdapter(sql, ConnectionString);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            if (ds != null)
                return ds;
            else
                return null;
        }
}5.sql脚本
if exists (select * from dbo.sysobjects where id = object_id(N[dbo].[Category]) and OBJECTPROPERTY(id, NIsUserTable) = 1)
drop table [dbo].[Category]
GO

if exists (select * from dbo.sysobjects where id = object_id(N[dbo].[Forms]) and OBJECTPROPERTY(id, NIsUserTable) = 1)
drop table [dbo].[Forms]
GO

CREATE TABLE [dbo].[Category] (
    [CategoryID] [int] IDENTITY (1, 1) NOT NULL ,
    [CategoryName] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
    [FatherID] [int] NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Forms] (
    [FormID] [int] IDENTITY (1, 1) NOT NULL ,
    [FormName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [FormUrl] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [Form_category_id] [int] NULL ,
    [target] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE FUNCTION IsLeaf (@cat_id int) 
RETURNS int AS 
BEGIN

declare @count int
select @count = (select count(*) from Category where FatherID=@cat_id)
if (@count=0)
return 1
return 0

END


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·C#时间函数扩展-.NET教程,C#语言
·成人影片女明星jameson推出手机情色铃声服务
·窗体中控件自动随窗体变化大小-.NET教程,组件控件开发
·C#使用CDO发送邮件-.NET教程,C#语言
·浅析nat的类型
·我的字符和文件加密类,128位加密-.NET教程,评论及其它
·数字机顶盒技术综述 (一)
·数字多媒体广播平台及其关键技术
·java 线程安全-JSP教程,Java技巧及代码
·Remoting和Web服务的区别-.NET教程,Web Service开发
最新文章
·如何通过博客月赚几万美金?_网赚技巧
·浅析广告联盟与个人网站的盈利_网赚技巧
·google adsense会议的主要内容回顾_网赚技巧
·赵克:创业基本素质勤奋专业低调务实_站长访谈
·行业网站分析之一:行业网站现状与前景_站长心得
·分享:做网站月入7000的网站赚钱经验_站长心得
·首页所应该达到的几个目标(2)_站长心得
·google sandbox效应_google推广
·google旧瓶装新酒 用遗弃技术开发web新应用_google推广
·认识google优化排名_google推广
相关主题
  • ajax实现分页查询_ajax教程
  • ajax实现不刷屏的前提下实现页面定时刷新_ajax教程
  • ajax实现datagrid/datalist动态tooltip _ajax教程
  • ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中_ajax教程
  • ajax实现网易相册样式的修改_ajax教程
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭