Take the 2-minute tour ×

I need to create a treeview control with checkboxes. However, my treeview needs to have checkboxes for the parent and the child. The business logic in my situation is that the user may choose "MS Office" but may not want "Microsoft".

I came across asp.net TreeView: how to use CheckBox in TreeView which helps, however, I can't seem to create a checkbox for "Microsoft" & "Adobe" nodes. How would I go about doing that?

enter image description here

The html code is:

    <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" />  

The code behind is:

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;

namespace TreeviewCheckboxSample
    public partial class Treeview : System.Web.UI.Page
        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.";

        protected void Page_Load(object sender, EventArgs e)
            if (!this.IsPostBack)
                TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;
                TreeView1.ForeColor = System.Drawing.Color.Black;
                TreeView1.BackColor = System.Drawing.Color.White;
                TreeView1.Width = 200;
                Button1.Font.Bold = true;
                Button1.ForeColor = System.Drawing.Color.Crimson;
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The ShowCheckBoxes property doesn't really cover this case.

However, you can change the ShowCheckBox property on each individual node after the tree is bound. That page contains some example code that even does it based on the node's depth, which looks like what you're after.

share|improve this answer
Thanks. "TreeView1.ShowCheckBoxes = TreeNodeTypes.All;" was what I needed. –  DotNetRookie Oct 1 '12 at 13:36
Oh, if that works for you, great. I wasn't sure whether or not you wanted one for "Products". –  Rawling Oct 1 '12 at 13:42
add comment

Your Answer


