模仿CSDN的左侧AJAX菜单,嘿嘿,大家看看

jiej007 2007-08-22 01:21:51
http://www.ddyuanlin.com/test/tree.aspx
...全文
1241 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
fffff_1982 2008-11-09
  • 打赏
  • 举报
回复
学习,研究一下
mengxj85 2008-11-08
  • 打赏
  • 举报
回复
顶,偶也希望楼主能改进成具有记忆功能的树,刷新后对树没有影响
liuph3000 2008-11-07
  • 打赏
  • 举报
回复
mark
Jack123 2008-10-31
  • 打赏
  • 举报
回复
up
yangtzeu 2008-01-10
  • 打赏
  • 举报
回复
up
qlc263 2008-01-04
  • 打赏
  • 举报
回复
这个不错,,,俺已经用了,,不过如果加上节点展开记忆的就更好了。。。刷新页面就闭合已展开的
No_Data_Found 2007-10-09
  • 打赏
  • 举报
回复
有错误

节点 收缩展开状态判断有误

另外 Loading Tree …… 有的时候没去掉
michaeljjf 2007-10-09
  • 打赏
  • 举报
回复
不错,学习
twinkleliang 2007-10-09
  • 打赏
  • 举报
回复
还是跟这个类似树一样的问题,鼠标没放在树叶,而是放在后面的空白上,一直都显示手形。
一帆 2007-10-09
  • 打赏
  • 举报
回复
up
yjwang0621 2007-10-09
  • 打赏
  • 举报
回复
mark
lyae 2007-10-03
  • 打赏
  • 举报
回复
不错呀
jiej007 2007-09-23
  • 打赏
  • 举报
回复
客户端代码查看源文件就全部错来了,不用我说了吧
jiej007 2007-09-23
  • 打赏
  • 举报
回复
嘿嘿,大家都要求源代码,偶当然要给出来的了,很简单的了
说明一下:
Function是我自己些的类,IsNumeric方法就是判断是否数字而已,这个就大家自己完成了;
SQL类是处理数据库的类,这里就用到一个返回DataTable的方法,就是直接根据SQL的查询语句返回记录集了。
protected void Page_Load(object sender, EventArgs e)
{
Function Fun = new Function();
if (Fun.IsNumeric(Request["ID"]))
GetTreeData(Request["ID"]);
}
protected void GetTreeData(string ID)
{
SQL DB = new SQL();
Function Fun = new Function();
string MenuName, MenuType, ListType, onClick;
DataTable Dt = DB.GetDataTable("SELECT [id],[d_name],[d_nextid] FROM [d_photo_cls] WHERE [d_parid]=" + ID + " ORDER BY [d_allorder] ASC");
Response.Clear();
for (int i = 0; i < Dt.Rows.Count; i++)
{
if (Dt.Rows[i][0].ToString() == Dt.Rows[i][2].ToString())
{//没有子类
ListType = onClick = "";
if (i + 1 == Dt.Rows.Count)
MenuType = "file_end";//最后一个
else
MenuType = "file";
}
else
{//有子类
if (i + 1 == Dt.Rows.Count)
{
MenuType = "folder_close_end";
ListType = "list1";
onClick = " onclick=\"GetData(" + Dt.Rows[i][0].ToString() + ");\"";
}
else
{
MenuType = "folder_close";
ListType = "list0";
onClick = " onclick=\"GetData(" + Dt.Rows[i][0].ToString() + ");\"";
}
}
MenuName = "<a href=\"#" + Dt.Rows[i][0].ToString() + "\" id=\"A_" + Dt.Rows[i][0].ToString() + "\" onclick=\"ShowMe(" + Dt.Rows[i][0].ToString() + ")\" class=\"text0\" title=\"" + Dt.Rows[i][1].ToString() + "\">" + Dt.Rows[i][1].ToString() + "</a>";
Response.Write("<div id=\"M1_" + Dt.Rows[i][0].ToString() + "\" style=\"display:block;\" class=\"" + MenuType + "\"" + onClick + " nowrap>" + MenuName + "</div>");
Response.Write("<div id=\"M2_" + Dt.Rows[i][0].ToString() + "\" style=\"display:none;\" class=\"" + ListType + "\" nowrap></div>");
}
Dt.Dispose();
Response.End();
}
Harvey_zhang 2007-09-20
  • 打赏
  • 举报
回复
zhanggbao@yahoo.com.cn,谢谢
netharry 2007-09-20
  • 打赏
  • 举报
