Default.aspx:
<%@ Page Language="C#" 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>
<script type="text/javascript">
function postBackByObject()
{
var obj = window.event.srcElement;
if(obj.tagName == "INPUT" && obj.type == "checkbox")
{
__doPostBack("","");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged"
ShowCheckBoxes="All" ShowLines="True">
<Nodes>
<asp:TreeNode Text="根节点" Value="1">
<asp:TreeNode Text="一级节点1" Value="11">
<asp:TreeNode Text="二级节点1" Value="111">
<asp:TreeNode Text="三级节点1" Value="1111"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="二级节点2" Value="112"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="一级节点2" Value="12">
<asp:TreeNode Text="121" Value="121"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="一级节点3" Value="13"></asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</div>
</form>
</body>
</html>
Default.aspx.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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.TreeView1.Attributes.Add("onclick", "postBackByObject()");
}
}
protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
if (e.Node.Checked)
{
SetChildChecked(e.Node);
SetParentChecked(e.Node);
// 此处添加选中后会影响的其他事件
}
else
{
CancelChildChecked(e.Node);
// 此处添加取消后会影响的其他事件
}
}
/// <summary>
/// 选中父节点,其所有子节点也选中
/// </summary>
/// <param name="parentNode"></param>
private void SetChildChecked(TreeNode parentNode)
{
foreach (TreeNode tn in parentNode.ChildNodes)
{
tn.Checked = true;
if (tn.ChildNodes.Count > 0)
{
SetChildChecked(tn);
}
}
}
/// <summary>
/// 选中子节点,其父节点也选中
/// </summary>
/// <param name="childNode"></param>
private void SetParentChecked(TreeNode childNode)
{
if (childNode.Parent != null)
{
childNode.Parent.Checked = true;
SetParentChecked(childNode.Parent);
}
}
/// <summary>
/// 取消父节点,其所有子节点也取消
/// </summary>
/// <param name="parentNode"></param>
private void CancelChildChecked(TreeNode parentNode)
{
foreach (TreeNode tn in parentNode.ChildNodes)
{
tn.Checked = false;
if (tn.ChildNodes.Count > 0)
{
CancelChildChecked(tn);
}
}
}
}
遍历树
TreeNode tnRet = null;
foreach (TreeNode tn in TreeViewRegion.Nodes)
{
tnRet = FindNode( tn, yourValue );
if( tnRet != null ) break;
}
}
/// <summary>
/// 查找树节点递归算法
/// </summary>
/// <param name="tnParent"></param>
/// <param name="strValue"></param>
/// <returns></returns>
private TreeNode FindNode(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Text == strValue) return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.Nodes)
{
tnRet = FindNode(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}