Sunday, February 1, 2009

ROOT TREE CODING IN VB>NET

Private Sub PopulateRootLevel() ' Tree-------------START
Dim objCommand As New SqlCommand("select catid,catname,(select count(*) FROM tblcat WHERE pcatid=sc.catid) childnodecount FROM tblcat sc where pcatid='-1' order by serialno,catid")
objCommand.Connection = con.createConnection(Session("site"))
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New Data.DataTable()
da.Fill(dt)
PopulateNodes(dt, treeCategory.Nodes)
End Sub
Private Sub PopulateNodes(ByVal dt As Data.DataTable, ByVal nodes As TreeNodeCollection)
For Each dr As Data.DataRow In dt.Rows
Dim tn As New TreeNode()
tn.Text = dr("catname").ToString()
tn.Value = dr("catid").ToString()
nodes.Add(tn)
'If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
Next
End Sub
Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal parentNode As TreeNode)
Dim objCommand As New SqlCommand("select catid,catname,(select count(*) FROM tblcat WHERE pcatid=sc.catid) childnodecount FROM tblcat sc where pcatid=@pcatid order by serialno,catid")
objCommand.Connection = con.createConnection(Session("site"))
objCommand.Parameters.Add("@pcatid", Data.SqlDbType.Int).Value = parentid
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New Data.DataTable()
da.Fill(dt)
PopulateNodes(dt, parentNode.ChildNodes)
End Sub
Protected Sub treeCategory_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles treeCategory.SelectedNodeChanged
lblSelprod.Text = treeCategory.SelectedNode.Text
lblselcataddpro.Text = treeCategory.SelectedNode.Text
Category_ExistingProducts_Bind()
ShowPanel(1, 1, 0, 0, 0)
lbl_selectedcategoryid.Visible = True
lbl_valueselectedcategory.Visible = True
lbl_valueselectedcategory.Text = treeCategory.SelectedNode.Value
End Sub


Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles treeCategory.TreeNodePopulate
PopulateSubLevel(CInt(e.Node.Value), e.Node)
End Sub

No comments:

Post a Comment