Sunday, August 2, 2009

JavaScript to select one checkbox at a time inside a gridview containing checkbox as template column

Many times we need JavaScript for controls inside a gridview.One such need is to check only one checkbox at a time.This code snippet illustrates how to do that and this can be a guideline for all such needs.

The below java script is for checkbox inside a gridview as template column.If one checkbox is selected, it should deselect other check boxes .In code behind u have to include JavaScript for each checkbox in rowdatabound event.



protected void dgprod_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox chkh = new CheckBox();
chkh=(checkbox)e.Row.FindControl("chkcat");
chkh.Attributes.Add("onclick", "javascript:return Selectone(this,'"+ dgprod.ClientID +"');");

}

javascript to select one checkbox at a time:

function selectone(chid,gridviewid)
{

var grid=document.getelementbyid("gridviewid");
for (i=1; i{
var inputs = grid.getElementsByTagName("input");
if (inputs.length>0)
{

for (i=1; i{
if(inputs[i].type=="checkbox")
{

var chk=$get(inputs[i].id);

if(chk.id!=chid.id)
{
chk.checked=false;
}

}
}

Regard

Prateek

No comments:

Post a Comment