Hi nadeem1218,
Check this example. Now please take its reference and correct your code.
Refering the below article i have created the example.
HTML
<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15"
OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
<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>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateTreeView(this.GetData("SELECT Id, Name FROM VehicleTypes"), 0, null);
}
}
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);
PopulateTreeView(this.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " + child.Value), int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
string selectedNode = this.TreeView1.SelectedNode.Text;
Response.Redirect("Details.aspx?Node=" + selectedNode);
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.PopulateTreeView(Me.GetData("SELECT Id, Name FROM VehicleTypes"), 0, Nothing)
End If
End Sub
Private Sub PopulateTreeView(dtParent As DataTable, parentId As Integer, treeNode As TreeNode)
For Each row As DataRow In dtParent.Rows
Dim child As New TreeNode() With { _
.Text = row("Name").ToString(), _
.Value = row("Id").ToString() _
}
If parentId = 0 Then
TreeView1.Nodes.Add(child)
PopulateTreeView(Me.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " + child.Value), Integer.Parse(child.Value), child)
Else
treeNode.ChildNodes.Add(child)
End If
Next
End Sub
Private Function GetData(query As String) As DataTable
Dim dt As New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query)
Using sda As New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
Return dt
End Using
End Function
Protected Sub TreeView1_SelectedNodeChanged(sender As Object, e As EventArgs)
Dim selectedNode As String = Me.TreeView1.SelectedNode.Text
Response.Redirect("Details.aspx?Node=" + selectedNode)
End Sub
Details
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["Node"]))
{
Response.Write("Selected Text is : " + Request.QueryString["Node"]);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(Request.QueryString("Node")) Then
Response.Write("Selected Text is : " & Request.QueryString("Node"))
End If
End Sub
Screenshot