我有许多属于一个组的单选按钮。我没有将它们列在列表中,因为它们都分散在页面上。如何轻松获取选定的单选按钮?
也许不是最快的方法,但这样的方法应该有效:
private RadioButton GetSelectedRadioButton(string groupName)
{
return GetSelectedRadioButton(Controls, groupName);
}
private RadioButton GetSelectedRadioButton(ControlCollection controls, string groupName)
{
RadioButton retval = null;
if (controls != null)
{
foreach (Control control in controls)
{
if (control is RadioButton)
{
RadioButton radioButton = (RadioButton) control;
if (radioButton.GroupName == groupName && radioButton.Checked)
{
retval = radioButton;
break;
}
}
if (retval == null)
{
retval = GetSelectedRadioButton(control.Controls, groupName);
}
}
}
return retval;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)