Set value property of RadioButton

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…?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

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.

Method 4

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x