问题描述
下面代码的TreeView的SelectedNodeChange事件对于populatednode不触发。如果连续展开childnode,再点击展开的childnode,label也不会显示,并且TreeView缩回。求问怎么回事?多谢!usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;publicpartialclassTreeViewTest:System.Web.UI.Page{privateconstintCount=2;privatestring[]codeList={"th","os"};protectedvoidPage_Load(objectsender,EventArgse){updateTree();}protectedvoidupdateTree(){treePanel.Controls.Clear();TreeView[]treeList=newTreeView[Count];for(inti=0;i<Count;i++){vartree=newTreeView();tree.ID="treelist"+i.ToString();tree.TreeNodePopulate+=TreeBranch_SelectedNodePopulate;tree.SelectedNodeChanged+=TreeBranch_SelectedNodeChanged;stringcodeName=codeList[i];TreeNodecodeNode=newTreeNode(codeName,codeName);codeNode.SelectAction=TreeNodeSelectAction.Select;codeNode.PopulateOnDemand=true;tree.Nodes.Add(codeNode);tree.ExpandDepth=2;treePanel.Controls.Add(tree);treeList[i]=tree;}}protectedvoidTreeBranch_SelectedNodeChanged(objectsender,EventArgse){TreeViewtreeview=(TreeView)sender;stringtext=treeview.SelectedNode.Text;Label1.Text=text;}protectedvoidTreeBranch_SelectedNodePopulate(objectsender,TreeNodeEventArgse){LoadChildNode(e.Node);}publicvoidLoadChildNode(TreeNodeparentNode){TreeNodechildNode=newTreeNode("childNode","childNode");parentNode.ChildNodes.Add(childNode);childNode.PopulateOnDemand=true;}}
解决方案
解决方案二:
tree.TreeNodePopulate+=TreeBranch_SelectedNodePopulate;tree.SelectedNodeChanged+=TreeBranch_SelectedNodeChanged;你为什么要把注册事件写进循环里你知道这样写是给treeview注册了几遍事件吗
解决方案三:
你不如把事件直接写进前台aspx的treeview的属性里而不是在后台代码里动态乱加事件