The difference between control.Attributes.Add and control.Attributes []

I am installing a css class in code located in ASP.NET

I could do:

txtBox.Attributes.Add("class", "myClass"); 

or

 txtBox.Attributes["class"] = "myClass"; 
  • What are the differences?
  • Are there any situations in which you need to use another?
  • What happens in case 1 if the class is already assigned on the aspx page? Does he rewrite it?
+4
source share
2 answers

1) Add adds an attribute, and [] allows you to directly access the value and assign it
2) Use [] if Attributes.Contains value, otherwise add it
3) Usually an ArgumentException is raised (an element with the same key has already been added)

+4
source

One adds an attribute, the other refers / sets it.

You might not want to add it if it already exists.

0
source

All Articles