如何使用组名查找选中的单选按钮我的代码附加

本文关键字:我的 单选按钮 代码 何使用 查找 | 更新日期: 2023-09-27 18:15:56

我正在尝试使用组名找到选中的无线电的值,我有返回它的方法,但是我应该将该方法与组名

一起传递

方法在这里,

private string getRadioValue(ControlCollection clts, string groupName)
{
    string ret = "";
    foreach (Control ctl in clts)
    {
        if (ctl.Controls.Count != 0)
        {
            if (ret == "")
                ret = getRadioValue(ctl.Controls, groupName);
        }
        if (ctl.ToString() == "System.Web.UI.WebControls.RadioButton")
        {
            RadioButton rb = (RadioButton)ctl;
            if (rb.GroupName == groupName && rb.Checked == true)
                ret = rb.Attributes["Value"];
        }
    }
    return ret;
}

我像

那样使用它
Oc.aProjectSubmited = getRadioValue(RadioButton,"Aps");

ap是无线电组,但在单选按钮上获得错误"无效参数"?

希望你的建议提前谢谢

如何使用组名查找选中的单选按钮我的代码附加

这是因为您通过了RadioButton。您的方法接受ControlCollection,而不是Control

为什么不传递这个呢?控件传递页面的整个ControlCollection ?或者任何其他你可能用来保存你想要检查的RadioButton的ControlCollection ?

下面是一个例子:

    protected void Page_Load(object sender, EventArgs e)
    {
        getRadioValue(this.Controls, "Hello");
    }
    private string getRadioValue(ControlCollection clts, string groupName)
    {
        string ret = "";
        foreach (Control ctl in clts)
        {
            if (ctl.Controls.Count != 0)
            {
                if (ret == "")
                    ret = getRadioValue(ctl.Controls, groupName);
            }
            if (ctl.ToString() == "System.Web.UI.WebControls.RadioButton")
            {
                RadioButton rb = (RadioButton)ctl;
                if (rb.GroupName == groupName && rb.Checked == true)
                    ret = rb.Attributes["Value"];
            }
        }
        return ret;
    }

下面是使用Linq来避免循环的简短版本…

public static string GetRadioValue(ControlCollection controls, string groupName)
{
   var selectedRadioButton = controls.OfType<RadioButton>().FirstOrDefault(rb => rb.GroupName == groupName && rb.Checked);
   return selectedRadioButton == null ? string.Empty : selectedRadioButton.Attributes["Value"];
}

使用LINQ:

container.Controls.OfType<RadioButton>().FirstOrDefault(r => r.GroupName == "GroupName" && r.Checked).Text;

ToString()不会给你想要的东西:你需要一些更像

的东西
private string getRadioValue(ControlCollection clts, string groupName)
{
    var ret = "";
    foreach (Control ctl in clts)
    {
        if (ctl.Controls.Count != 0)
        {
            ret = getRadioValue(ctl.Controls, groupName);
        }
        if (!string.IsNullOrEmpty(ret)) {
          return ret;
        }
        var rb = ctl as RadioButton;
        if (rb != null && rb.GroupName == groupName && rb.Checked)
                return = rb.Attributes["Value"];
        }
    }
    return ret;
}

更短的版本(在我的例子中,我需要的是Text of the Radio)按钮

你也可以把它作为HtmlGenericControl的一个扩展方法,比如div。

public static class HtmlGenericControlExtensions
{
    /// <summary>
    /// Gets selected value of radio button by group name.
    /// </summary>
    /// <param name="controls">Html generic control.</param>
    /// <param name="groupName">Name of the button group.</param>
    /// <returns>Selected radio button name.</returns>
    public static string GetSelectedRadioButtonName(this HtmlGenericControl control, string groupName)
        => control.Controls
            .OfType<RadioButton>()
            .FirstOrDefault(rb => rb.GroupName.Equals(groupName) && rb.Checked)?.Text ?? string.Empty;
}