I have a tree view created in my HTML Pa开发者_如何学运维ge
<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....
any suggestions.. thanks
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.
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;
}
}
}
You can try this, it works for Leaf Nodes only.
TreeView1.Nodes[0].Text = "";
TreeView1.Nodes[0].ShowCheckBox = false;
P.S: You will need a recursive function to access each node.
Set the node text to "" and it won't be rendered.
![This is how i have used.][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;
}
}
}
protected void Page_Load(object sender, EventArgs e)`{
TreeView1.Nodes.RemoveAt(2); }`
精彩评论