13
votes

I am trying to add a custom data-required attribute to an asp Checkbox control, it works fine for all other controls I've used it on, but on checkboxs it renders the checkbox inside a span that has the custom attribute on. I've tried adding the attribute in the markup and tried adding it to the control in the code behind. Any ideas?

2
Wow, that is a bummer ... if it's just rendering like that, I'm inclined to think you might have to re-write the control, or at least override its "Render" method or something. Have you considered a client-side hack, adding the attribute by Javascript after the page loads? - McGarnagle
@dbaseman CheckBox control has collections for adding attributes to the rendered span and for the rendered input also - Adrian Iftode
@AdrianIftode nice! Good thing I don't work for tom, eh? - McGarnagle
That's worked great, thanks! I didn't think it should be difficult, I hoped was just missing something. - Tom Riley

2 Answers

12
votes

Here is the code I have used:

cb.InputAttributes.Add("data-group", "chkbox");

Then you can use JQuery to target all check-boxes.

 $("*[data-group='chkbox']").prop("checked", true);