繁体   English   中英

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

[英]How To Find Checked Radio Button Using Group name My code Attached?

我正在尝试使用组名查找已检查无线电的值我有返回它的方法但是我应该在该方法中传递什么以及组名

方法在这里,

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");

Aps 是单选组,但在我传递的单选按钮上出现错误“无效参数”?

希望得到您的建议 在此先感谢

这是因为你正在传递RadioButton 您的方法接受ControlCollection ,而不是Control

为什么不传递this.Controls传递页面的整个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;
}

更短的版本(在我的例子中我需要单选文本)按钮。

您也可以将其作为 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM