[英]Get selected radio button not in a list in ASP .NET
I have a number of radio buttons belonging to a group. 我有一些属于一个组的单选按钮。 I don't have them in a list, as they are all scattered around the page. 我没有将它们放在列表中,因为它们都散落在页面周围。 How can I easily get the selected radio button? 如何轻松获取所选单选按钮?
Maybe not the fastest way, but something like this should work: 也许不是最快的方式,但这样的事情应该有效:
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;
}
Use the "GroupName" attribute to group radio buttons into a group. 使用“GroupName”属性将单选按钮分组到一个组中。 This will keep them behaving as a group. 这将使他们表现为一个群体。 You will still need to query them individually for checked status. 您仍然需要单独查询它们以查看已检查状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.