回复
不错
slaim1127 2007-09-20
  • 打赏
  • 举报
回复
是用ajax+javascript么?
cqnucsmoon 2007-09-19
  • 打赏
  • 举报
回复
弓虽!
alphayycn 2007-09-19
  • 打赏
  • 举报
回复
4.生成菜单文件 (LeftTreeAjax.aspx文件调用该文件)

CreateTreeAjax.aspx //前台页面中只有这一句
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateTreeAjax.aspx.cs" Inherits="Manage_BaseData_CreateTreeAjax" %>

CreateTreeAjax.aspx.cs 代码如下

// (c) Copyright XXXXXXX Corporation.
// 无级别限制Aajx树型菜单
// See http://www.ddyuanlin.com/test/tree.aspx
// Email:alphacn@126.com
// 2007.9.19 Alpha

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 System.Data.SqlClient;
using System.Text;

/// <summary>
/// 生成当前结点子树
/// </summary>
public partial class Manage_BaseData_CreateTreeAjax : System.Web.UI.Page
{
//数据库连接字符串
private const string CONNSTRING = "server=.;database=PubData;uid=sa;pwd=123456";
//当前结点代码
private string BaseDataCode = "";
//Sql代码
private string Sqlstr = "";
//样式名称
private string ClassName="";
//孙子结点数目
private int SubCount;
//子结点数目
private int PrantCount;
//DataSet对象
private DataSet Ds;
//子结点代码
private string BaseCode = "";
//子结点名称
private string BaseName = "";
//Javascript事件名
private string JsAction="";
//样式表名称
private string IfShowClass = "";
//子符串变量
StringBuilder Sb;

/// <summary>
/// 页面初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
//获得当前结点代码
BaseDataCode = Request.QueryString["ID"];
if (BaseDataCode != "" || BaseDataCode != "")
{
Sqlstr = "SELECT BaseDataCode, BaseDataName, LevelType,ParentCode FROM SysBaseData WHERE (ParentCode = '" + BaseDataCode + "') AND (VaildType = '0') ORDER BY SerialNumberID";
using (SqlConnection Conn = new SqlConnection(CONNSTRING))
{
SqlDataAdapter Da = new SqlDataAdapter(Sqlstr, Conn);
Conn.Open();

Ds = new DataSet();
Da.Fill(Ds,"Dt");

Sb = new StringBuilder();

//当前结点的子结点数量
PrantCount = Ds.Tables["Dt"].Rows.Count;

//循环读出子点结点
for (int i = 0; i < PrantCount;i++ )
{
//子结点代码
BaseCode = Ds.Tables["Dt"].Rows[i][0].ToString().Trim();
//子结点名称
BaseName = Ds.Tables["Dt"].Rows[i][1].ToString().Trim();

//判断该结点下,是否还有子结点
Sqlstr = "SELECT count(BaseDataCode) from SysBaseData where ParentCode='" + BaseCode + "'";
SqlCommand cmd2 = new SqlCommand(Sqlstr, Conn);
//当前结点子结点的子结点数量
SubCount = int.Parse(cmd2.ExecuteScalar().ToString());

//是否有孙子结点
if (SubCount > 0)
{
//最后一个结点
if (i == PrantCount - 1)
{
ClassName = "folder_close_end";
IfShowClass = "";
}
else
{
ClassName = "folder_close";
IfShowClass = "class='list0'";
}
JsAction ="onclick=\"GetData('" + BaseCode + "')\"";
}
else
{
//最后一个结点
if (i == PrantCount - 1)
{
ClassName = "file_end";
IfShowClass = "";
}
else
{
ClassName = "file";
IfShowClass = "class='list0'";
}
JsAction = "";
}
//生成子树代码
Sb.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
Sb.Append("<tr><td><div id='M1_" + BaseCode + "' class='" + ClassName + "'" + JsAction + " alt='" + BaseName + "'><a href='#" + BaseCode + "'' >" + BaseName + "</a></div>");
Sb.Append("<div id='M2_" + BaseCode + "' style='display:none' " + IfShowClass + "></div></td></tr>");
Sb.Append("</table>");
}
//输出
Response.Write(Sb.ToString());
}
}
else
{
Response.Write("没有数据");
}
}
}
alphayycn 2007-09-19
  • 打赏
  • 举报
回复
3.菜单显示文件 (文件:LeftTreeAjax.aspx,LeftTreeAjax.aspx.cs没有代码)
LeftTreeAjax.aspx 代码如下:

