gdjlc

培养良好的习惯,每天一点一滴的进步,终将会有收获。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1、收缩其他已点过的节点

View Code
/// <summary>
/// 收缩其他已点过的节点
/// </summary>
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
TreeNodeCollection ts
= null;
if (e.Node.Parent == null)
{
ts
= ((TreeView)sender).Nodes;
}
else
ts
= e.Node.Parent.ChildNodes;
foreach (TreeNode node in ts)
{
if (node != e.Node)
{
node.Collapse();
}
}
}

 

2、设置节点收缩展开时的图片文件

View Code
/// <summary>
/// 设置节点收缩展开时的图片文件
/// </summary>
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
e.Node.ImageUrl
= "/images/folder_Open.gif";
}
protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
{
e.Node.ImageUrl
= "/images/folder_Close.gif";
}

3、查找指定节点以及它的所有子节点

View Code
/// <summary>
/// 查找指定节点以及它的所有子节点
/// </summary>
/// <param name="tnParent"></param>
/// <param name="tnList"></param>
private void FindNode(TreeNode tnParent, ref List<TreeNode> tnList)
{
tnList.Add(tnParent);
foreach (TreeNode tn in tnParent.ChildNodes)
{
FindNode(tn,
ref tnList);
}
}

4、删除选择的节点以及它的所有子节点

View Code
/// <summary>
/// 删除选择的节点以及它的所有子节点
/// </summary>
protected void btnDelNode_Click(object sender, EventArgs e)
{
if (this.TreeView1.SelectedNode != null)
{
List
<TreeNode> tnList = new List<TreeNode>();
FindNode(
this.TreeView1.SelectedNode, ref tnList);
List
<int> idList = new List<int>();
tnList.ForEach(t
=> idList.Add(Convert.ToInt32(t.Value)));
//删除所有节点,如果只删除选择节点用this.TreeView1.SelectedNode.Parent.ChildNodes.Remove(this.TreeView1.SelectedNode);
tnList.ForEach(t => t.Parent.ChildNodes.Remove(t));

//删除数据库中相应数据
//....
}
}

5、在选择的节点下面添加子节点

View Code
///在选择的节点下面添加子节点
TreeNode tn = new TreeNode("节点名称", "保存到数据库后的节点ID");
tn.ImageUrl
= "/images/page.gif";
this.TreeView1.SelectedNode.ImageUrl = "/images/folder_Open.gif";
this.TreeView1.SelectedNode.ChildNodes.Add(tn);
this.TreeView1.SelectedNode.Expand();

6、JS获取所有选中节点的值 (把节点的值保存在ToolTip,点击节点时获取这个值)

.aspx

View Code
<!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>
<mce:script language="javascript" type="text/javascript"><!--
function init1() {
var getAllNodes
= "";
var tree
= document.getElementById("treeView1").getElementsByTagName("Input");
for (var i = 0; i < tree.length; i++) {
if (tree[i].type == "checkbox" && tree[i].checked) {
if (tree[i].getAttribute("title", 2) != ""){
getAllNodes
= getAllNodes + tree[i].nextSibling.innerHTML + ";";
}
}
}
alert(getAllNodes);
//所有选中节点的值
}

// --></mce:script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="treeView1" runat="server" EnableClientScript="False" ShowExpandCollapse="False"
OnClick
="init1(this)">
</asp:TreeView>
</div>
</form>
</body>
</html>

.aspx.cs

View Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt
= GetDataTable();
DataRow[] rows
= dt.Select();
foreach (DataRow row in rows)
{
TreeNode nd
= new TreeNode();
nd.Text
= row["name"].ToString();
nd.Value
= row["id"].ToString();
nd.ToolTip
= row["id"].ToString();
nd.ShowCheckBox
= true;
treeView1.Nodes.Add(nd);
}
}
}
DataTable GetDataTable()
{
System.Data.DataTable dt
= new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(
new System.Data.DataColumn("id", typeof(System.Int32)));
dt.Columns.Add(
new System.Data.DataColumn("name", typeof(System.String)));
dr
= dt.NewRow();
dr[
0] = 1;
dr[
1] = "广东";
dt.Rows.Add(dr);
dr
= dt.NewRow();
dr[
0] = 2;
dr[
1] = "广西";
dt.Rows.Add(dr);
return dt;
}
posted on 2008-10-07 12:51  gdjlc  阅读(775)  评论(0编辑  收藏  举报