Monday, July 30, 2007

RadioButton Bug in DataList and Repeater Controls

Hey Guys,

Found an interesting bug while working with DataList and Repeater Controls or in controls where templates are created dynamically in VS 2005......RadioButtons are Not Mutually Exclusive when used in these Controls.....that means it allows us to check all radiobuttons even with a same group Name....Try it out and check.....The link at the end provides the bug description from microsoft and after some good night's search found a solution for this bug.....so thought of posting that solution here.......
Just need to create one JavaScript function...

function SetUniqueRadioButton(nameregex, current)
{
re = new RegExp(nameregex);
for(i = 0; i < document.forms[0].elements.length; i++)
{
elm = document.forms[0].elements[i]
if (elm.type == 'radio')
{
if (re.test(elm.name))
{
elm.checked = false;
}
}
}
current.checked = true;
}


I will show you how to call this function in code..........

SetUniqueRadioButton('dgGrid.*GroupName',this)

here dgGrid is the name of the DataList or Repeater Control.........and GroupName is the
GroupName of the RadioButtons


http://support.microsoft.com/default.aspx?scid=kb;en-us;316495

No comments: