TreeView控件例子
XmL文件代码:
<?xml version="1.0" encoding="utf-8" ?> <Area> <Province iAreaID="1" cAreaName="北京市"/> <Province iAreaID="2" cAreaName="上海市"/> <Province iAreaID="3" cAreaName="天津市"/> <Province iAreaID="4" cAreaName="重庆市"/> <Province iAreaID="5" cAreaName="湖北省"> <City iAreaID="51" cAreaName="武汉市"/> <City iAreaID="52" cAreaName="黄冈市"/> <City iAreaID="53" cAreaName="荆州市"/> <City iAreaID="54" cAreaName="武穴市"/> <City iAreaID="55" cAreaName="十堰市"/> <City iAreaID="56" cAreaName="黄石市"/> </Province> <Province iAreaID="6" cAreaName="河北省"> <Province iAreaID="61" cAreaName="石家庄市"/> <Province iAreaID="62" cAreaName="唐山市"/> </Province> <Province iAreaID="7" cAreaName="山西省"> <Province iAreaID="71" cAreaName="太原市"/> <Province iAreaID="72" cAreaName="大同市"/> </Province> </Area>
aspx前代码:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TreeView ID="treeAreaList" runat="server" AutoGenerateDataBindings="false" OnSelectedNodeChanged="Tree_SelectedNodeChanged"></asp:TreeView> </div> </form> </body> </html>
CS代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; namespace WebApplication1.复合控制和模板页 { public partial class TreeView控件 : System.Web.UI.Page { XmlDocument xmlDoc; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { MakeParentNode(); } } #region protected void MakeParentNode() { treeAreaList.Nodes.Clear(); xmlDoc=new XmlDocument(); xmlDoc.Load(Server.MapPath("area.xml "));//动态加载xml文档 XmlNodeList nodeList=xmlDoc.SelectSingleNode("Area").ChildNodes;//获取子节点 //定义Area节点 TreeNode topNode=new TreeNode();//定义顶级节点 topNode.Text="area"; //tn.Value="-1"; topNode.Expanded=true; treeAreaList.Nodes.Add(topNode);//添加”区域“父节点 XmlElement element=null; TreeNode treeNode=null; //遍历区域下的所有省和直辖市 foreach (XmlNode node in nodeList) { element=(XmlElement)node; treeNode=new TreeNode(); treeNode.Text=element.GetAttribute("cAreaName"); //在树控件上显示省或直辖市 treeNode.Value=element.GetAttribute("iAreaID"); //获取节点值 treeNode.Expanded=true; topNode.ChildNodes.Add(treeNode); MakeChildNode(node.ChildNodes,treeNode); } } #endregion #region protected void MakeChildNode( XmlNodeList nodeList,TreeNode treeNode) { XmlElement element = null; TreeNode subTreeNode = null; foreach (XmlNode node in nodeList) { element = (XmlElement)node; subTreeNode = new TreeNode(); subTreeNode.Text = element.GetAttribute("cAreaName"); //在树控件上显示省或直辖市 subTreeNode.Value = element.GetAttribute("iAreaID"); //获取节点值 subTreeNode.Expanded = true; treeNode.ChildNodes.Add(subTreeNode); MakeChildNode(node.ChildNodes, subTreeNode); } } #endregion protected void Tree_SelectedNodeChanged(object sender, EventArgs e) { Response.Write("节点的值:" + treeAreaList.SelectedNode.Value + "<br/>"); Response.Write("节点的路径:" + treeAreaList.SelectedNode.ValuePath + "<br/>"); Response.Write("节点的数据路径:" + treeAreaList.SelectedNode.DataPath + "<br/>"); } } }
显示效果:
版权声明:本文为ai394495243原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。