Reputation: 5894
I've been working on someone's project and noticed Visual Studio produces a message for each ASP tag that has a class attribute. For example:
Attribute 'class' is not a valid attribute of element 'TextBox'.
If I visit the website, it seems to work correctly. An example that produces the message looks like this:
<asp:TextBox class="makeInline loginItem" ID="UserName" runat="server"></asp:TextBox>
On the website it becomes this:
<input type="text" class="makeInline loginItem" id="Login1_UserName" name="Login1$UserName">
So it looks like the class attribute gets carried over to the HTML tag. Is this fine, or is there a better way to do this?
Upvotes: 5
Views: 8657
Reputation: 54359
Server controls use CssClass
instead of class
(presumably to avoid ambiguity over the meaning of class
).
So it looks like the class attribute gets carried over to the HTML tag. Is this fine or is there a better way to do this?
Unknown attributes will be carried over. But while it works in this case, use the attributes that the control expects whenever possible. ASP.Net will occasionally alter the markup to "correct" it. Example: valid HTML 5 input type attributes (e.g. type="number"
) were "corrected" when the control was rendered until a fix was released to correct the problem.
You can place a custom attribute (e.g. data-*
) on a server tag without concern.
<asp:TextBox runat="server" ID="txtTest" data-foo="bar" />
This does not cause a validation error in Visual Studio 2012, and renders as expected.
Upvotes: 7
Reputation: 17385
The correct attribute to use is CssClass
it will also be converted to the HTML class
attribute when the page is generated.
Upvotes: 3
Reputation: 3332
Use CssClass
instead of class
to get rid of the message produced by Visual studio. But either way, using class works too.
Upvotes: 2