在MSDN中写了关于TreeNode:
“默认情况下,节点处于选择模式。”
“要将节点置于选择模式,请将节点的NavigateUrl属性设置为空字符串。”
“当节点处于选择模式时,使用SelectAction属性指定选择节点时引发的事件。”
“设置TreeNodeSelectAction值TreeNodeSelectAction.Select在选择节点时引发SelectedNodeChanged事件。”
请参阅TreeNode
这是问题,可能是控件中的错误:
当我将TreeNode对象PopulateOnDemand值设置为true并在该节点上调用Collapse()函数时。 然后,除了SelectedNodeChanged事件之外,还会引发TreeNodeExpanded事件。 这与MSDN中所写的内容相矛盾。 根据MSDN,只有在TreeNodeSelectAction属性设置为时才会发生这种情况 TreeNodeSelectAction.SelectExpand value。
有些人知道原因吗?
以下是代码:
<asp:TreeView ID="TreeView1" runat="server" AutoGenerateDataBindings="False"
onselectednodechanged="TreeView1_SelectedNodeChanged"
ontreenodepopulate="TreeView1_TreeNodePopulate">
</asp:TreeView>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string path = Server.MapPath(".");
PopulateTopNodes(path);
}
}
//MSDN : Occurs when a node with its PopulateOnDemand property set to true is expanded in //the TreeView control.
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
LoadChildNode(e.Node);
}
private void PopulateTopNodes(string pathToRootFolder)
{
DirectoryInfo dirInfo = new DirectoryInfo(pathToRootFolder);
DirectoryInfo[] dirs = dirInfo.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
string relativePath = (dir.FullName).Replace(pathToRootFolderPrefix, "");
TreeNode folderNode = new TreeNode(dir.Name, relativePath);
if (dir.GetDirectories().Length > 0)
{
folderNode.PopulateOnDemand = true;
folderNode.Collapse();
}
folderNode.NavigateUrl = "";
folderNode.SelectAction = TreeNodeSelectAction.Select;
TreeView1.Nodes.Add(folderNode);
}
}
private void LoadChildNode(TreeNode treeNode)
{
string d = treeNode.NavigateUrl;
string action = treeNode.SelectAction.ToString();
string fullPath = Path.Combine(pathToRootFolderPrefix, treeNode.Value);
DirectoryInfo dirInfo = new DirectoryInfo(fullPath);
DirectoryInfo[] dirs = dirInfo.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
string relativePath = (dir.FullName).Replace(pathToRootFolderPrefix, "");
TreeNode folderNode = new TreeNode(dir.Name, relativePath);
if(dir.GetDirectories().Length>0){
folderNode.PopulateOnDemand = true;
folderNode.Collapse();
}
folderNode.NavigateUrl = "";
folderNode.SelectAction = TreeNodeSelectAction.Select;
treeNode.ChildNodes.Add(folderNode);
}
}
//MSDN:Occurs when a node is selected in the TreeView control.
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
}
由于
答案 0 :(得分:0)
我不知道ASP.Net,但只是阅读你的代码,你将它设置为SelectExpand,但在上面的评论中,听起来你认为你将它设置为选择(或者这只是一个错字示例代码?)。在您的示例中的两种方法中,它都写为:
folderNode.SelectAction = TreeNodeSelectAction.SelectExpand;