我有一些单选按钮属于一个组。我没有将它们列在列表中,因为它们都散布在页面各处。如何轻松获取选中的单选按钮?
最佳答案
也许不是最快的方法,但像这样的方法应该可行:
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;
}
关于c# - 获取不在 ASP .NET 列表中的选定单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3654453/