Wednesday, August 28, 2013

how to select one check box at a time using java script in asp.net page when using a check box list

following javascript can control the selection of one check box at a time

function SelectOne(event, cname) {
            if (!event)
                event = window.event;
            var listItem = event.target || event.srcElement;
            var cbl = document.getElementById(cname);
            var listItems = cbl.getElementsByTagName('input');
            for (var i = 0; i < listItems.length; i++) {
                if (listItems[i] != listItem) {
                    listItems[i].checked = false;
                }
            }
        }

Usage:

<asp:CheckBoxList ID="chkRegt" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"
    Onclick="SelectOne(event,'ctl00_MainContent_chkRegt');readCBLReg('ctl00_MainContent_chkRegt')">
    <asp:ListItem Text="Yes">Yes</asp:ListItem>
    <asp:ListItem Text="No">No</asp:ListItem>
</asp:CheckBoxList>

No comments:

Post a Comment