<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<HTML>
	<HEAD>
		<title>TreeViewExample2</title>
		<script language="C#" runat="server">
			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;	// 使当前节点和新增节点处于展开状态
				
				TreeView1.SelectedNodeIndex = newNode.GetNodeIndex();	// 置新节点为选择状态
			}

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

				// 等效代码:
				// TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex).Remove();
			}
		
		</script>
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<H3>TreeView Web控件添加、删除节点示例</H3>
			<iewc:treeview 
				id="TreeView1" 
				runat="server" 
				ChildType="数据" 
				SelectExpands="True" 
				ExpandedImageUrl="folderopen.gif"
				ImageUrl="folder.gif">
				<iewc:TreeNode Text="同学">
					<iewc:TreeNode Text="张三"></iewc:TreeNode>
				</iewc:TreeNode>
			</iewc:treeview>
			<br>
			<table cellPadding="5">
				<tr>
					<td align="center">
						<asp:button 
							id="DelButton" 
							runat="server" 
							Text="删除当前选中的节点"
							OnClick="DelButton_Click">
						</asp:button>
					</td>
				</tr>
				<tr>
					<td>
						<asp:textbox id="AddTextBox" runat="server" Width="104px"></asp:textbox>
						<asp:button 
							id="AddButton" 
							runat="server" 
							Font-Size="9pt" 
							Text="增加节点" 
							OnClick="AddButton_Click">
						</asp:button>
					</td>
				</tr>
			</table>
		</form>
	</body>
</HTML>
