1.建立一个aspx页面
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, N'IsUserTable') = 1)
drop table [dbo].[Category]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Forms]') and OBJECTPROPERTY(id, N'IsUserTable') = 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
6.
源代码下载