Show parent child node value at runtime and populate treeview in asp.net.
This program is use to populate treeview at runtime using asp.net. you can try this code.
This program is use to populate treeview at runtime using asp.net. you can try this code.
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls.TreeNode
Partial Class Bibliography
Inherits System.Web.UI.Page
Dim clscmn1 As New clsCommon
Dim cn As New clsConnection
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
PopulateRootLevel()
Else
Exit Sub
End If
End Sub
Private Sub PopulateRootLevel()
Dim dt1 As New DataTable()
Dim objCommand As New SqlCommand("select PRODUCT_CATEGORY_CD,PRODUCT_CATEGORY,(select count(*) FROM PRODUCT_CATEGORY " _
& "WHERE PARENT_CATEGORY_CD=sc.PRODUCT_CATEGORY_CD) as childnodecount FROM PRODUCT_CATEGORY sc where PARENT_CATEGORY_CD IS NULL", _
cn.openDb)
Dim da As New SqlDataAdapter(objCommand)
da.Fill(dt1)
PopulateNodes(dt1, TreeView1.Nodes)
End Sub
Private Sub PopulateSubLevel(ByVal PARENT_CATEGORY_CD As String, ByVal parentNode As TreeNode)
cn.closeDb()
Dim objCommand As New SqlCommand("select PRODUCT_CATEGORY_CD,PRODUCT_CATEGORY,(select count(*) FROM PRODUCT_CATEGORY " _
& "WHERE PARENT_CATEGORY_CD=sc.PRODUCT_CATEGORY_CD) as childnodecount FROM PRODUCT_CATEGORY sc where PARENT_CATEGORY_CD=@PARENT_CATEGORY_CD", _
cn.openDb)
objCommand.Parameters.Add("@PARENT_CATEGORY_CD", SqlDbType.Char).Value = PARENT_CATEGORY_CD
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
PopulateNodes(dt, parentNode.ChildNodes)
End Sub
Private Sub PopulateNodes(ByVal dt As DataTable, ByVal nodes As TreeNodeCollection)
'For Each dr As DataRow In dt.Rows
' Dim tn As New TreeNode()
' tn.Text = dr("PRODUCT_CATEGORY").ToString().Trim
' tn.Value = dr("PRODUCT_CATEGORY_CD").ToString().Trim
' nodes.Add(tn)
' 'If node has child nodes, then enable on-demand populating
' tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
'Next
For Each dr As DataRow In dt.Rows
Dim tn As New TreeNode()
tn.Text = dr("PRODUCT_CATEGORY").ToString().Trim
tn.Value = dr("PRODUCT_CATEGORY_CD").ToString().Trim
Dim NoofChild As Integer = Convert.ToInt32(dr("childnodecount").ToString().Trim)
If NoofChild <> 0 Then
tn.PopulateOnDemand = True
' tn.SelectAction = TreeNodeSelectAction.None
Else
tn.PopulateOnDemand = False
'If dr("TotalDoc").ToString().Equals("0") = False Then
' tn.NavigateUrl = "~/CategoryWiseProductList.aspx?ID=" + dr("CategoryID").ToString()
' tn.Text = tn.Text + " (" + dr("TotalProduct").ToString() & ")"
'Else
' tn.NavigateUrl = "javascript:alert('Product is not Available Under this Category.')"
'End If
End If
nodes.Add(tn)
Next
End Sub
Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
PopulateSubLevel(e.Node.Value, e.Node)
Session("Productbib") = e.Node.Value
End Sub
Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
Session("Productbib") = TreeView1.SelectedValue
If (Session("Productbib") <> "") Then
Response.Redirect("abc.aspx")
End If
End Sub