Here I have created sample that will help you out.
HTML
<div>
Parents :<asp:DropDownList ID="ddlParents" runat="server" OnSelectedIndexChanged="ChangeChild"
AutoPostBack="true">
</asp:DropDownList>
Childs :
<asp:DropDownList ID="ddlChilds" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Select" Value="0" />
</asp:DropDownList>
<h3>
Vehicle Details</h3>
<hr />
<asp:TreeView ID="TreeView1" runat="server" ImageSet="News" NodeIndent="15">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
<asp:TextBox ID="Txtfind" runat="server" BorderColor="#999999" BorderStyle="Solid"
Style="margin-top: 0px; border-radius: 5px" Width="150px" BackColor="#CCCCCC"></asp:TextBox>
<asp:Button ID="srch" runat="server" Text="Search" OnClick="Search_Click" />
</div>
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateParents();
}
}
protected void ChangeChild(object sender, EventArgs e)
{
DataTable dtChild = GetChildData(int.Parse(ddlParents.SelectedItem.Value));
this.ddlChilds.Items.Clear();
this.ddlChilds.DataSource = dtChild;
this.ddlChilds.DataTextField = "Name";
this.ddlChilds.DataValueField = "Id";
this.ddlChilds.DataBind();
this.ddlChilds.Items.Insert(0, new ListItem("Select", "0"));
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["Name"].ToString(),
Value = row["Id"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetChildData(Convert.ToInt32(child.Value));
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
public DataTable GetParentData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string))});
dt.Rows.Add(1, "Audi");
dt.Rows.Add(2, "BMW");
dt.Rows.Add(3, "Bugati");
return dt;
}
public DataTable GetChildData(int vehicleTypeId)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("VehicleTypeId", typeof(int))});
dt.Rows.Add(1, "Audi-100", 1);
dt.Rows.Add(2, "BMW-Zx", 2);
dt.Rows.Add(3, "Bugati-895", 3);
dt.Rows.Add(4, "Audi-200", 1);
dt.Rows.Add(5, "BMW-Zz", 2);
dt.Rows.Add(6, "Bugati-800", 3);
DataRow[] filteredRows = dt.Select("VehicleTypeId=" + vehicleTypeId);
DataTable resultDt = dt.Clone();
foreach (DataRow dr in filteredRows)
{
resultDt.ImportRow(dr);
}
return resultDt;
}
public void PopulateParents()
{
DataTable dt = this.GetParentData();
this.ddlParents.DataSource = dt;
this.ddlParents.DataTextField = "Name";
this.ddlParents.DataValueField = "Id";
this.ddlParents.DataBind();
this.ddlParents.Items.Insert(0, new ListItem("Select", "0"));
this.PopulateTreeView(dt, 0, null);
}
protected void Search_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Txtfind.Text))
{
TreeNode searchedNode = null;
foreach (TreeNode node in TreeView1.Nodes)
{
searchedNode = SearchNode(node, Txtfind.Text);
if (searchedNode == null)
{
foreach (TreeNode childNode in node.ChildNodes)
{
searchedNode = SearchNode(childNode, Txtfind.Text);
if (searchedNode != null)
goto Here;
}
}
else
{
break;
}
}
Here:
if (searchedNode != null)
{
searchedNode.Select();
TreeView1.ExpandAll();
Txtfind.Text = "";
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "Not Found", "alert('Node " + Txtfind.Text + " not found');", true);
}
}
}
TreeNode SearchNode(TreeNode node, string searchText = null)
{
if (node.Text == searchText) return node;
TreeNode tn = null;
foreach (TreeNode childNode in node.ChildNodes)
{
tn = SearchNode(childNode);
if (tn != null) break;
}
if (tn != null) node.Expand();
return tn;
}
Screenshot