AjaxPro.NET框架生成高效率的Tree(Asp.net 2.0)(示例代码下载)

(一). 说明
        用Tree显示菜单及物品列表(从服务端获取数据)比较方便, 当前显示Tree 主要有两种方式:
        1. 在Tree初始化时将数据全部一次性从服务端获取, 获取完数据后页面展开或收缩时就不再需要获取数据,这样, 获取完数据使用时效率比较高, 但当树节点很多时, 在每次初始化时会有较大的延迟.
        2. 初始化时只加载展开的节点, 当用户需要查看某个节点下的数据时, 再去取数据, 这样, 初始化时延迟会相对减少, 但每次单击节点时要获取数据, 页面每次都要刷新, 所以也会产生延迟.
        此事例用Ajax实现第二种方式, 每次只动态加载要展开的节点数据(闭合节点不展开时,则不获取其子节点的数据),  另外加载节点时页面不会刷新.

(二). 运行示例图
AjaxTree1.jpgAjaxTree2.jpg

(三). AjaxPro.NET简介

         首先对AjaxPro.NET作一下介绍, AjaxPro.NET是一个优秀的Ajax框架, 在实际应用中只要添加其DLL引用并进行简单的配置, 即可以非常方便的在客户端直接调用服务端方法, 来获取Tree节点.

(四).使用AjaxPro.NET预配置

       1. 添加 AjaxPro.dll 文件的引用(示例代码中已经包含,直接COPY过来使用即可).
       2. 在Web.config文件中添加以下配置.

None.gif < httpHandlers >
None.gif            
< add verb = " POST,GET "  path = " ajaxpro/*.ashx "  type = " AjaxPro.AjaxHandlerFactory, AjaxPro "   />
None.gif
</ httpHandlers >
None.gif

      3. 在要使用AjaxPro.NET框架的页面 *.aspx.cs 的 Page_Load事件中加如下代码:

None.gif AjaxPro.Utility.RegisterTypeForAjax( typeof (_Default));

      4. 经过以上三步骤后, 只要在后台服务端的方法前面增加属性[AjaxMethod]后:

None.gif    [AjaxMethod()]     //  or [AjaxPro.AjaxMethod] 
None.gif
   public  ArrayList GetSearchItems(  string  strQuery )
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif       
//生成数据源
InBlock.gif
       ArrayList items = new ArrayList();
InBlock.gif       items.Add(
"King");
InBlock.gif       items.Add(
"Rose");
InBlock.gif       
return items ;
ExpandedBlockEnd.gif  }
 
None.gif

         就可以在客户端直接使用服务端方法, 非常方便, 客户端调用后台代码如下:

None.gif var returnValue  =  后台代码类名.GetSearchItems(参数);

(五). 代码

         1. 页面 Tree.aspx 代码:

