Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting css class for dynamically generated checkbox

Tags:

c#

.net

asp.net

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>"));
like image 226
Legion Avatar asked Dec 04 '25 07:12

Legion


2 Answers

Replace:

checkbox.CssClass = "chkRoles" 

With:

checkbox.InputAttributes["class"] = "chkRoles"
like image 70
Wahtever Avatar answered Dec 06 '25 22:12

Wahtever


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>"));
like image 28
user2473569 Avatar answered Dec 06 '25 23:12

user2473569



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!