c# - 获取不在 ASP .NET 列表中的选定单选按钮

标签 c# asp.net radio-button

我有一些单选按钮属于一个组。我没有将它们列在列表中,因为它们都散布在页面各处。如何轻松获取选中的单选按钮?

最佳答案

也许不是最快的方法,但像这样的方法应该可行:

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/

相关文章:

c# - 用于在 background-url 中查找 url 内值的正则表达式

c# - TeamCity - 无法通过 API 进行身份验证

html - 如何让输入单选元素水平对齐?

Django BooleanField 作为单选按钮但在内联表单之间

c# - x :Key, x :Name, x :Type, x:XAML 中的静态的含义

c# - 如何反序列化动态Json对象?

javascript - Angular 主模块 app.js 正在被缓存,需要一种方法来确保添加新代码时不会被缓存

asp.net - 在什么情况下,您更喜欢 ASP.NET 网络表单而不是 MVC?

asp.net - 为什么在 asp.net webpart 中 ZoneTemplate 中缺少一些 div 内容?

javascript - If Else & Switch 语句不适用于 HTML 表单中的值(与 JavaScript 交互)