views:

47

answers:

1

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;
+1  A: 

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

http://stackoverflow.com/questions/997342/why-does-asp-net-radiobutton-and-checkbox-render-inside-a-span#1711516

mdresser