简体   繁体   English

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

[英]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.

相关问题 使用javascript循环通过asp.net单选按钮列表以获取选定的值和文本 - using javascript to loop through asp.net radio button list to get selected value and text 单选按钮列表选择的值在asp.net中不持久 - Radio button list selected value not persistent in asp.net 获取选定的输入单选按钮 asp.net webforms - get selected input radio button asp.net webforms 使用数据源asp.net获取单选按钮列表响应 - Get radio button list responses with data source asp.net 如何使用jquery获取选定的asp单选按钮? - how can i get the selected asp radio button using jquery? 尝试从单选按钮列表ASP.NET Web表单/ C#获取SelectedValue时出错 - Errors when trying to get SelectedValue from radio button list ASP.NET web form/C# 如何使用jQuery获取或设置单选按钮列表的选定索引? - How to get or set Selected index of the Radio Button List using jquery? 多个单选按钮列表,获取控制器中的选定值 - Multiple radio button list, get selected values in controller 在 ASP.NET 中的一组单选按钮(与 GroupName 属性关联)中查找所选单选按钮的值的最佳方法 - Best way to find the value of the selected radio button in a group of radio buttons (associated with GroupName attribute) in ASP.NET 如何将单选按钮列表值转换为asp.net中的位 - How to convert radio button list value to bit in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM