【问题标题】:How can I find the selected RadioButton's value in ASP.NET?如何在 ASP.NET 中找到所选 RadioButton 的值?
【发布时间】:2011-08-09 14:32:42
【问题描述】:

我有两个asp:RadioButton 控件具有相同的GroupName,这基本上使它们相互排斥。

我的标记:

<asp:RadioButton ID="OneJobPerMonthRadio" runat="server" 
        CssClass="regtype"
        GroupName="RegistrationType"
        ToolTip="125"/>
<asp:RadioButton ID="TwoJobsPerMonthRadio" runat="server" 
        CssClass="regtype"
        GroupName="RegistrationType"
        ToolTip="200"/>

我的目的是找到选中的 RadioButton 的工具提示/文本。我有这个代码隐藏:

int registrationTypeAmount = 0;
if (OneJobPerMonthRadio.Checked)
{
    registrationTypeAmount = Convert.ToInt32(OneJobPerMonthRadio.ToolTip);
}
if (TwoJobsPerMonthRadio.Checked)
{
    registrationTypeAmount = Convert.ToInt32(TwoJobsPerMonthRadio.ToolTip);
}

我发现该代码丑陋且多余。 (如果我有 20 个复选框呢?)

有没有一种方法可以从一组具有相同GroupName 的 RadioButtons 中获取选中的RadioButton?如果没有,写一个的指针是什么?

P.S:在这种情况下我不能使用RadioButtonList

【问题讨论】:

    标签: c# asp.net linq radio-button


    【解决方案1】:

    你想这样做:

    RadioButton selRB = radioButtonsContainer.Controls.OfType<RadioButton>().FirstOrDefault(rb => rb.Checked);
    if(selRB != null)
    {
        int registrationTypeAmount = Convert.ToInt32(selRB.ToolTip);
        string cbText = selRB.Text;
    }
    

    radioButtonsContainer 是单选按钮的容器。

    更新

    如果您想确保获得具有相同组的 RadioButtons,您有 2 个选项:

    • 将它们放在单独的容器中

    • 将组过滤器添加到 lamdba 表达式中,如下所示:

      rb =&gt; rb.Checked &amp;&amp; rb.GroupName == "YourGroup"

    更新 2

    修改了代码,确保在没有选择 RadioButton 时它不会失败,从而使其更加防错。

    【讨论】:

    • +1:非常好的想法。我不会每次都有一个 asp.net(runat="server") 容器控件。
    • 如果您没有声明的容器,请使用该页面并按照建议按组查找。
    • @Adraian:不太好。访问页面 controlcollection 应该是递归的。实际上,我正在考虑按组名访问控件。但是 webforms 架构太死板了,无法做到这一点。曾经喜欢过网络表单,但我想我应该切换到 MVC。这些天来,我对 webforms 的喜爱越来越少,而对 c# 的喜爱越来越多……
    • 为什么不把单选按钮放在面板中?那将是您的 radioButtonsContainer。
    • 我的单选按钮在 &lt;asp:Content ID="Main" ContentPlaceHolderID="cphMain" ClientIDMode="Static" runat="server"&gt; 内,但我似乎无法通过 Page.FindControl 访问该容器。还有更多相关信息吗?
    【解决方案2】:

    您可以尝试写下与以下类似的方法:

        private RadioButton GetSelectedRadioButton(params RadioButton[] radioButtonGroup)
        {
            // Go through all the RadioButton controls that you passed to the method
            for (int i = 0; i < radioButtonGroup.Length; i++)
            {
                // If the current RadioButton control is checked,
                if (radioButtonGroup[i].Checked)
                {
                    // return it
                    return radioButtonGroup[i];
                }
            }
    
            // If none of the RadioButton controls is checked, return NULL
            return null;
        }
    

    然后,您可以像这样调用该方法:

    RadioButton selectedRadio = 
                 GetSelectedRadioButton(OneJobPerMonthRadio, TwoJobsPerMonthRadio);
    

    它会返回选中的那个(如果有的话),无论你有多少单选按钮,它都会起作用。如果愿意,您可以重写该方法,使其返回 SelectedValue。

    【讨论】:

    • 我在我的项目中将它与“System.Web.UI.HtmlControls.HtmlInputRadioButton”一起使用,而不仅仅是“RadioButton”,它工作得很好。谢谢!
    猜你喜欢
    • 2010-09-22
    • 2021-09-25
    • 2021-06-13
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多