Let's say we have the following Tree view structure.
We have four levels in the tree view and following will be the HTML code.
<asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All">
<Nodes>
<asp:TreeNode Text="YEAR" Value="YEAR">
<asp:TreeNode Text="SEM1" Value="SEM1">
<asp:TreeNode Text="SUB1" Value="SUB1"></asp:TreeNode>
<asp:TreeNode Text="SUB2" Value="SUB2"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="SEM2" Value="SEM2">
<asp:TreeNode Text="SUBJECT" Value="SUBJECT">
<asp:TreeNode Text="SUB3" Value="SUB3"></asp:TreeNode>
<asp:TreeNode Text="SUB4" Value="SUB4"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="SEM3" Value="SEM3">
<asp:TreeNode Text="SUBJECT" Value="SUBJECT">
<asp:TreeNode Text="SUB5" Value="SUB5"></asp:TreeNode>
<asp:TreeNode Text="SUB6" Value="SUB6"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
Now we need to access each level and check the relevant check boxes.
private void AccessTreeView()
{
foreach (TreeNode item in TreeView1.Nodes)
{
if (item.Text == "YEAR")
{
item.Checked = true; // Check the root node
foreach (TreeNode citem in item.ChildNodes)
{
if (citem.Text == "SEM1" || citem.Text == "SEM2") // Check the first level
{
citem.Checked = true;
}
foreach (TreeNode sitem in citem.ChildNodes)
{
if (sitem.Text == "SUBJECT")
{
sitem.Checked = true;// Check the second level
}
foreach (TreeNode scitem in sitem.ChildNodes)
{
scitem.Checked = true;// Check the third level
}
}
}
}
}
}
Our Final out put will be as follows.
No comments:
Write comments