简体   繁体   中英

Control.Attributes.Add doesn't work for check box

I have created dynamic checkbox with ID="chk" and I want to apply inline style to it. I tried

chk.Attributes.Add("style","opacity:1");

But it doesn't work as expected. It created span around check box and applies style to that. But it works

chk.InputAttributes["style"] = "opacity:1";

But former works fine for TextBox . Why this complexity for different controls?

Update

As un-lucky answered , it is just an alternative that buries my real question ie Why this complexity for different controls? . Lets forget about style , think of any other atrribute. Why check box is different when it comes to Attribute.Add ?

To add styles you have to try Style property instead for Attributes Following code will help you:

chkBox.Style.Add("opacity", ".3");

You can also try like this:

chkBox.Style["width"] = "70px";
chkBox.Style["height"] = "70px";

ASP.Net renders two <input> elements for checkboxes to overcome the fact that an unchecked checkbox is not POST ed.

I guess the easier approach is to assign CSS classes in code, and adjust your CSS to deal with the HTML details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM