using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Web.UI.WebControls;

namespace ASP.NET答疑300问例程.第5章_高级Web控件
{
	/// <summary>
	/// TreeViewExample2 的摘要说明。
	/// </summary>
	public class TreeViewExample9 : System.Web.UI.Page
	{
		protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
		protected System.Web.UI.WebControls.Button DelButton;
		protected System.Web.UI.WebControls.TextBox FindTextBox;
		protected System.Web.UI.WebControls.TextBox AddTextBox;
		protected System.Web.UI.WebControls.Button AddButton;
		protected System.Web.UI.WebControls.Label NodeIndexLabel;
		protected System.Web.UI.WebControls.TextBox SelTextBox;
		protected System.Web.UI.WebControls.Button SelButton;
		protected System.Web.UI.WebControls.Button ExpandLevelButton;
		protected System.Web.UI.WebControls.TextBox ExpTextBox;
		protected System.Web.UI.WebControls.Button FindButton;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.TreeView1.SelectedIndexChange += new Microsoft.Web.UI.WebControls.SelectEventHandler(this.TreeView1_SelectedIndexChange);
			this.DelButton.Click += new System.EventHandler(this.DelButton_Click);
			this.FindButton.Click += new System.EventHandler(this.FindButton_Click);
			this.AddButton.Click += new System.EventHandler(this.AddButton_Click);
			this.SelButton.Click += new System.EventHandler(this.SelButton_Click);
			this.ExpandLevelButton.Click += new System.EventHandler(this.ExpandButton_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void DelButton_Click(object sender, System.EventArgs e)
		{
			// 移除节点
			TreeNode delNode = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex);
			delNode.Remove();

			// 等效代码:
			// TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex).Remove();
		}

		private void FindButton_Click(object sender, System.EventArgs e)
		{
			string findStr = FindTextBox.Text.Trim();

			// 判断输入字符串是否为空，空则不进行任何处理
			if(findStr.Length == 0)
			{
				FindTextBox.Text = "输入值为空";
				return;
			}

			// 遍历查找节点
			TreeNode findNode = TravelFindTreeNode(TreeView1.Nodes, findStr);

			// 判断是成功找到
			if(findNode == null)
			{
				FindTextBox.Text = "未查询到符合条件的值";
			}
			else
			{
				FindTextBox.Text = "查询成功";

				// 置找到的节点为选择状态
				ExpandNode(TreeView1, findNode);

				// 在Label标签中显示节点的索引号
				NodeIndexLabel.Text = findNode.GetNodeIndex();
			}
		}

		// 查找值为nodeValue的节点
		private TreeNode TravelFindTreeNode(TreeNodeCollection nodes, string nodeValue)
		{
			if(nodes.Count == 0)
				return null;

			foreach(TreeNode node in nodes)
			{
				if(node.Text == nodeValue)
				{
					return node;
				}
				else
				{
					TreeNode tmpNode;
					tmpNode = TravelFindTreeNode(node.Nodes, nodeValue);
					if(tmpNode != null)
						return tmpNode;
				}
			}
			return null;
		}

		private void ExpandNode(TreeView treeView, TreeNode node)
		{
			// 暂存索引号在NodeIndex字符串变量中
			string NodeIndex = node.GetNodeIndex();

			// 展开各级节点
			while(node.GetType() == node.Parent.GetType())
			{
				node.Expanded = true;
				node = (TreeNode)node.Parent;
			}
			node.Expanded = true;

			// 将节点置为选择状态
			treeView.SelectedNodeIndex = NodeIndex;
		}

		private void AddButton_Click(object sender, System.EventArgs e)
		{
			string addStr = AddTextBox.Text.Trim();
			if(addStr.Length == 0)
				return;

			// 获得当前节点对象的引用
			TreeNode curNode = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex);

			// 生成一个新的节点，并赋值
			TreeNode newNode = new TreeNode();
			newNode.Text = addStr;

			// 在当前节点下面增加一个新的节点
			curNode.Nodes.Add(newNode);

			// 使当前节点和新增节点处于展开状态
			curNode.Expanded = true;
			newNode.Expanded = true;

			// 置新节点为选择状态
			TreeView1.SelectedNodeIndex = newNode.GetNodeIndex();

			// 在Label标签上显示新增节点的索引号
			NodeIndexLabel.Text = newNode.GetNodeIndex();

		}

		private void TreeView1_SelectedIndexChange(object sender, Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e)
		{
			NodeIndexLabel.Text = e.NewNode;
			NodeIndexLabel.Text += "<br> OldNode is:" + e.OldNode;
		}

		private void ExpandButton_Click(object sender, System.EventArgs e)
		{
			TreeView1.SelectedNodeIndex = "0";
			TreeView1.ExpandLevel = Convert.ToInt32(ExpTextBox.Text);
		}

		// 根据索引号直接展开点节
		private void SelButton_Click(object sender, System.EventArgs e)
		{
			string selIndexStr = SelTextBox.Text.Trim();
			if(selIndexStr.Length == 0)
				return;

			// 获得索引号代表的节点对象
			TreeNode selNode = TreeView1.GetNodeFromIndex(selIndexStr);
			if(selNode == null)
			{
				SelTextBox.Text = "不存在这个索引节点";
				return;
			}

			// 展开各级节点
			TreeView1.SelectExpands = true;
			while(selNode.Parent.GetType() == selNode.GetType())
			{
				((TreeNode)selNode.Parent).Expanded = true;
				selNode = (TreeNode)selNode.Parent;
			}
			// 将索引号代表的节点置为选择状态
			TreeView1.SelectedNodeIndex = selIndexStr;
		}
	}
}
