Reputation: 7525
I am setting the css class in the code behind in ASP.NET
I could either do:
txtBox.Attributes.Add("class", "myClass");
or
txtBox.Attributes["class"] = "myClass";
Upvotes: 4
Views: 3064
Reputation: 50018
1) Add adds the attribute, while [] allows you to access the value directly and assign it
2) Use [] if Attributes.Contains the value, otherwise Add it
3) Usually an ArgumentException will occur (An item with the same key has already been added)
Upvotes: 4
Reputation: 25278
One is adding an attribute, the other is referencing/setting it.
You may not want to add it if it already exists.
Upvotes: 0