获取不在 ASP .NET 列表中的选定单选按钮 [英] Get selected radio button not in a list in ASP .NET

查看:42
本文介绍了获取不在 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;
}

这篇关于获取不在 ASP .NET 列表中的选定单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