我有一个 TreeView,其中包含所有存储在数据库中的文件夹和文件。我现在使用 AjaxToolkit 中的自动完成搜索功能创建了一个搜索功能。一切都很好,除了我正在努力寻找具有我从搜索中获得的值的节点。我现在认为它必须是一种递归方法,但我不知道该怎么做。
所以,问题是。如何根据已有的节点值在树视图中找到节点。我还需要获取所有父节点值。所以我可以从中选择节点。
以下是部分代码(在搜索完成后的回发中):
else
{
// postBack handler for AJAX AutoComplete Extender - JavaScript call: AutoCompletedClientItemSelected
if (Request.Form["__EVENTTARGET"] != null &&
Request.Form["__EVENTTARGET"] == "AutoCompleteExtender" &&
Request.Form["__EVENTARGUMENT"] != null)
{
//i have the value for the node, but i need to search for it here,
//and i also need the node-values from all the parents. I need the "path"
string nodeValue = Session["NodeValueFromSearchForm"].ToString();
string nodePath = "";
foreach (TreeNode node in TreeViewFolders.Nodes)
{
if (node.Value == nodeValue)
{
//I'm stuck here
nodePath += "\\s" + node.Value;
}
}
string prompt = String.Format("<script>{0}{1}{2}</script>", "javascript:__doPostBack('TreeViewFolders','s", nodePath, "')");
ScriptManager.RegisterStartupScript(this, GetType(), "message", prompt, false);
}
}
在foreach
循环中,我只得到“根”文件夹(在顶部)。如何递归地执行此操作,以到达子节点,最后找到我拥有唯一 node.value 的节点?
感谢 Georges Oates Larsen 的回答,我得到了它的工作。当用户在树视图中上传文件时,我没有考虑只保存节点的值路径。所以,现在我在上传过程中将值路径保存在我的“文件”表中,并将该值传递给__doPostBack