Legion
Legion

Reputation: 3427

setting css class for dynamically generated checkbox

I have a series of checkboxes that I'm adding to a panel programmatically. I set the CssClass property but instead of setting the CSS class of the control it just wraps it in a span with that CSS class. Is there a way to make it give the checkbox the class? I need the class on the actual checkbox because in javascript I'm selecting by class.

Here's my code:

        CheckBox checkbox = new CheckBox();
        checkbox.Text = checkboxText;
        checkbox.ID = checkboxID;
        checkbox.CssClass = "chkRoles";
        pnlMandatoryRoles.Controls.Add(checkbox);
        pnlMandatoryRoles.Controls.Add(new LiteralControl("<br>"));

Upvotes: 2

Views: 3343

Answers (2)

Wahtever
Wahtever

Reputation: 3678

Replace:

checkbox.CssClass = "chkRoles" 

With:

checkbox.InputAttributes["class"] = "chkRoles"

Upvotes: 6

user2473569
user2473569

Reputation: 31

Ues Like below

CheckBox checkbox = new CheckBox();
checkbox.Text = "checkboxText";
checkbox.ID = "checkboxID";
checkbox.InputAttributes["class"] ="chkRoles";
pnlMandatoryRoles.Controls.Add(checkbox);pnlMandatoryRoles.Controls.Add(new LiteralControl("<br>"));

Upvotes: 3

Related Questions