Asp.Net Listbox and Tree View Code Updated:
UI:
<table>
<tr>
<td> <asp:ListBox ID="listBox1" runat="server" Height="192px"
onselectedindexchanged="ListBox1_SelectedIndexChanged" Width="147px"
AutoPostBack="True"></asp:ListBox></td>
<td> <asp:ListBox ID="listBox2" runat="server" Height="190px"
onselectedindexchanged="ListBox2_SelectedIndexChanged" Width="142px"
AutoPostBack="True"></asp:ListBox></td>
<td> <asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView></td>
</tr>
</table>
Csharp Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
listBox1.Items.Add("Development");
listBox1.Items.Add("Testing");
listBox1.Items.Add("HR");
listBox1.Items.Add("Admin");
listBox1.Items.Add("External Affairs");
listBox2.Items.Add("EmpIDone");
listBox2.Items.Add("EMpIDTWo");
listBox2.Items.Add("EMptree");
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (TreeNode node in TreeView1.Nodes)
{
if (node.Text == listBox1.SelectedItem.Text)
isExists = true;
}
if (!isExists)
{
TreeView1.Nodes.Add(new TreeNode(listBox1.SelectedItem.Text));
}
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (TreeNode node in TreeView1.Nodes)
{
if (node.Text == listBox1.SelectedItem.ToString())
{
TreeNode currentnode = node;
currentnode.ChildNodes.Add(new TreeNode(listBox2.SelectedItem.Text));
}
}
}