<!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>
<title>AJAX MenuTree</title>

<style type="text/css">
body { line-height:20px; font-size:12px; background:#EFEFEF;}

.load { padding:2px 2px 0px 2px; line-height:18px; display:inline; color:#FFF; background:#69C;}

.text0 { padding:2px 0px 0px 2px; line-height:18px; display:block; color:#069; text-decoration:none;}
.text0:hover { color:#000; background:#CCC;}
.text1 { padding:2px 0px 0px 2px; line-height:18px; display:block; color:#FFF; text-decoration:none; background:#69C;}

a:link{padding 2px 0px 0px 2px;line-height:18px;display:block;color:#069;text-decoration:none;}
a:visited{padding 2px 0px 0px 2px;line-height:18px;display:block;color:#069;text-decoration:none;bgckground:#CCC}
a:hover{padding 2px 0px 0px 2px;line-height:18px;display:block;color:#069;text-decoration:none;background:#69C;}

.folder_close { padding-left:38px; background-image:url(images/P1.gif); background-repeat:no-repeat; height:20px; cursor:pointer;}
.folder_open { padding-left:38px; background-image:url(images/M1.gif); background-repeat:no-repeat; height:20px; cursor:pointer;}
.folder_close_end { padding-left:38px; background-image:url(images/P2.gif); background-repeat:no-repeat; height:20px; cursor:pointer;}
.folder_open_end { padding-left:38px; background-image:url(images/M2.gif); background-repeat:no-repeat; height:20px; cursor:pointer;}

.file { padding-left: 38px; background-image: url(images/L1.gif); background-repeat: no-repeat; height: 20px; cursor: pointer;}
.file_end { padding-left: 38px; background-image: url(images/L2.gif); background-repeat: no-repeat; height: 20px; cursor: pointer;}

.list0 { padding-left: 20px; background-image: url(images/L4.gif); background-repeat: repeat-y;}
.list1 { padding-left: 20px;}
</style>

<script language="javascript" type="text/javascript">
// <!CDATA[
var xmlHttp;
var ShowID;
var ParID;
//创建对象
function createXMLHttpRequest()
{
// if (window.ActiveXObject){
// xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
// }
//else if (window.XMLHttpRequest){
// xmlHttp = new XMLHttpRequest();
//else{
// //alert("XML对象创建失败");
//}

var isRight = true;
try{ xmlHttp = new XMLHttpRequest(); }
catch(trymicrosoft)
{
try{xmlHttp = new ActiveXobject("Msxml2.XMLHTTP");}
catch(othermicrosoft)
{
try{xmlHttp = new ActiveXobject("Microsoft.XMLHTTP");}
catch(failed)
{
isRight = false;
}
}
}
if(!isRight)
alert("XML对象创建失败");
}

//获取数据
function GetData(ID)
{
ParID = document.getElementById("M1_" + ID);
ShowID = document.getElementById("M2_" + ID);
if(ShowID.innerHTML != "")
{
if(ShowID.style.display == "none")
ShowID.style.display = "block";
else
ShowID.style.display = "none";
ShowFolderICO();
return;
}

ShowID.style.display = "block";
ShowID.innerHTML = "<span class=\"load\">Loading Tree ……</span>";
var Url = "CreateTreeAjax.aspx?ID=" + ID;
createXMLHttpRequest();
xmlHttp.onreadystatechange = ShowTree;
xmlHttp.open("GET", Url, true);
xmlHttp.send(null);
}

//此方法直接从服务器端返回HTML
function ShowTree()
{
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200)
{
ShowID.innerHTML = xmlHttp.responseText;
// alert(xmlHttp.responseText);
ShowFolderICO();
//alert(M1_DCCK.innerHTML);
}else
{
ShowID.innerHTML = "数据获取错误!"+xmlHttp.status;
}
}
}

//改变文件夹图标打开/关闭状态
function ShowFolderICO()
{
if (ParID != null){
if (ParID.className == "folder_close")
ParID.className = "folder_open";
else if (ParID.className == "folder_open")
ParID.className = "folder_close";
else if (ParID.className == "folder_close_end")
ParID.className = "folder_open_end";
else if (ParID.className == "folder_open_end")
ParID.className = "folder_close_end";
}
}
// ]]>
</script>
</head>

<body onload="GetData(0)">
<input type="hidden" id="MeID" value="" />
<div><img src="images/root.gif" alt="RootNote" /></div>
<div id="M2_0" style="display:block;"></div>

</body>
</html>
加载更多回复(11)

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