RemarkLima
RemarkLima

Reputation: 12037

ASP.NET C# Adding attribute to control - it comes out in lowercase?

I have a ListBox control:

<asp:ListBox runat="server" ID="lstBox" />

In code behind, I use the following:

int _maxListBoxSelection = 3;
lstBox.Attributes.Add("data-maximumSelectionSize", _maxListBoxSelection.ToString());

However, when I inspect the HTML the ListBox is rendered so:

<select id="blah_lstBox" name="blah$lstBox" data-maximumselectionsize="3" />

It seems similar to the reported thing here: How to prevent asp.net to make lowercase my custom attribute I placed in a server control

This is in .NET 4.0, using Visual Studio 2010.

Obviously, it means for accessing these via JavaScript it's nice to know that everything will be converted to lowercase.

So, why does ASP.NET change these to lower case? Can this behaviour be changed?

EDIT to ADD

Just tried with non data attributes and again ASP.NET changes these to lowercase. So if I add aCustomAttribute it gets changed to acustomattribute - is this expected?

Upvotes: 0

Views: 1396

Answers (2)

Andrei
Andrei

Reputation: 56688

This is a "by design" behavior. From XHTML Standards in Visual Studio and ASP.NET, MSDN, section "ASP.NET Features for XHTML Conformance":

Tag and attribute names are rendered in lowercase

So this is done to conform with XHTML standard. As others have noted, one should use dashes instead of came case to ensure readability of the attribute name.

Upvotes: 1

LbISS
LbISS

Reputation: 630

The HTML5 specification restricts the usage to lower case

All attributes on HTML elements in HTML documents get ASCII-lowercased automatically

Upvotes: 1

Related Questions