How To Set The Value Attribute Of An ASP.NET RadioButton In Server-Side Code

Recently I ran into a situation wherein I needed to set the value attribute of a radio button rendered via an ASP.NET RadioButton control -- except I found there was no Value property. I attempted to set it via the InputAttributes property of the control, but this didn't work either -- if I set it using the value key, my value would be ignored and replaced with the server-side ID of the control (which is what ASP.NET uses as the value attribute by default), and if I added a value attribute this way, it would add an additional value attribute, giving me two in the same control.

The solution was actually a simpler approach: merely setting the value using the control's Attributes property (not InputAttributes). Here's an example:

rdoButton.Attributes["value"] = "somevalue";


Popular Posts

How To Mock Out Child Components In Unit Tests of Angular 2 Code

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

A Red Herring When Using Moq to Mock Methods With Optional Parameters

Unit Testing with a Mock Entity Framework DbContext and Fake DbSets