Tom Riley
Tom Riley

Reputation: 1722

Adding custom attributes to an asp:CheckBox control

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?

Upvotes: 13

Views: 15498

Answers (2)

Ravi Ram
Ravi Ram

Reputation: 24498

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

Upvotes: 13

Adrian Iftode
Adrian Iftode

Reputation: 15683

use InputAttributes

Upvotes: 26

Related Questions