asp.net - Programmatically show and hide CheckBox in a TreeView

Show and hide checkboxes in a TreeView programmatically
The TreeView is an asp.net web server control. The TreeView is located under System.Web.UI.WebControls namespace. TreeView web server control displays hierarchical data. Such as a table of contents in a tree structure.

The following asp.net c# tutorial code demonstrates how we can show and hide checkboxes in a TreeView control programmatically. The asp.net c# developers can change the checkboxes visibility of a TreeView programmatically using its ShowCheckBoxes property.

The TreeView ShowCheckBoxes property gets or sets a value indicating which node types will display a check box in the TreeView control. The ShowCheckBoxes property value is a TreeNodeTypes.

The TreeNodeTypes represent the different node types (leaf, parent, and root) in the TreeView control. The TreeView ShowCheckBoxes property value is a bitwise combination of the TreeNodeTypes values. The default is TreeNodeType.None.

The TreeView ShowCheckBoxes property throws ArgumentOutOfRangeException if the bitwise combination value is outside the range of the TreeNodeTypes enumeration.

In this asp.net c# tutorial code, we used the ShowCheckBoxes property value to TreeNodeTypes.Leaf to show checkboxes on TreeView. And we used ShowCheckBoxes property value to TreeNodeTypes.None to hide/remove checkboxes from a TreeView control.

The ShowCheckBoxes property value to TreeNodeTypes.Leaf indicates a node with no child nodes. And the TreeNodeTypes.None indicates no nodes.
TreeViewShowCheckBoxes.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        TreeView1.ShowCheckBoxes = TreeNodeTypes.None;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to show hide CheckBox in TreeView, ShowCheckBoxes</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">TreeView Example: ShowCheckBoxes</h2>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        <asp:TreeView 
             ID="TreeView1"
             runat="server"
             DataSourceID="SiteMapDataSource1"
             Width="275"
             BorderWidth="2"
             BorderColor="LawnGreen"
             Font-Italic="true"
             ForeColor="IndianRed"
             >
        </asp:TreeView>
        <br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             ForeColor="CadetBlue"
             Text="TreeView Show CheckBoxes"
             Height="45"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
        <asp:Button 
             ID="Button2" 
             runat="server"
             ForeColor="CadetBlue"
             Text="TreeView Hide CheckBoxes"
             Height="45"
             OnClick="Button2_Click"
             Font-Bold="true"
             />
    </div>
    </form>
</body>
</html>
Web.sitemap
Web.sitemap source code here.

   
Related asp.net example