How To Find Checked Radio Button Using Group name My code Attached?
16,673
Solution 1
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;
}
Solution 2
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"];
}
Solution 3
Use LINQ:
container.Controls.OfType<RadioButton>().FirstOrDefault(r => r.GroupName == "GroupName" && r.Checked).Text;
Author by
tester Joe
Updated on June 24, 2022Comments
-
tester Joe about 2 years
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
-
aiapatag about 11 yearswell, ToString() will still work although it's not a best practice. I don't think this is the answer though.
-
tester Joe about 11 years@Bob what should i pass for "ControlCollection clts" ?
-
Bob Vale about 11 years@aiapatag OK didn't check that
RadioButton.ToString()
outputted the class name, however if an inherited control was used that used a different implementation of ToString then my answer would still work -
aiapatag about 11 years@BobVale yes indeed. But what I'm saying is... that is not the answer to the poster's question.
-
Bob Vale about 11 years@testerJoe Guessing the value attribute for the found radio button is null or unset.
-
tester Joe about 11 yearsif i wanna get id of radio then i would replace ["Value"] by ["Id"] ?
-
Bob Vale about 11 years@testerJoe either
rb.ID
if you want the server side id orrb.ClientID
if you want the client side id -
tester Joe about 11 years@BOb i did the same Thanks
-
wloescher about 9 yearsJust found a similar answer from SLaks on a similar question... stackoverflow.com/questions/1797907/…
-
track0 over 7 yearsThe "similar answer" mentioned above relates to WinForms - this is a WebForms question.