How to use CheckBox in TreeView

CheckBox in TreeView
treeview control display hierarchical data. this control can provide multi-node selection support. to allow multiple node selection, you need to display checkbox next to the image for a treeview node. treeview ShowCheckBoxes property value specify which type nodes will display checkbox. TreeNodeTypes possible values are All, Leaf, None, Parent and Root. Leaf display checkboxes for all leaf nodes and None value hide all checkboxes from treeview. you can use bitwise OR operator to combine TreeNodeType.Parent and TreeNodeType.Leaf values. so treeview will display checkbox for all parent and leaf nodes. because treeview ShowCheckBoxes enumeration type is a flag enumeration.

we can determine which node have their checkbox selected, we need to iterate through CheckedNodes collection. we also can override settings of ShowCheckBoxes by setting the each node ShowCheckBox property. the following c# example code demonstrate us how can we use checkbox in treeview web server control.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeViewCheckBox.aspx.cs" Inherits="TreeViewCheckBox" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <title> TreeView: how to use CheckBox in TreeView</title>
    <form id="form1" runat="server">
    <h2 style="color:Green">TreeView: ShowCheckBoxes</h2>
            Text="Checked Favorite"
        <br /><br />
                <asp:TreeNodeBinding DataMember="Products" Text="Products" />
                <asp:TreeNodeBinding DataMember="Type" TextField="Name" />
                <asp:TreeNodeBinding DataMember="Product" TextField="Name" ValueField="ID" />
        <br />
                    <Type Name="Microsoft">
                        <Product ID="1" Name="MS Office"/>
                        <Product ID="2" Name="ASP.NET"/>
                    <Type Name="Adobe">
                        <Product ID="1" Name="ColdFusion"/>
                        <Product ID="2" Name="Flex"/>
                        <Product ID="2" Name="Photoshop"/>
        <asp:Button ID="Button1" runat="server" Text="Submit Favorite" OnClick="Button1_Click" />

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class TreeViewCheckBox : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
            TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;
            TreeView1.ForeColor = System.Drawing.Color.Snow;
            TreeView1.BackColor = System.Drawing.Color.Tomato;
            TreeView1.Width = 200;
            Button1.Font.Bold = true;
            Button1.ForeColor = System.Drawing.Color.Crimson;

    protected void Button1_Click(object sender, EventArgs e) {
        Label1.Text = "Your favorite:";
        if (TreeView1.CheckedNodes.Count > 0)
            foreach (TreeNode node in TreeView1.CheckedNodes)
                Label1.Text += "<br />" + node.Text.ToString();
            Label1.Text = "Checked before submit.";
More example