aspx文件内容:
<%@ Page Language="C#" MasterPageFile="~/Admin/Admin.master" AutoEventWireup="true" CodeFile="User_Tree_Tj.aspx.cs" Inherits="Admin_User_Tree_Tj" Title="推荐结构图查看" Debug="true" EnableEventValidation="false"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="Title_Black"> 推荐结构图查看
<asp:TextBox ID="TextBox_Key" runat="server"></asp:TextBox>
<asp:Button ID="Button_Find" runat="server" OnClick="Button_Find_Click" Text="查找" /></div>
<asp:TreeView ID="TreeView1" runat="server" ShowLines="True" ExpandDepth="1" NodeIndent="10">
</asp:TreeView>
</asp:Content>
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;
public partial class Admin_User_Tree_Tj : System.Web.UI.Page
{
protected void Button_Find_Click(object sender, EventArgs e)
{
BindTreeView();
}
/// <summary>
/// 绑定TreeView控件
/// </summary>
protected void BindTreeView()
{
//先清空所有节点
TreeView1.Nodes.Clear();
string Sql = "select User_Id,User_Code,User_TuJian,User_Name,User_Yj from [User] order by User_Reg_Date desc";
DataTable dt = SqlHelper.ExecuteDateSet(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, Sql, null).Tables[0];
DataView parentNodes = dt.DefaultView;
//判断有无关键字//根据筛选DataTable中的内容
if (TextBox_Key.Text.Trim() != "")
{
parentNodes.RowFilter = "User_Code = '" + TextBox_Key.Text.Trim() + "'";
}
else
{
parentNodes.RowFilter = "User_TuJian = 0";
}
foreach (DataRowView parentNode in parentNodes)
{
TreeNode node = new TreeNode();
node.Text = "<div style='width:60px;float:left;'> [" + parentNode["User_Code"].ToString() + "] </div> <div style='width:100px;float:left;'>呢称:" + parentNode["User_Name"].ToString() + "</div><div style='width:100px;float:left;'>业绩:" + parentNode["User_Yj"].ToString() + "盒</div><div style='width:100px;float:left;'><a href='User_Info.aspx?Id=" + parentNode["User_Id"].ToString() + "'>查看</a><!-- | <a href='User_Del.aspx?Id=" + parentNode["User_Id"].ToString() + "'>删除</a>--></div><div class='Clear_Div'></div>";
node.Value = parentNode["User_Id"].ToString();
//切换节点的展开和折叠状态
node.SelectAction = TreeNodeSelectAction.Expand;
//绑定子节点
GetChildrenNode(dt, node);
//添加根节点
TreeView1.Nodes.Add(node);
}
}
/// <summary>
/// 绑定子节点
/// </summary>
/// <param name="dt">DataTable对象</param>
/// <param name="parentNode">根节点对象</param>
protected void GetChildrenNode(DataTable dt, TreeNode parentNode)
{
DataView childrenNodes = dt.DefaultView;
childrenNodes.RowFilter = "User_TuJian = " + parentNode.Value.Trim();
foreach (DataRowView childrenNode in childrenNodes)
{
TreeNode node = new TreeNode();
node.Text = "<div style='width:60px;float:left;'> [" + childrenNode["User_Code"].ToString() + "] </div> <div style='width:100px;float:left;'>呢称:" + childrenNode["User_Name"].ToString() + "</div><div style='width:100px;float:left;'>业绩:" + childrenNode["User_Yj"].ToString() + "盒</div><div style='width:100px;float:left;'><a href='User_Info.aspx?Id=" + childrenNode["User_Id"].ToString() + "'>查看</a><!-- | <a href='User_Del.aspx?Id=" + childrenNode["User_Id"].ToString() + "'>删除</a>--></div><div class='Clear_Div'></div>";
node.Value = childrenNode["User_Id"].ToString();
//通过递归调用显示所有子节点
GetChildrenNode(dt, node);
//添加节点
parentNode.ChildNodes.Add(node);
}
}
}