title: Creating a parent and child treeview checkbox nodes
body: <p>I need to create a treeview control with checkboxes. However, my treeview needs to have checkboxes for the parent and the child. <em>The business logic in my situation is that the user may choose "MS Office" but may not want "Microsoft".</em>  </p>
  
  <p>I came across <a href="http://asp-net-example.blogspot.com/2008/12/aspnet-treeview-how-to-use-checkbox-in.html" rel="nofollow noreferrer">asp.net TreeView: how to use CheckBox in TreeView</a> which helps, however, I can't seem to create a checkbox for "Microsoft" &amp; "Adobe" nodes.  How would I go about doing that?</p>
  
  <p><img src="https://i.stack.imgur.com/ZnFTv.gif" alt="enter image description here"></p>
  
  <p><strong>The html code is:</strong>  </p>
  
  <pre><code>&lt;form id="form1" runat="server"&gt;  
      &lt;div&gt;  
      &lt;h2 style="color:Green"&gt;TreeView: ShowCheckBoxes&lt;/h2&gt;  
          &lt;asp:Label   
              ID="Label1"   
              runat="server"   
              Text="Checked Favorite"  
              Font-Size="Medium"  
              ForeColor="SeaGreen"  
              Font-Italic="true"  
              &gt;  
          &lt;/asp:Label&gt;  
          &lt;br /&gt;&lt;br /&gt;  
          &lt;asp:TreeView   
              ID="TreeView1"  
              runat="server"  
              DataSourceID="XmlDataSource1"  
              &gt;  
              &lt;DataBindings&gt;  
                  &lt;asp:TreeNodeBinding DataMember="Products" Text="Products" /&gt;  
                  &lt;asp:TreeNodeBinding DataMember="Type" TextField="Name" /&gt;  
                  &lt;asp:TreeNodeBinding DataMember="Product" TextField="Name" ValueField="ID" /&gt;  
              &lt;/DataBindings&gt;  
          &lt;/asp:TreeView&gt;  
          &lt;br /&gt;  
          &lt;asp:XmlDataSource   
              ID="XmlDataSource1"  
              runat="server"  
              &gt;  
              &lt;Data&gt;  
                  &lt;Products&gt;  
                      &lt;Type Name="Microsoft"&gt;  
                          &lt;Product ID="1" Name="MS Office"/&gt;  
                          &lt;Product ID="2" Name="ASP.NET"/&gt;  
                      &lt;/Type&gt;  
                      &lt;Type Name="Adobe"&gt;  
                          &lt;Product ID="1" Name="ColdFusion"/&gt;  
                          &lt;Product ID="2" Name="Flex"/&gt;  
                          &lt;Product ID="2" Name="Photoshop"/&gt;  
                      &lt;/Type&gt;  
                  &lt;/Products&gt;  
              &lt;/Data&gt;  
          &lt;/asp:XmlDataSource&gt;  
          &lt;asp:Button ID="Button1" runat="server" Text="Submit Favorite" OnClick="Button1_Click" /&gt;  
      &lt;/div&gt;  
      &lt;/form&gt;  
  </code></pre>
  
  <p><strong>The code behind is:</strong>  </p>
  
  <pre><code>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 &gt; 0)
              {
                  foreach (TreeNode node in TreeView1.CheckedNodes)
                  {
                      Label1.Text += "&lt;br /&gt;" + node.Text.ToString();
                  }
              }
              else
              {
                  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;
              }
          }
      }
  }
  </code></pre>