Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom attributes to asp.NET RadioButton control

I want to add a custom attribute to an asp.net RadioButton called Key which I'm using client-side for an ajax request.

What I'm finding is that my aspx markup which is the following:

<asp:RadioButton ID="rdoPost" GroupName=PreferredContactMethod" value="Post" onclick="DoStuff(this)" runat="server" />

gets rendered in the page as

<span Key="ContactMethod">
   <input id="rdoPost" type="radio" name="PreferredContactMethod"" value="Post" onclick="DoStuff(this);" />
</span>

whereas I'd expected (and hoped) to get the following

<input id="rdoPost" type="radio" Key="ContactMethod" name="PreferredContactMethod"" value="Post" onclick="DoStuff(this);" />

I've tried the same thing with an asp TextBox control and it works exactly as I'd expect simply adding the Key="myKey" attribute to the <input type="text"/> element.

Is there a way around this with the standard RadioButton control, or will I have to inherit from the standard one to achieve the markup I'm wanting?

Also... (sorry to ask two questions at the same time), is adding non-standard attributes to html markup a bad idea anyway? Currently I'm using these attributes in JavaScript in the following way:

var key = rdoPost.Key;
like image 843
Matthew Dresser Avatar asked Sep 23 '10 11:09

Matthew Dresser


1 Answers

I've found from the question/answer below that the easiest way to do this is via the code-behind using the InputAttributes property as follows:

rdoPost.InputAttributes.Add("class", "myCheckBoxClass");

Why does ASP.Net RadioButton and CheckBox render inside a Span?

like image 154
Matthew Dresser Avatar answered Oct 15 '22 07:10

Matthew Dresser