1)default.aspx前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ExtjsTreeDemo._Default" %>
2)GetTrees.ashx 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Configuration;
namespace ExtjsTreeDemo.ServletHandlers
{
///
/// $codebehindclassname$ 的摘要说明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class GetTrees : IHttpHandler
{
private string TreeDemoConnectionString = ConfigurationManager.ConnectionStrings["TreeDemoConnectionString"].ConnectionString;
DataClassesDataContext dc = null;
public void ProcessRequest(HttpContext context)
{
dc = new DataClassesDataContext(TreeDemoConnectionString);
string str = GetParentNodeString(0);
context.Response.ContentType = "text/plain";
context.Response.Write(str);
}
///
/// 获取父节点
///
///
private string GetParentNodeString(int id)
{
int counter= 1;
IQueryable nodes=dc.TreeNodes.Where(u=>u.parentId==id);
StringBuilder sb = new StringBuilder();
if (nodes != null && nodes.Count() != 0)
{
sb.Append("[");
foreach (TreeNodes node in nodes)
{
sb.Append("{");
sb.Append("id:'" + node.id + "'");
sb.Append(",text:'" + node.text + "'");
sb.Append(GetSubNodeString(node.id));
sb.Append("}");
if (counter < nodes.Count())
{
sb.Append(",");
}
counter++;
}
sb.Append("]");
}
return sb.ToString();
}
///
/// 获取子节点
///
///
///
private string GetSubNodeString(int id)
{
int counter = 1;
IQueryable nodes = dc.TreeNodes.Where(u => u.parentId == id);
StringBuilder sb = new StringBuilder();
if (nodes != null & nodes.Count() != 0)
{
sb.Append(",children: [");
foreach (TreeNodes node in nodes)
{
sb.Append("{");
sb.Append("id:'" + node.id + "'");
sb.Append(",text:'" + node.text + "'");
sb.Append(GetSubNodeString(node.id));
sb.Append("}");
if (counter < nodes.Count())
{
sb.Append(",");
}
counter++;
}
sb.Append("]");
}
else
{
sb.Append(",leaf: true");
//sb.Append(",href:'http://www.baidu.com?id="+id+"'");
//sb.Append(",hrefTarget:'_blank'");
}
return sb.ToString();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
3)TreeHandler.ashx代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.Linq;
namespace ExtjsTreeDemo.ServletHandlers
{
///
/// $codebehindclassname$ 的摘要说明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TreeHandler : IHttpHandler
{
private string TreeDemoConnectionString = ConfigurationManager.ConnectionStrings["TreeDemoConnectionString"].ConnectionString;
DataClassesDataContext dc = null;
public void ProcessRequest(HttpContext context)
{
dc = new DataClassesDataContext(TreeDemoConnectionString);
string type = "";
int parentId = 0;
string text = "";
if (context.Request.QueryString["type"] != null)
{
type = context.Request.QueryString["type"].ToString();
}
if (context.Request.QueryString["parentId"] != null)
{
string strId = context.Request.QueryString["parentId"].ToString();
parentId = Convert.ToInt32(strId);
}
if (context.Request.QueryString["text"] != null)
{
text = context.Request.QueryString["text"].ToString();
}
if (type == "add")
{
TreeNodes node = new TreeNodes();
node.text = text;
node.parentId = parentId;
bool flag = AddNode(node);
}
else if (type == "del")
{
TreeNodes node = GetNode(parentId);
if (node != null)
{
bool flag = DelNode(node);
DelNodesEvent(parentId);
}
}
else
{
TreeNodes node = GetNode(parentId);
bool flag = EditNode(node, text);
}
context.Response.ContentType = "text/plain";
context.Response.Write("response");
}
///
/// 递归删除所有的字节点
///
///
private void DelNodesEvent(int parentId)
{
IQueryable nodes = GetNodes(parentId);
if (nodes != null && nodes.Count() != 0)
{
foreach (TreeNodes node in nodes)
{
DelNodesEvent(node.id);
}
DelNodes(nodes);
}
}
///
/// 根据id获取节点
///
///
///
private TreeNodes GetNode(int id)
{
TreeNodes node = null;
node=dc.TreeNodes.SingleOrDefault(u => u.id == id);
return node;
}
///
/// 根据parentId删除多个节点
///
///
///
private IQueryable GetNodes(int parentId)
{
IQueryable nodes = null;
nodes = dc.TreeNodes.Where(u => u.parentId == parentId);
return nodes;
}
///
/// 添加节点
///
///
///
private bool AddNode(TreeNodes node)
{
bool flag = false;
try
{
dc.TreeNodes.InsertOnSubmit(node);
dc.SubmitChanges();
flag = true;
}
catch (Exception ex)
{
flag = false;
}
return flag;
}
///
/// 删除节点
///
///
///
private bool DelNode(TreeNodes node)
{
bool flag = false;
try
{
dc.TreeNodes.DeleteOnSubmit(node);
dc.SubmitChanges();
}
catch (Exception ex)
{
flag = false;
}
return flag;
}
///
/// 删除多个节点
///
///
///
private bool DelNodes(IQueryable nodes)
{
bool flag = false;
try
{
dc.TreeNodes.DeleteAllOnSubmit(nodes);
dc.SubmitChanges();
}
catch (Exception ex)
{
flag = false;
}
return flag;
}
///
/// 修改
///
///
///
///
private bool EditNode(TreeNodes node, string text)
{
bool flag = false;
node.text = text;
try
{
dc.SubmitChanges();
flag = true;
}
catch (Exception ex)
{
flag = false;
}
return flag;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}