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

Comments

Popular Posts

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

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

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Determine if a Column Exists in a DataReader

Setting Default Values in an Angular Reactive Form