Is it's a good pracice to give same id's to multiple tags?
The first thing that comes to my mind when i hear the word ID is that it's something which is unique, Well it开发者_Python百科 is in the case of ASP.NET server tags. But it seems that there is nothing wrong using same id's for multiple tags if it's not a server tag. So is it a problem at all?
Your DOM selectors will fail. document.getElementById
will only return the first match, and different javascript libraries might to their own funky stuff depending on the parsing they do on your selectors. plus, different browsers might also do their own when using querySelectorAll
.
I'd recommend to use a class
attribute instead of an id
, even if you do not intend to address the elements with javascript. Repeating the same id
on a page is still invalid code and will lead to uncertain results.
So is it a problem at all?
It wouldn't validate: the IDs of HTML elements are supposed to be unique. It would be difficult to hyperlink to an element whose ID is not unique.
No , you shouldn't reuse the tags even though they are serverside, they add the clientid to the server but again it should be unique.
anyway why do you want to reuse them??
Please see the below code , we can make it unique like this
<li id="userID<%# Eval("UserId")%>">
other example
<asp:Repeater ID="idtest" runat="server">
<ItemTemplate>
<div id="catID<%# Eval("Id")%>" class="Content">
<ul id="someotherRandom<%# Eval("Id").ToString()%>" style="width:364px;">
</ul>
</div>
</ItemTemplate>
</asp:Repeater>
IDs should be unique. If you need to identify a tag that repeats then you need to give it a class. The class attribute is intended to identify multiple items on a page.
精彩评论