Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to add CheckBox in TreeView Control Using ASP.Net 2.0
Download Source Code :

In this article I will try to explain you how you can add CheckBox in TreeView control. Here I will create a very simple program that adds the nodes to TreeView Control dynamically.

Microsoft .Net Framework 2.0 has added many new features like classes, controls etc. Among these new features you will find a new addition to navigational controls. Add the new addition in this control is TreeView control that allows you to display your data in hierarchal way. For example you want to display your directory contents on your browser; this TreeView Control can be helpful you in displaying data. Similarly, there are many other uses for displaying data on a browser using TreeView Control.


TreeView control has many features e.g. you can bind TreeView control with XMLDataSource or you have auto format option that allow you to show TreeView control in your own way. TreeView control also allows you to display CheckBox either on Parent node or on Leaf Node or to all nodes i.e. Parent Node as well as Leaf Node. In following example I have created a TreeView Control programmatically having CheckBox on each leaf node and on the button click event a message is displayed on the browser that shows which child node you have selected of which parent. Code Snippet for this example is as follows:


<asp:TreeView ID="myTreeView" runat="server" ImageSet="Arrows" PathSeparator="|" ShowCheckBoxes="Leaf">


<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />


<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px"      VerticalPadding="0px" />


<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"      NodeSpacing="0px" VerticalPadding="0px" />




<asp:Button ID="cmdCheckSelectNode" runat="server" Text="Nodes Selected" /><br /><br />


<asp:Label ID="lblShowMessage" runat="server" /> 

On the Page_Load event and cmdCheckSelectNode_Click event add the following code:


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


'Declare Variable to geneate Random Number

Dim randomNo As New Random

Dim useNo As Integer = randomNo.Next(2, 10)


'Outer Loop to create Parent Node

For i As Integer = 1 To 5


Dim PNode As New TreeNode("Node Number : " & i, "NOD_" & i)


'Add Node to TreeView control



'Inner Loop to create Child Node

 For j As Integer = 1 To useNo


      'Add Child node to parent node

      PNode.ChildNodes.Add(New TreeNode("Child No : " & j))

      useNo = randomNo.Next(2, 10)








End Sub


Protected Sub cmdCheckSelectNode_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCheckSelectNode.Click


'Checks If Parent Node has Child Node

If myTreeView.CheckedNodes.Count > 0 Then


'Display your selected nodes

 For Each node As TreeNode In myTreeView.CheckedNodes

  lblShowMessage.Text += node.Text & " " & node.Parent.Text & "





  lblShowMessage.Text = "Sorry! You didnt select any node"


End If


End Sub


Note you can display your messages using String Builder. Using this approach means String Builder is more efficient.

Article Comments
I want to add controls dynamically using stringbuilder in a table

Posted on 8/25/2007 4:31:51 PM by chirag

Its very good

and is useful tome
thank u

Posted on 9/7/2007 8:54:28 AM by Rammohan reddy

I want how to add checkboxes in treeview control

Posted on 11/7/2007 4:52:02 AM by milind

I need know, only the checked subnodes. can u send me code how to check this one?

Posted on 11/26/2008 5:08:53 AM by Kasim Mohamed

I binded the Database values to Treeview Control...

Now i face a issue is when i click the + symbol of the Parent Node, it didn't expand. it only expand when i use treeview1.ExpandAll as well as treeView1.CollabseAll...

But what i need is it has to expand or Collaps when click the + Symbol..

for that what should i do?

Posted on 11/27/2008 10:16:29 AM by Kasim Mohamed

Thanks... It work for me perfectly...

Posted on 6/26/2010 4:03:49 AM by Yogesh

It's very useful. but i want to know more. so u can display more messages. thank you

Posted on 10/12/2010 3:31:35 AM by viji

Add Article Comment:
Name :
Email Address :
Comments :
<< Enumerating Web Sites present on a web server using System.DirectoryServices in ASP.NET

Disclaimer - Privacy
© 2002-2017