Refer Below sample code for your reference also refer the beloe article link for your reference how we can use Treeview with checkbox option.
SQL
CREATE TABLE [Type](
[Id] [int] NULL,
[Type] [varchar](50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [FruitsNVegetables]
(
[id] [nchar](10) NULL,
[Name] [varchar](50) NULL,
[TypeId] [varchar](50) NULL
) ON [PRIMARY]
GO
INSERT INTO [Type] ([Id],[Type])
SELECT 1,'Fruit'
UNION ALL
SELECT 2,'Vegetables'
INSERT INTO [FruitsNVegetables]([id],[Name] ,[TypeId])
SELECT 1,'Mango',1
union all
SELECT 2,'Apple',1
union all
SELECT 3,'Pineapple',1
union all
SELECT 4,'Orange',1
union all
SELECT 5,'Grapes',1
union all
SELECT 6 ,'Carrot',2
union all
SELECT 7 ,'Cauliflower',2
union all
SELECT 8,'Potato',2
union all
SELECT 9,'Tomato',2
union all
SELECT 10,'Onion',2
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All">
</asp:TreeView>
<br />
<br />
<asp:Button ID="btnFruits" runat="server" Text="Display Selected Items" OnClick="SelectedFruits" />
<br />
<br />
<asp:Label ID="lblSelectedDetails" runat="server"></asp:Label>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=TreeView1] input[type=checkbox]").bind("click", function () {
var table = $(this).closest("table");
if (table.next().length > 0 && table.next()[0].tagName == "DIV") {
//Is Parent CheckBox
var childDiv = table.next();
var isChecked = $(this).is(":checked");
$("input[type=checkbox]", childDiv).each(function () {
if (isChecked) {
$(this).attr("checked", "checked");
} else {
$(this).removeAttr("checked");
}
});
} else {
//Is Child CheckBox
var parentDIV = $(this).closest("DIV");
//alert(parentDIV);
if ($("input[type=checkbox]", parentDIV).length == $("input[type=checkbox]:checked", parentDIV).length) {
$("input[type=checkbox]", parentDIV.prev()).attr("checked", "checked");
} else {
$("input[type=checkbox]", parentDIV.prev()).removeAttr("checked");
}
}
});
})
</script>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTreeView();
}
}
private void BindTreeView()
{
DataTable dt = GetData("SELECT ID,Type FROM Type");
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeView1.Nodes.Add(new TreeNode(dt.Rows[i]["Type"].ToString(), dt.Rows[i]["ID"].ToString()));
DataTable dt2 = GetData("SELECT ID,Name FROM FruitsNVegetables WHERE TypeId =" + dt.Rows[i]["ID"].ToString() + "");
for (int j = 0; j < dt2.Rows.Count; j++)
{
TreeView1.Nodes[i].ChildNodes.Add(new TreeNode(dt2.Rows[j]["Name"].ToString(), dt2.Rows[j]["ID"].ToString()));
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["ConString"].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 SelectedFruits(object sender, EventArgs e)
{
string selectedItems = string.Empty;
for (int i = 0; i < TreeView1.Nodes.Count; i++)
{
//TreeView1.Nodes
if (TreeView1.Nodes[i].Checked)
{
// if yo need to do some code
}
for (int j = 0; j < TreeView1.Nodes[i].ChildNodes.Count; j++)
{
if (TreeView1.Nodes[i].ChildNodes[j].Checked)
{
selectedItems += string.Format("Type :{0} Item :{1}\\n\\r", TreeView1.Nodes[i].Text, TreeView1.Nodes[i].ChildNodes[j].Text);
}
}
}
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + selectedItems + "')", true);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindTreeView()
End If
End Sub
Private Sub BindTreeView()
Dim dt As DataTable = GetData("SELECT ID,Type FROM Type")
For i As Integer = 0 To dt.Rows.Count - 1
TreeView1.Nodes.Add(New TreeNode(dt.Rows(i)("Type").ToString(), dt.Rows(i)("ID").ToString()))
Dim dt2 As DataTable = GetData("SELECT ID,Name FROM FruitsNVegetables WHERE TypeId =" & dt.Rows(i)("ID").ToString() & "")
For j As Integer = 0 To dt2.Rows.Count - 1
TreeView1.Nodes(i).ChildNodes.Add(New TreeNode(dt2.Rows(j)("Name").ToString(), dt2.Rows(j)("ID").ToString()))
Next
Next
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim dt As DataTable = New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = 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 SelectedFruits(ByVal sender As Object, ByVal e As EventArgs)
Dim selectedItems As String = String.Empty
For i As Integer = 0 To TreeView1.Nodes.Count - 1
If TreeView1.Nodes(i).Checked Then
End If
For j As Integer = 0 To TreeView1.Nodes(i).ChildNodes.Count - 1
If TreeView1.Nodes(i).ChildNodes(j).Checked Then
selectedItems += String.Format("Type :{0} Item :{1}\\n\\r", TreeView1.Nodes(i).Text, TreeView1.Nodes(i).ChildNodes(j).Text)
End If
Next
Next
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('" & selectedItems & "')", True)
End Sub
Screenshot