visual-studio 在树视图控件中隐藏节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4101182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hide node in treeview control
提问by user175084
I have a tree view created in my HTML Page
我在 HTML 页面中创建了一个树视图
<asp:TreeView ID="TreeView1" runat="server"
onselectednodechanged="TreeView1_SelectedNodeChanged"
PopulateNodesFromClient="False" onunload="TreeView1_Unload">
<Nodes>
<asp:TreeNode Text="Reports" Value="Report">
<asp:TreeNode Text="Status" Value="Service">
</asp:TreeNode>
<asp:TreeNode Text="Status" Value="Status">
</asp:TreeNode>
<asp:TreeNode Text="Stats"
Value="Stats"></asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
now i want to hide the Stats node in the page load function in my code behind....
现在我想在后面的代码中隐藏页面加载功能中的 Stats 节点....
any suggestions.. thanks
任何建议..谢谢
采纳答案by Win
I use Telerik RadTreeView; TreeView doesn't have DataBound event and Visible property for each node. Here is the code to remove the child node for TreeView.
我使用 Telerik RadTreeView;TreeView 没有每个节点的 DataBound 事件和 Visible 属性。这是删除 TreeView 子节点的代码。
protected void Page_Load(object sender, EventArgs e)
{
RemoveNodeRecurrently(TreeView1.Nodes, "Status");
}
private void RemoveNodeRecurrently(TreeNodeCollection childNodeCollection, string text)
{
foreach (TreeNode childNode in childNodeCollection)
{
if (childNode.ChildNodes.Count > 0)
RemoveNodeRecurrently(childNode.ChildNodes, text);
if (childNode.Text == text)
{
TreeNode parentNode = childNode.Parent;
parentNode.ChildNodes.Remove(childNode);
break;
}
}
}
回答by Syed Ali Taqi
You can try this, it works for Leaf Nodes only.
你可以试试这个,它只适用于叶节点。
TreeView1.Nodes[0].Text = "";
TreeView1.Nodes[0].Text = "";
TreeView1.Nodes[0].ShowCheckBox = false;
TreeView1.Nodes[0].ShowCheckBox = false;
P.S: You will need a recursive function to access each node.
PS:您将需要一个递归函数来访问每个节点。
回答by Mohammad Jahangeer Ansari
protected void Page_Load(object sender, EventArgs e)`{
TreeView1.Nodes.RemoveAt(2); }`
TreeView1.Nodes.RemoveAt(2); }`
回答by SUMIT
![This is how i have used.][1]
![这是我使用的方式。][1]
protected void Page_Load(object sender, EventArgs e)
{
if (Session["type"] == null)
{
RemoveNodeRecurrently(rptTree.Nodes, "Create Users");
}
if (Session["user"] != null)
{
}
else
{
Response.Redirect(ConfigurationManager.AppSettings.Get("RootFolder") + "/ERP - Login.aspx");
}
}
private void RemoveNodeRecurrently(TreeNodeCollection childNodeCollection, string text)
{
foreach (TreeNode childNode in childNodeCollection)
{
if (childNode.ChildNodes.Count > 0)
RemoveNodeRecurrently(childNode.ChildNodes, text);
if (childNode.Text == text)
{
TreeNode parentNode = childNode.Parent;
parentNode.ChildNodes.Remove(childNode);
break;
}
}
}
回答by Tools
Set the node text to "" and it won't be rendered.
将节点文本设置为“”,它不会被渲染。

