本文介绍了treeview发现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个使用数据表中的一些数据生成的树视图。 我想找到一个节点并显示它们。 我能找到...但我无法展示。 我的问题在哪里? 这是我的所有代码... protected void Page_Load( object sender,EventArgs e) { if (!IsPostBack) { string SERVER_NAME = Request.ServerVariables [ SERVER_NAME]。ToString(); MindResources._MetodosEstaticos.server = SERVER_NAME; BusinessLayer._MetodosEstaticos.setServer(SERVER_NAME); DataTable dtMenu = BusinessLayer._MetodosEstaticos.GetMenuTree(); loadTreeMenu(tvMenu,dtMenu); tvMenu.CollapseAll(); } } public TreeView loadTreeMenu(TreeView tvMenu,DataTable dtMenu) { if (dtMenu.Rows.Count > 0 ) { foreach (DataRow menu in dtMenu.Select( Parent_Id = 179)) { TreeNode ParentNode = new TreeNode(); ParentNode.Text = menu [ MenuName]。ToString(); tvMenu.Nodes.Add(ParentNode); loadTreeSubMenu( ref ParentNode, int .Parse(menu [ MenuCode]。ToString()),dtMenu); } } return tvMenu; } private void loadTreeSubMenu( ref TreeNode ParentNode, int ParentId,DataTable dtMenu) { DataRow [] childs = dtMenu.Select ( Parent_Id =' + ParentId + '); foreach (DataRow dRow in childs) { TreeNode child = new TreeNode(); child.Text = dRow [ MenuName]。ToString(); child.Value = dRow [ MenuCode]。ToString(); ParentNode.ChildNodes.Add(child); // 递归调用 loadTreeSubMenu( ref child, int .Parse(dRow [ MenuCode]。ToString()),dtMenu); } } 私有 void btnFind_Click( object sender,EventArgs e) { // 用法 TreeNode itemNode = null ; foreach (TreeNode节点 in tvMenu.Nodes) { itemNode = FromID(tbSearch.Text,node); if (itemNode!= null ) break ; } } public TreeNode FromID( string itemId,TreeNode rootNode) { foreach ( rootNode中的TreeNode节点。 ChildNodes) { if (node.Text.Equals(itemId)) return 节点; TreeNode next = FromID(itemId,node); if (next!= null ) return 下一个; } 返回 null ; } protected void btnProcurar_Click( object sender,EventArgs e) { // 用法 btnProcurar.Text = 搜索; TreeNode itemNode = null ; foreach (TreeNode节点 in tvMenu.Nodes) { itemNode = FromID(tbSearch.Text,node); if (itemNode!= null ) { itemNode。已选择= true ; tvMenu.SelectedNode.Expand(); btnProcurar.Text = 是的!!!!我终于找到!!!!!; tvMenu.FindNode(itemNode.ValuePath); break ; } } } <%@ Page 语言 = C# AutoEventWireup = true CodeBehind = WebForm1.aspx.cs 继承 = HRC.WebForm1 %> < !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 > < / head > < 正文 > < ; 表格 id = form1 runat = 服务器 > < div > < asp:TextBox runat = server id = tbSearch / > < asp:Bu tton 文字 = Procurar id = btnProcurar runat = server onclick = btnProcurar_Click / > < asp:TreeView runat = server ID = tvMenu ExpandDepth = 1 NodeStyle-CssClass = treeNode RootNodeStyle-CssClass = rootNode LeafNodeStyle-CssClass = leafNode ShowLines = true > < LeafNodeStyle CssClass = leafNode / > < NodeStyle CssClass = treeNode / > < RootNodeStyle CssClass = rootNode / > < SelectedNodeStyle BackColor = 蓝色 BorderColor = 黑色 BorderStyle = 实体 BorderWidth = 2px / > < / asp:TreeView > < / div > < / form > < / body > < / html > 解决方案 我的问题,我想,在方法 btnFind_Click()中。你声明了一个 TreeNode 类型的对象,最终为它分配了一些东西。但是,然后,该方法退出并因此抛弃您珍贵的TreeNode而不是显示它。 将显示代码放在此处: private void btnFind_Click( object sender,EventArgs e) { // 用法 TreeNode itemNode = 空; foreach (TreeNode节点 in tvMenu.Nodes) { itemNode = FromID(tbSearch.Text,node); if (itemNode!= null ) break ; } // 显示刚找到的节点。 if (itemNode!= null ) { DisplayTreeNodeInfo(ItemNode) ); } } 当然,你必须编写一个方法 DisplayTreeNodeInfo()完全适合你的需要。 I have a treeview that is generated with some data from a datatable.I want to find a node and show them.I can find... but i can't show.Where is my problem?Here is all my code...protected void Page_Load(object sender, EventArgs e){ if (!IsPostBack) { string SERVER_NAME = Request.ServerVariables["SERVER_NAME"].ToString(); MindResources._MetodosEstaticos.server = SERVER_NAME; BusinessLayer._MetodosEstaticos.setServer(SERVER_NAME); DataTable dtMenu = BusinessLayer._MetodosEstaticos.GetMenuTree(); loadTreeMenu(tvMenu, dtMenu); tvMenu.CollapseAll(); }}public TreeView loadTreeMenu(TreeView tvMenu, DataTable dtMenu){ if (dtMenu.Rows.Count > 0) { foreach (DataRow menu in dtMenu.Select("Parent_Id=179")) { TreeNode ParentNode = new TreeNode(); ParentNode.Text = menu["MenuName"].ToString(); tvMenu.Nodes.Add(ParentNode); loadTreeSubMenu(ref ParentNode, int.Parse(menu["MenuCode"].ToString()), dtMenu); } } return tvMenu;}private void loadTreeSubMenu(ref TreeNode ParentNode, int ParentId, DataTable dtMenu){ DataRow[] childs = dtMenu.Select("Parent_Id='" + ParentId + "'"); foreach (DataRow dRow in childs) { TreeNode child = new TreeNode(); child.Text = dRow["MenuName"].ToString(); child.Value = dRow["MenuCode"].ToString(); ParentNode.ChildNodes.Add(child); //Recursion Call loadTreeSubMenu(ref child, int.Parse(dRow["MenuCode"].ToString()), dtMenu); }}private void btnFind_Click(object sender, EventArgs e){ //Usage TreeNode itemNode = null; foreach (TreeNode node in tvMenu.Nodes) { itemNode = FromID(tbSearch.Text, node); if (itemNode != null) break; }}public TreeNode FromID(string itemId, TreeNode rootNode){ foreach (TreeNode node in rootNode.ChildNodes) { if (node.Text.Equals(itemId)) return node; TreeNode next = FromID(itemId, node); if (next != null) return next; } return null;}protected void btnProcurar_Click(object sender, EventArgs e){ //Usage btnProcurar.Text = "Search"; TreeNode itemNode = null; foreach (TreeNode node in tvMenu.Nodes) { itemNode = FromID(tbSearch.Text, node); if (itemNode != null) { itemNode.Selected = true; tvMenu.SelectedNode.Expand(); btnProcurar.Text = "Yes!!!! i finally Found!!!!!"; tvMenu.FindNode(itemNode.ValuePath); break; } }}<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="HRC.WebForm1" %><!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></head><body> <form id="form1" runat="server"> <div> <asp:TextBox runat="server" id="tbSearch"/><asp:Button Text="Procurar" id="btnProcurar" runat="server" onclick="btnProcurar_Click" /> <asp:TreeView runat="server" ID="tvMenu" ExpandDepth="1" NodeStyle-CssClass="treeNode" RootNodeStyle-CssClass="rootNode" LeafNodeStyle-CssClass="leafNode" ShowLines="true" > <LeafNodeStyle CssClass="leafNode" /> <NodeStyle CssClass="treeNode" /> <RootNodeStyle CssClass="rootNode" /> <SelectedNodeStyle BackColor="Blue" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" /> </asp:TreeView> </div> </form></body></html> 解决方案 Your problem, I suppose, is in the method btnFind_Click(). You're declaring an object of type TreeNode, eventually assigning something to it. But then, the method exits and therefore throws away your precious TreeNode instead of displaying it.Put the displaying code here:private void btnFind_Click(object sender, EventArgs e){ //Usage TreeNode itemNode = null; foreach (TreeNode node in tvMenu.Nodes) { itemNode = FromID(tbSearch.Text, node); if (itemNode != null) break; } // Display the node just found. if( itemNode != null) { DisplayTreeNodeInfo(ItemNode); }}Of course, You will have to write a method DisplayTreeNodeInfo() suitable exactly for your needs. 这篇关于treeview发现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-11 09:18