13

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?

4
  • 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? Commented Mar 30, 2012 at 8:22
  • @dbaseman CheckBox control has collections for adding attributes to the rendered span and for the rendered input also Commented Mar 30, 2012 at 8:27
  • @AdrianIftode nice! Good thing I don't work for tom, eh? Commented Mar 30, 2012 at 8:29
  • 1
    That's worked great, thanks! I didn't think it should be difficult, I hoped was just missing something. Commented Mar 30, 2012 at 8:34

2 Answers 2

26

use InputAttributes

Sign up to request clarification or add additional context in comments.

Comments

13

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.