treeview查找定位

cjijh 2004-08-14 04:09:08
我有一个treeview ,现在想根据node.text去查找到该节点,然后选中
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
buildTree()'''初始化treeview

Dim item As TreeNode
For Each item In TreeView1.Nodes
If GetChildText(item) = True Then
Exit Sub'''一找到就退出
End If
Next i
End If
End Sub
Public Function GetChildText(ByVal node As TreeNode) As Boolean
Dim num As Integer
Dim child As TreeNode
For Each child In node.Nodes
If child.Text.ToString = TextBox1.Text.ToString Then'''''获取要找的文本
TreeView1.SelectedNodeIndex = child.GetNodeIndex.ToString
Response.Write(child.GetNodeIndex.ToString)
Exit For
Return True
End If

Next child
End Function

问题:不能找到对应的节点
什么地方出错了 各位大虾
...全文
198 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cjijh 2004-08-14
  • 打赏
  • 举报
回复
呵呵 高人
结贴

有时间在帮我看看这个问题:(解决了那个500m空间送你)
http://community.csdn.net/Expert/topic/3249/3249116.xml?temp=.3563654
BearRui 2004-08-14
  • 打赏
  • 举报
回复
TreeView1.SelectedNodeIndex = child.GetNodeIndex.ToString

这句有问题。
---------------------------------------


private bool FindAll(TreeNodeCollection nodes)
{
foreach(TreeNode node in nodes)
{
if(node.Text=="hoiu") //找到
{
//选中该节点
tvMain.SelectedNode=node;
return true;
}
}
//递归
if(node.Nodes.Count!=0)
FindAll(node.Nodes);

return false;
}
俞庆平 2004-08-14
  • 打赏
  • 举报
回复
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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 findtree
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;

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.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string TreeNodeIndex;
for(int i = 0; i < TreeView1.Nodes.Count; i++)
{
TreeNodeIndex = FindByValue(TreeView1.Nodes[i],TextBox1.Text);
if(!(TreeNodeIndex.Equals("")))
{
TreeView1.SelectedNodeIndex = TreeNodeIndex;
TreeView1.DataBind();
break;
}
}

}
string FindByValue(Microsoft.Web.UI.WebControls.TreeNode Node,string Text)
{
string TreeNodeIndex = "";
if(Node.Text.Equals(Text))
{
TreeNodeIndex = Node.GetNodeIndex();
return TreeNodeIndex;
}

for(int i = 0; i < Node.Nodes.Count; i++)
{
TreeNodeIndex = FindByValue(Node.Nodes[i],TextBox1.Text);
if(!(TreeNodeIndex.Equals("")))
{
return TreeNodeIndex;
}
}
return "";
}
}
}
俞庆平 2004-08-14
  • 打赏
  • 举报
回复
WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="findtree.WebForm1" %>
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<iewc:TreeView id="TreeView1" style="Z-INDEX: 101; LEFT: 144px; POSITION: absolute; TOP: 64px"
runat="server">
<iewc:TreeNode Text="Node0" Expanded="True"></iewc:TreeNode>
<iewc:TreeNode Text="Node1"></iewc:TreeNode>
<iewc:TreeNode Text="Node2">
<iewc:TreeNode Text="Node6"></iewc:TreeNode>
<iewc:TreeNode Text="Node7"></iewc:TreeNode>
<iewc:TreeNode Text="Node8"></iewc:TreeNode>
</iewc:TreeNode>
<iewc:TreeNode Text="Node3">
<iewc:TreeNode Text="Node9"></iewc:TreeNode>
<iewc:TreeNode Text="Node10"></iewc:TreeNode>
<iewc:TreeNode Text="Node11"></iewc:TreeNode>
</iewc:TreeNode>
<iewc:TreeNode Text="Node4"></iewc:TreeNode>
<iewc:TreeNode Text="Node5"></iewc:TreeNode>
</iewc:TreeView>
<asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 96px" runat="server">Node0</asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 280px; POSITION: absolute; TOP: 64px" runat="server"
Text="Find"></asp:Button></FONT>
</form>
</body>
</HTML>
俞庆平 2004-08-14
  • 打赏
  • 举报
回复
我看错你的意思了。你说是要定位是吗?
下面的代码就是啦。
private void Button1_Click(object sender, System.EventArgs e)
{
string TreeNodeIndex;
for(int i = 0; i < TreeView1.Nodes.Count; i++)
{
TreeNodeIndex = FindByValue(TreeView1.Nodes[i],TextBox1.Text);
if(!(TreeNodeIndex.Equals("")))
{
TreeView1.SelectedNodeIndex = TreeNodeIndex;
TreeView1.DataBind();
break;
}
}

}
string FindByValue(Microsoft.Web.UI.WebControls.TreeNode Node,string Text)
{
string TreeNodeIndex = "";
if(Node.Text.Equals(Text))
{
TreeNodeIndex = Node.GetNodeIndex();
return TreeNodeIndex;
}

for(int i = 0; i < Node.Nodes.Count; i++)
{
TreeNodeIndex = FindByValue(Node.Nodes[i],TextBox1.Text);
if(!(TreeNodeIndex.Equals("")))
{
return TreeNodeIndex;
}
}
return "";
}
cjijh 2004-08-14
  • 打赏
  • 举报
回复
有点晕 原来是只能找到第2层树 找不到第3层树

的用递归
大哥能给我修改一下上面代码吗
cjijh 2004-08-14
  • 打赏
  • 举报
回复
这个我知道一点 值类型 引用类型 和object 之间的相互转换
对应il的装箱和拆箱

可是一般来说是能自动完成的

要不我把源代码贴上你看看
cjijh 2004-08-14
  • 打赏
  • 举报
回复
试了 还是不能找到指定位置 ???
俞庆平 2004-08-14
  • 打赏
  • 举报
回复
返回结果应当是:
True
False
True
False
True
俞庆平 2004-08-14
  • 打赏
  • 举报
回复
using System;
class Test
{
public static void Main()
{
// Numeric equality: True
Console.WriteLine((2 + 2) == 4);

// Reference equality: different objects, same boxed value: False
object s = 1;
object t = 1;
Console.WriteLine(s == t);

// Define some string
string a = "hello";
string b = String.Copy(a);
string c = "hello";

// compare string values for a constant and an instance: True
Console.WriteLine(a == b);

// compare string references;
// a is a constant but b is an instance: False
Console.WriteLine((object)a == (object)b);

// compare string references, both constants have the same value,
// so string interning points to same reference: True
Console.WriteLine((object)a == (object)c);
}
}
俞庆平 2004-08-14
  • 打赏
  • 举报
回复
If child.Text.ToString = TextBox1.Text.ToString Then
这句有点问题:应使用:child.Text.ToString.Equals(TextBox1.Text),如果不理解,请查相应的参考书,看看比较字符串相等时这两者有何不同。

其它的地方没看出有什么问题。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