In this article I will explain with an example, how to get selected (checked) CheckBoxes of TreeView on Button click in ASP.Net using C# and VB.Net.
When the Button is clicked, all the selected (checked) CheckBoxes are fetched and displayed using JavaScript Alert Message Box.
 
 
HTML Markup
The HTML Markup consists of an ASP.Net TreeView control with the ShowCheckBoxes property set to All and a Button.
<asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All">
</asp:TreeView>
<hr />
<asp:Button Text="Submit" runat="server" OnClick="Submit" />
 
Note: For Check All CheckBox functionality in TreeView, please refer my article TreeView Check Uncheck all CheckBoxes ( CheckBox ) using JavaScript and jQuery in ASP.Net.
 
 
Populating the ASP.Net TreeView Control
Inside the Page Load event, the ASP.Net TreeView control is populated with some dummy values of Fruits and Vegetables.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        TreeView1.Nodes.Add(new TreeNode("Fruits", "0"));
        TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Mango", "1"));
        TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Apple", "2"));
        TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Pineapple", "3"));
        TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Orange", "4"));
        TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Grapes", "5"));
 
        TreeView1.Nodes.Add(new TreeNode("Vegetables", "10"));
        TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Carrot", "11"));
        TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Cauliflower", "12"));
        TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Potato", "13"));
        TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Tomato", "14"));
        TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Onion", "15"));
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        TreeView1.Nodes.Add(New TreeNode("Fruits", "0"))
        TreeView1.Nodes(0).ChildNodes.Add(New TreeNode("Mango", "1"))
        TreeView1.Nodes(0).ChildNodes.Add(New TreeNode("Apple", "2"))
        TreeView1.Nodes(0).ChildNodes.Add(New TreeNode("Pineapple", "3"))
        TreeView1.Nodes(0).ChildNodes.Add(New TreeNode("Orange", "4"))
        TreeView1.Nodes(0).ChildNodes.Add(New TreeNode("Grapes", "5"))
        TreeView1.Nodes.Add(New TreeNode("Vegetables", "10"))
        TreeView1.Nodes(1).ChildNodes.Add(New TreeNode("Carrot", "11"))
        TreeView1.Nodes(1).ChildNodes.Add(New TreeNode("Cauliflower", "12"))
        TreeView1.Nodes(1).ChildNodes.Add(New TreeNode("Potato", "13"))
        TreeView1.Nodes(1).ChildNodes.Add(New TreeNode("Tomato", "14"))
        TreeView1.Nodes(1).ChildNodes.Add(New TreeNode("Onion", "15"))
    End If
End Sub
 
 
Get Selected (Checked) CheckBoxes of TreeView on Button click in ASP.Net
Inside the Click event handler, a loop is executed over the TreeView’s CheckNodes collection and the Text and Value parts of all the selected (checked) Nodes are displayed using JavaScript Alert Message Box.
C#
protected void Submit(object sender, EventArgs e)
{
    string message = "Selected nodes:\\n";
    foreach (TreeNode node in TreeView1.CheckedNodes)
    {
        message += node.Text + " " + node.Value + "\\n";
    }
 
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
 
VB.Net
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
    Dim message As String = "Selected nodes:\n"
    For Each node As TreeNode In TreeView1.CheckedNodes
        message += node.Text & " " & node.Value & "\n"
    Next
    ClientScript.RegisterStartupScript(Me.GetType, "alert", "alert('" & message & "');", True)
End Sub
 
 
Screenshot
Get Selected (Checked) CheckBoxes of TreeView on Button click in ASP.Net
 
 
Demo
 
 
Downloads