tester Joe
tester Joe

Reputation: 73

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

i am trying to find the value of checked radio using group name i have method that return it but what should i pass in that method along with group name

method is here,

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;
}

i use it like

Oc.aProjectSubmited = getRadioValue(RadioButton,"Aps");

where Aps is radio group but getting error "invalid argument" on radio button i pass ??

hopes for your suggestion thanks in advance

Upvotes: 3

Views: 17547

Answers (5)

Dusan Gojkov
Dusan Gojkov

Reputation: 1

Even more shorter version (in my example I needed the Text of the Radio) Button.

You can also make it as an extension method of HtmlGenericControl which would be div for example.

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;
}

Upvotes: 0

Adi S
Adi S

Reputation: 31

Use LINQ:

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

Upvotes: 1

wloescher
wloescher

Reputation: 4603

Here's a shorter version using Linq to avoid the loops...

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"];
}

Upvotes: 3

aiapatag
aiapatag

Reputation: 3430

This is because you are passing RadioButton. Your method accepts ControlCollection, not a Control.

Why not pass this.Controls to pass the whole ControlCollection of the page? Or any other ControlCollection that you might be using to keep that RadioButton you want to check?

Here's an example:

    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;
    }

Upvotes: 4

Bob Vale
Bob Vale

Reputation: 18474

The ToString() is not going to give you what you want: You need somthing more like

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;
}

Upvotes: 0

Related Questions