Tuesday, January 27, 2009

Java Script in ASP.Net RadioButtonList

The below sample code shows how to fire an event for a RadioButtonList control of Asp.Net.

The sample contains the java script function and additionally you need to add the following code in the Page_load event of aspx.cs -
RadioButtonList1.Attributes.Add("onclick","RadioCheck(this);");

function RadioCheck(obj)
{
var srcEl = event.srcElement;
// If the Radio's label is cliked the OnClick is fired twice. To avoid this
if(srcEl.tagName == 'LABEL')
return;

var srcObj = document.getElementById(obj.id+"_"+srcEl.parentElement.parentElement.rowIndex);
if(srcObj)
alert("Selected Radio's Value - "+srcObj.value);
}