Set value property of RadioButton
Asked Answered
M

4

10

I need to built a list of radio buttons, based on data I return from my DB. Each button needs to have a value associated with it that I can get out based on the selected button.

Ideally I would just use the RadioButtonList control, however, I need to have a very custom layout which a RadioButtonList doesn't appear to be able to handle.

An alternative would be to create individual RadioButtons and wrap them in a Panel to group them. However, there doesn't appear to be a Value property on a RadioButton?

Is there an alternative way to set a value to a RadioButton control? Alternatively, a way to completely customise the RadioButtonList output.

At the moment, I'm thinking I might have to resort to using HTML radio buttons with runat="server", must be a better way...?

Mickelson answered 20/2, 2012 at 21:1 Comment(1)
Good solution, thank you. Where are the web forms advocates now...Yarn
T
4

You could create your own radio button class which extends the standard one and adds a value property:

public class ValueCheckBox : System.Web.UI.WebControls.RadioButton
{
    public string Value { get; set; }
}
Trainload answered 20/2, 2012 at 21:12 Comment(3)
I like the solution, is there any advantage to using this over the Attributes.Add as suggested by Rahul and Marchin? Both seem to do the job.Mickelson
This one would make the Value property easier to manage in the long run in case you plan to reuse the control elsewhere. I mean, just compare radioButton.Attributes.Add("Key", "Value"); and radioButton.Value = "value";.Breathe
It looks like the attributes are supposed to be used for HTML attributes which get sent to the browser msdn.microsoft.com/en-us/library/…Trainload
B
3

You can always try using attributes to save the associated value. eg)

radioButton.Attributes.Add("Key", "Value");

Set the Group property to be the same for all the radio buttons and you should be good to go. Just remember, ASP .Net has a slight problem if these individual radio buttons are in different rows of a repeater, gridview or some such grid-style.

Breathe answered 20/2, 2012 at 21:15 Comment(1)
Ah yeh, forgot I could use Attributes.Add. Is there any advantage over using Travor Pilley's suggestion of extending the control? Both would work, torn between the two.Mickelson
A
1

For a quick and dirty set of STATIC radio buttons.
I used the Tag field in the Properties window to manually define a value.

If you are using a DB you should probably bind your data to it. You never know when you'll change a key or name.

Angelita answered 11/6, 2013 at 13:47 Comment(0)
L
-1

RadioButton control doesn't have Value property, that's right. You have to use Checked instead.

Lula answered 20/2, 2012 at 21:5 Comment(4)
Yes but Checked just tells me whether the field has been checked or not. The buttons are dynamic so I need to store an associated value.Mickelson
What do you think by 'associate value'? Do you want to do some kind of binding? That Checked property can be set, not only read.Lula
I mean that if I have 3 radio buttons, each one represents a row in my database. When the user checks one, I then need to check which button has been checked and associate it with a record in my database. So really I need to store the ID along with the radio button.Mickelson
You can use Attributes collection to store that kind of data or just extend RadioButton class with your own control that inherits from RadioButton.Lula

© 2022 - 2024 — McMap. All rights reserved.