None.gif <% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Tree.aspx.cs "  Inherits = " _Default "   %>
None.gif 
None.gif 
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
None.gif 
None.gif 
< html xmlns = " http://www.w3.org/1999/xhtml "   >
None.gif 
< head runat = " server " >
None.gif     
< title > Ajax Efficient Tree </ title >
None.gif     
< link type = " text/css "  href = " css/tree.css "  rel = " stylesheet " >
None.gif 
</ head >
None.gif 
< body >
None.gif     
< form id = " form1 "  runat = " server " >
None.gif     
< div >
None.gif         
< asp:Panel ID = " Panel1 "  runat = " server "  Height = " 424px "  Width = " 251px " >
None.gif             
< div id = " CategoryTree "   class = " TreeMenu " ></ div >
None.gif         
</ asp:Panel >
None.gif         
< script language = " jscript " >
None.gif             var tree 
=  document.getElementById( " CategoryTree " );
None.gif             var root 
=  document.createElement( " li " );
None.gif             root.id 
=   " li_0 " ;
None.gif             tree.appendChild( root );
None.gif             ExpandSubCategory( 
0  );
None.gif             function ExpandSubCategory( categoryID )
ExpandedBlockStart.gifContractedBlock.gif             
dot.gif {
InBlock.gif                 var liFather 
= document.getElementById( "li_" + categoryID );
InBlock.gif                 
if( liFather.getElementsByTagName("li").length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                 
dot.gif{
InBlock.gif                     ChangeStatus( categoryID );
InBlock.gif                     
return;
ExpandedSubBlockEnd.gif                 }

InBlock.gif                 liFather.className 
= "Opened";
InBlock.gif                 SwitchNode( categoryID, 
true );
InBlock.gif                 
InBlock.gif                 
//仅获取当前节点的子Nodes
InBlock.gif
                 _Default.GetSubCategory( categoryID, GetSubCategory_callback );
ExpandedBlockEnd.gif             }
            
None.gif             function SwitchNode( CategoryID, show )
ExpandedBlockStart.gifContractedBlock.gif             
dot.gif {
InBlock.gif                 var li_father 
= document.getElementById("li_" + CategoryID);
InBlock.gif                 
if( show )
ExpandedSubBlockStart.gifContractedSubBlock.gif                 
dot.gif{
InBlock.gif                     var ul 
= document.createElement("ul");
InBlock.gif                     ul.id 
= "ul_note_" + CategoryID;
InBlock.gif                     
InBlock.gif                     var note 
= document.createElement("li");
InBlock.gif                     note.className 
= "Child";              
InBlock.gif                     
InBlock.gif                     var img 
= document.createElement("img");
InBlock.gif                     img.className 
= "s";
InBlock.gif                     img.src 
= "css/s.gif";                    
InBlock.gif                     
InBlock.gif                     var a 
= document.createElement("a");
InBlock.gif                     a.href 
= "javascript:void(0);";
InBlock.gif                     a.innerHTML 
= "Please waiting";
InBlock.gif                     
InBlock.gif                     note.appendChild(img);
InBlock.gif                     note.appendChild(a);
InBlock.gif                     ul.appendChild(note);
InBlock.gif                     li_father.appendChild(ul);                                        
ExpandedSubBlockEnd.gif                 }
   
InBlock.gif                 
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                 
dot.gif{
InBlock.gif                     var ul 
= document.getElementById("ul_note_" + CategoryID );
InBlock.gif                     
if( ul )
ExpandedSubBlockStart.gifContractedSubBlock.gif                     
dot.gif{
InBlock.gif                         li_father.removeChild(ul);
ExpandedSubBlockEnd.gif                     }

ExpandedSubBlockEnd.gif                 }
             
ExpandedBlockEnd.gif             }

None.gif             function GetSubCategory_callback( response )
ExpandedBlockStart.gifContractedBlock.gif             
dot.gif {
InBlock.gif                var dt 
= response.value.Tables[0];
InBlock.gif                
if( dt.Rows.length > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                     var iCategoryID 
= dt.Rows[0].FatherID;               
ExpandedSubBlockEnd.gif                }
                                
InBlock.gif                var li_father 
= document.getElementById("li_" + iCategoryID );
InBlock.gif                var ul 
= document.createElement("ul");
InBlock.gif                
for( var i = 0; i < dt.Rows.length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                     
if( dt.Rows[i].IsChild == 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                     
dot.gif{
InBlock.gif                         var li 
= document.createElement("li");
InBlock.gif                         li.className 
= "Child";
InBlock.gif                         li.id 
= "li_" + dt.Rows[i].CategoryID;
InBlock.gif                         var img 
= document.createElement("img");
InBlock.gif                         img.id 
= dt.Rows[i].CategoryID;
InBlock.gif                         img.className 
= "s";
InBlock.gif                         img.src 
= "css/s.gif";
InBlock.gif                         var a 
= document.createElement("a");
InBlock.gif                         a.href 
= "javascript:OpenDocument('" + dt.Rows[i].CategoryID + "');";
InBlock.gif                         a.innerHTML 
= dt.Rows[i].CategoryName;                                          
ExpandedSubBlockEnd.gif                     }

InBlock.gif                     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                     
dot.gif{
InBlock.gif                         var li 
= document.createElement("li");
InBlock.gif                         li.className 
= "Closed";
InBlock.gif                         li.id 
= "li_" + dt.Rows[i].CategoryID;
InBlock.gif                         var img 
= document.createElement("img");
InBlock.gif                         img.id 
= dt.Rows[i].CategoryID;
InBlock.gif                         img.className 
= "s";
InBlock.gif                         img.src 
= "css/s.gif";
ExpandedSubBlockStart.gifContractedSubBlock.gif                         img.onclick 
= function()dot.gif{ ExpandSubCategory( this.id ); };
InBlock.gif                         img.alt 
= "Expand/collapse";
InBlock.gif                         var a 
= document.createElement("a");
InBlock.gif                         a.href 
= "javascript:ExpandSubCategory('" + dt.Rows[i].CategoryID + "');";
InBlock.gif                         a.innerHTML 
= dt.Rows[i].CategoryName;                                         
ExpandedSubBlockEnd.gif                     }

InBlock.gif                     li.appendChild(img);
InBlock.gif                     li.appendChild(a);
InBlock.gif                     ul.appendChild(li);
ExpandedSubBlockEnd.gif                }

InBlock.gif                li_father.appendChild(ul);
InBlock.gif                SwitchNode( iCategoryID, 
false );
ExpandedBlockEnd.gif             }
          
None.gif             
None.gif             
// 单击叶节点时, 异步从服务端获取单个节点的数据.
None.gif
             function OpenDocument( CategoryID )
ExpandedBlockStart.gifContractedBlock.gif             
dot.gif {                
InBlock.gif                 _Default.GetNameByCategoryID( CategoryID, GetNameByCategoryID_callback );
ExpandedBlockEnd.gif             }

None.gif             
None.gif             function GetNameByCategoryID_callback( response )
ExpandedBlockStart.gifContractedBlock.gif             
dot.gif {
InBlock.gif                 alert( response.value );
ExpandedBlockEnd.gif             }

None.gif             
None.gif             function ChangeStatus( CategoryID )
ExpandedBlockStart.gifContractedBlock.gif             
dot.gif {
InBlock.gif                 var li_father 
= document.getElementById("li_" + CategoryID );
InBlock.gif                 
if( li_father.className == "Closed" )
ExpandedSubBlockStart.gifContractedSubBlock.gif                 
dot.gif{
InBlock.gif                     li_father.className 
= "Opened";
ExpandedSubBlockEnd.gif                 }

InBlock.gif                 
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                 
dot.gif{
InBlock.gif                     li_father.className 
= "Closed";
ExpandedSubBlockEnd.gif                 }

ExpandedBlockEnd.gif            }
              
None.gif         
</ script >           
None.gif     
</ div >
None.gif     
</ form >     
None.gif 
</ body >       
None.gif 
</ html >
None.gif
None.gif

         2. 页面后台文件 Tree.aspx.cs 代码: 

None.gif using  System;
None.gif 
using  System.Data;
None.gif 
using  System.Configuration;
None.gif 
using  System.Web;
None.gif 
using  System.Web.Security;
None.gif 
using  System.Web.UI;
None.gif 
using  System.Web.UI.WebControls;
None.gif 
using  System.Web.UI.WebControls.WebParts;
None.gif 
using  System.Web.UI.HtmlControls;
None.gif 
None.gif 
public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif    
//此对象用于存放所有的节点数
InBlock.gif
    public static DataSet dsAllNodes = new DataSet();
InBlock.gif 
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        AjaxPro.Utility.RegisterTypeForAjax(
typeof(_Default));       
InBlock.gif        CreateNodes();
ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif    
private DataTable CreateStructure()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       DataTable dt 
= new DataTable();
InBlock.gif       dt.Columns.Add(
new DataColumn("CategoryID"typeof(int)));
InBlock.gif       dt.Columns.Add(
new DataColumn("CategoryName"typeof(string)));
InBlock.gif       dt.Columns.Add(
new DataColumn("FatherID"typeof(string)));
InBlock.gif       dt.Columns.Add(
new DataColumn("IsChild"typeof(bool)));
InBlock.gif       
return dt;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public void CreateNodes()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       DataTable dt 
= this.CreateStructure();
InBlock.gif 
InBlock.gif       DataRow drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 1;
InBlock.gif       drNew[
"CategoryName"= "物品类别";
InBlock.gif       drNew[
"FatherID"= 0;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 2;
InBlock.gif       drNew[
"CategoryName"= "水果";
InBlock.gif       drNew[
"FatherID"= 1;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 3;
InBlock.gif       drNew[
"CategoryName"= "工具";
InBlock.gif       drNew[
"FatherID"= 1;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 4;
InBlock.gif       drNew[
"CategoryName"= "萍果";
InBlock.gif       drNew[
"FatherID"= 2;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 5;
InBlock.gif       drNew[
"CategoryName"= "香蕉";
InBlock.gif       drNew[
"FatherID"= 2;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 6;
InBlock.gif       drNew[
"CategoryName"= "桔子";
InBlock.gif       drNew[
"FatherID"= 2;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 7;
InBlock.gif       drNew[
"CategoryName"= "萝卜";
InBlock.gif       drNew[
"FatherID"= 2;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 8;
InBlock.gif       drNew[
"CategoryName"= "钢笔";
InBlock.gif       drNew[
"FatherID"= 3;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 9;
InBlock.gif       drNew[
"CategoryName"= "铅笔";
InBlock.gif       drNew[
"FatherID"= 3;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 10;
InBlock.gif       drNew[
"CategoryName"= "尺子";
InBlock.gif       drNew[
"FatherID"= 3;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif       
InBlock.gif       drNew 
= dt.NewRow();
InBlock.gif       drNew[
"CategoryID"= 11;
InBlock.gif       drNew[
"CategoryName"= "橡皮";
InBlock.gif       drNew[
"FatherID"= 3;
InBlock.gif       dt.Rows.Add( drNew );
InBlock.gif 
InBlock.gif       dsAllNodes.Tables.Add(dt);
ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif    [AjaxPro.AjaxMethod]
InBlock.gif    
public DataSet GetSubCategory(int CategoryID)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       DataSet ds 
= new DataSet();
InBlock.gif       DataTable dt 
= this.CreateStructure();
InBlock.gif       DataRow[] drSelect 
= dsAllNodes.Tables[0].Select("FatherID=" + CategoryID.ToString());
InBlock.gif       
foreach (DataRow drTemp in drSelect)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif          DataRow dr 
= dt.NewRow();
InBlock.gif          dr[
"CategoryID"= drTemp["CategoryID"];
InBlock.gif          dr[
"CategoryName"= drTemp["CategoryName"];
InBlock.gif          dr[
"FatherID"= drTemp["FatherID"];
InBlock.gif          dr[
"IsChild"= IsLeaf( int.Parse( drTemp["CategoryID"].ToString() ) );
InBlock.gif          dt.Rows.Add(dr);
ExpandedSubBlockEnd.gif       }

InBlock.gif       ds.Tables.Add(dt);
InBlock.gif       
return ds;
ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif    [AjaxPro.AjaxMethod]
InBlock.gif    
public bool IsLeaf(int Category)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
foreach(DataRow dr in dsAllNodes.Tables[0].Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif           
if (dr["FatherID"!= null && int.Parse(dr["FatherID"].ToString()) == Category)
ExpandedSubBlockStart.gifContractedSubBlock.gif           
dot.gif{
InBlock.gif              
return false;  
ExpandedSubBlockEnd.gif           }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
return true;
ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif    [AjaxPro.AjaxMethod]
InBlock.gif    
public string GetNameByCategoryID(string CategoryID )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       
foreach( DataRow dr in dsAllNodes.Tables[0].Rows )
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif          
if( dr["CategoryID"].ToString() == CategoryID.ToString() )
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif             
return dr["CategoryName"].ToString();
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif       }

InBlock.gif       
return "";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif }

(六). 示例代码下载:
        http://www.cnitblog.com/Files/ChengKing/AjaxPro.net_EfficientTree.rar

转载于:https://www.cnblogs.com/SUNBOY/archive/2007/02/12/648004.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值