公司项目要用到treeview,而我却没用过,实在着急棘手,望各位多指教指教!
解决方案 »
- 请问如果在上传视频(flv文件)的同时创建该视频的缩略图?
- asp.net怎样实现批量下载文件(非打包形式下载)
- 创意互动:求很黄很暴力的代码
- 控件的ID
- <script src="ly.aspx?uu=20081118172926" 急............
- 关于Attributes.Add的困惑~
- Asp.Net 2.0树形控件导航问题?
- ExecuteScalar()执行之后是不是自动释放OleDbCommand关闭数据库?
- 问一个堪称经典的白痴问题: 如何将 .aspx 文件加载到 .Net框架中
- URL問題
- 【求助】怎样给treeview 每个节点(包括父节点和所有子结点)加上checkbox控件
- 为什么报ORA-01036的错误阿?
protected void tv_depart_populate(object sender, TreeNodeEventArgs e)
{
if (e.Node.ChildNodes.Count == 0)
{
String sql = "";
switch (e.Node.Depth)
{
case 0:
sql = "select DepartId,DepartName from aspnet_Department where PareDepart='工厂名字'";
break;
default:
sql = "select DepartId,DepartName from aspnet_Department where PareDepart='" + e.Node.Value.ToString()+"'";
break;
}
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sqldata = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
conn.Open();
sqldata.Fill(ds, "depart");
conn.Close();
foreach (DataRow row in ds.Tables["depart"].Rows)
{
TreeNode treeNode = new TreeNode(row["DepartName"].ToString(), row["DepartName"].ToString());
treeNode.NavigateUrl = "EditDepart.aspx?id=" + row["DepartId"].ToString();
if (Is_have_childNode(treeNode))
{
treeNode.PopulateOnDemand = true;
treeNode.SelectAction = TreeNodeSelectAction.Select;
}
else
{
treeNode.PopulateOnDemand = false;
treeNode.SelectAction = TreeNodeSelectAction.Expand;
}
e.Node.ChildNodes.Add(treeNode);
}
}
catch(Exception ee)
{ Response.Write(ee.Message); }
}
}
private bool Is_have_childNode(TreeNode node)
{
bool flag = false;
String sql = "select DepartId from aspnet_Department where PareDepart='" + node.Value.ToString() + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sqldata = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
conn.Open();
sqldata.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
flag = true;
}
}
catch
{ }
return flag;
}
{
if (!IsPostBack)
{
TreeViewBind(TreeView1);
}
} private void TreeViewBind(TreeView TreeView1)
{
DataTable myDataTable = Manager.Menu();//读取菜单数据 InitTree(TreeView1.Nodes, "0", myDataTable);
} private void InitTree(TreeNodeCollection Nds, string parentID, DataTable dt)
{
TreeNode tmpNd;
DataRow[] rows = dt.Select("MenuParentID='" + parentID + "'", "MenuOrder"); //使用foreach语句 来判断递归结束的条件
foreach (DataRow row in rows)
{
tmpNd = new TreeNode();
tmpNd.Value = row["MenuID"].ToString();
tmpNd.Text = row["MenuName"].ToString();
tmpNd.NavigateUrl = row["MenuUrl"].ToString();
tmpNd.ImageUrl = row["MenuImageUrl"].ToString();
tmpNd.SelectAction = TreeNodeSelectAction.Expand;//点击节点触发的事件
SetNodeText(tmpNd);
Nds.Add(tmpNd); InitTree(tmpNd.ChildNodes, tmpNd.Value, dt);
}
} /// <summary>
/// 把节点设置为客户端选中状态
/// </summary>
private void SetNodeText(TreeNode node)
{
string id = "spnode_" + node.Value;
node.Text = "<span style='cursor:hand' id='" + id + "' onclick=\"SetNode('" + id + "')\">" + node.Text + "</span>";
}
protected void btnExpand_Click(object sender, EventArgs e)
{
TreeView1.ExpandAll();
}
protected void btnCollapse_Click(object sender, EventArgs e)
{
TreeView1.CollapseAll();
}表
MenuID MenuName MenuParentID
1 第一级 0
2 第二级 1
3 第二级 1
读出来后会是:
第一级
第二级
第二级
<head id="Head1" runat="server">
<title>系统导航菜单</title>
<script language="javascript" type="text/javascript">
var lastnode = "";
function SetNode(node)
{
document.getElementById(node).style.backgroundColor='#DBDBDB';
if(lastnode!="" && lastnode!=node)
{
document.getElementById(lastnode).style.backgroundColor='#FFFFFF';
}
lastnode=node;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css">
<!--
body {
background-color: #F5F7F7;
}
-->
</style></head>
<body style="margin:0px;font-size:12px">
<form id="form1" runat="server">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="20">
<asp:LinkButton ID="btnExpand" runat="server" OnClick="btnExpand_Click">展开菜单</asp:LinkButton>
<asp:LinkButton ID="btnCollapse" runat="server" OnClick="btnCollapse_Click">收缩菜单</asp:LinkButton></td>
</tr>
</table>
<asp:TreeView ID="TreeView1" runat="server" ShowLines="True" ShowExpandCollapse="true" Width="100%" Height="100%" Target="mainFrame" ExpandDepth="0">
<NodeStyle ForeColor="Black"></NodeStyle>
<HoverNodeStyle Font-Underline="True" ForeColor="Black"></HoverNodeStyle>
<SelectedNodeStyle BackColor="#DBDBDB" ForeColor="Black"></SelectedNodeStyle>
</asp:TreeView>
</form>
</body>
</html>
字段 :
pathID int ;
pathName varchar(200);
title varchar(20);
pathNode varchar(500);
parentNode varchar(500);把要绑定的功能插入数据库 根目录的parentNode为0aspx.cs利用迭代
using System;
using System.Data;
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 System.Data.Common;
using System.Data.SqlClient;
using System.Collections;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TreeNode parentNode = null;
BindData("1",parentNode); }
} private void BindData(string pathNode, TreeNode parentNode)
{
DataTable dt = GetDataTable.GetDatatable("Select * From Paths Where PathNode='" + pathNode + "'");
TreeNode currentNode = new TreeNode();
DataRow row = dt.Rows[0];
currentNode.Text = row["Title"].ToString();
currentNode.Value = row["pathID"].ToString();
string pathName = row["pathName"].ToString().Trim();
currentNode.NavigateUrl = pathName;
if (pathName == "")
{
currentNode.SelectAction = TreeNodeSelectAction.None;
}
if (parentNode == null)
{
tree.Nodes.Add(currentNode);
}
else
{
parentNode.ChildNodes.Add(currentNode);
}
dt = GetDataTable.GetDatatable("Select * From Paths Where ParentNode='" + pathNode + "'");
if (dt.Rows.Count > 0)
{
foreach (DataRow sunRow in dt.Rows)
{
BindData(sunRow["PathNode"].ToString(), currentNode);
}
}
}
aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView runat="server" ID="tree" ShowLines="true" Target="main"></asp:TreeView>
</div>
</form>
</body>
</html>
getDataTable.csusing System;
using System.Data;
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 System.Data.SqlClient;
using System.Data.Common;/// <summary>
/// GetDataTable 的摘要说明
/// </summary>
public class GetDataTable
{
public GetDataTable()
{
}
public static DataTable GetDatatable(string sql)
{
DataTable table = new DataTable();
using (DbConnection conn = new SqlConnection("server=.;database=dbTest;uid=sa;pwd="))
{
conn.Open();
DbCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
DbDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
dap.Fill(table);
}
return table;
}
}