Tuesday, January 18, 2011

Java Script Code to allow only one checkbox selection in GridView

To allow only one checkbox selection in GridView (like radio button selection from group).
Call the following function from checkbox onclick event like OnClick="CheckGridList();"
put the following code in javascript tag
function CheckGridList()
       {     
        var count=0;
        for (i=0; i < document.forms[0].elements.length; i++)
            {          
                if ((document.forms[0].elements[i].type == 'checkbox') &&
                    (document.forms[0].elements[i].name.indexOf('checkBox1') > -1))
                {
                    if (document.forms[0].elements[i].checked == true)
                    {                                  
                    count++;
                    if (count>1)
                        {
                            document.forms[0].elements[i].checked = false;                      
                            break;
                        }
                    }
                }
            }
            if (count > 1)
            {          
            alert('Please select only one checkbox from grid);
            return false;
            }
            else { return true;}
        }

No comments:

Post a Comment