Click here to Skip to main content
15,890,557 members
Articles / Web Development
Tip/Trick

TreeView with Radio/Option Buttons - Select One at a Time - Using JavaScript

Rate me:
Please Sign up or sign in to vote.
4.13/5 (5 votes)
8 Jul 2013CPOL 29.9K   391   5   10
TreeView with radio/option buttons.

Introduction 

This article helps you to have radio buttons or option buttons with tree view. Normally treeview controls with ASP.NET allows to have checkboxes. By using a simple JavaScript it is possible to convert check boxes to radio buttons. Also, another small JavaScript allows only one node be selected at a time. While selecting a node it will uncheck/unselect all other nodes.

Image 1

Using the code

Tree View in source

ASP.NET
<asp:TreeView ID="TreeView1" runat="server" 
         ShowLines="True" ShowCheckBoxes="All">
    <Nodes>
         <asp:TreeNode Text="Root" Value="Root">
         	<asp:TreeNode Text="Parent 1" Value="New Node">
                      <asp:TreeNode Text="Leaf 1" Value="New Node"></asp:TreeNode>
                      <asp:TreeNode Text="Leaf 2" Value="New Node"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode Text="Parent 2" Value="New Node">
                      <asp:TreeNode Text="Leaf 3" Value="New Node"></asp:TreeNode>
                      <asp:TreeNode Text="Leaf 4" Value="New Node"></asp:TreeNode>
                </asp:TreeNode>
         </asp:TreeNode>
    </Nodes>
</asp:TreeView>  

JavaScript to convert checkboxes to Radio Buttons 

JavaScript
<script type="text/javascript">

    function MakeRadio() {
        var tv = document.getElementById("<%= TreeView1.ClientID %>");
        var chkArray = tv.getElementsByTagName("input");

        for (i = 0; i <= chkArray.length - 1; i++) {
            if (chkArray[i].type == 'checkbox') {
                chkArray[i].type = 'radio';
            }
        }
    }

    window.onload = MakeRadio;
    
</script>  

JavaScript to convert Select single Radio Button at a time 

JavaScript
<script type="text/javascript">

    function OnTreeClick(evt) {
        var src = window.event != window.undefined ? window.event.srcElement : evt.target;
        var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "radio");
        if (isChkBoxClick) {
            SelectOne(src.id);
        }
    }


    function SelectOne(objId) {
        var tv = document.getElementById("<%= TreeView1.ClientID %>");
        var chkArray = tv.getElementsByTagName("input");

        for (i = 0; i <= chkArray.length - 1; i++) {
            if (chkArray[i].type == 'radio') {
                if (chkArray[i].id != objId) {
                    chkArray[i].checked = false;
                }
            }
        }
    }
</script>

In code

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TreeView1.Attributes.Add("onclick", "OnTreeClick(event)");
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Santhisoft Technologies
India India

Comments and Discussions

 
Praisethank you Pin
Thamin6624-Aug-18 16:59
Thamin6624-Aug-18 16:59 
QuestionSo lovely Pin
Member 257669410-Jun-15 23:01
Member 257669410-Jun-15 23:01 
QuestionFails in IE 10 Pin
Member 1021909621-Aug-13 9:08
Member 1021909621-Aug-13 9:08 
AnswerRe: Fails in IE 10 Pin
Member 1021909622-Aug-13 3:04
Member 1021909622-Aug-13 3:04 
QuestionGreat Pin
_Thomas29-Jul-13 4:12
_Thomas29-Jul-13 4:12 
AnswerRe: Great Pin
Lovely M29-Jul-13 19:43
Lovely M29-Jul-13 19:43 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi27-Jul-13 22:12
professionalAmir Mohammad Nasrollahi27-Jul-13 22:12 
GeneralRe: My vote of 4 Pin
Lovely M29-Jul-13 19:45
Lovely M29-Jul-13 19:45 
GeneralMy vote of 5 Pin
Amogh Natu8-Jul-13 2:33
professionalAmogh Natu8-Jul-13 2:33 
GeneralRe: My vote of 5 Pin
Lovely M9-Jul-13 18:12
Lovely M9-Jul-13 18:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.