HTML标签标记和ASP.NET [英] Html label tag and ASP.NET
问题描述
什么是使用的最佳方法<标签>
ASP.NET应用程序中的标签?我想这是XHTML有效,方便和实用。
What is the best way to use the <label>
tag within an ASP.NET application? I want it to be XHTML valid, accessible and usable.
据我了解的最佳方式是这样的:
I understand the optimal way is this:
<label for="Username">Username:</label>
<input type="text" id="Username" runat="server" />
但是,如果上述code是在ASP.NET用户控件,输入ID将会改变,这意味着标签的为属性是没用的。我可以使标签标记服务器控件,并设置它的为,在code(Username.ClientID)属性,但它似乎想了很多工作,为这样一个简单的事情。
But if the above code is in an ASP.NET user control, the input ID will change, meaning the label's "for" attribute is useless. I could make the label tag a server control and set it's "for" attribute in the code (Username.ClientID) but it seems like a lot of work for such a simple thing.
我也看到了这个HTML过去使用的:
I've also seen this HTML used in the past:
<label>
<span>Username</span>
<input type="text" id="Username" runat="server" />
</label>
什么是最佳方法?
What is the optimal approach?
推荐答案
我用&LT; ASP:标签... AssociatedControlID =用户名...&GT;
该控件。他们得到呈现为&LT;标签&gt;
标签,并设置为
属性适当
I use <asp:Label ... AssociatedControlID="Username" ...>
controls for this. They get rendered as <label>
tags and set the for
attribute appropriately.
请注意,您可以在Label控件中嵌套其它标记,如果你想:
Note that you can also nest other tags within the Label control if you wish:
<asp:Label ID="UsernameLabel"
Text="Username:"
AssociatedControlID="UsernameTextBox"
runat="server">
<asp:TextBox ID="UsernameTextBox" runat="server" />
</asp:Label>
这篇关于HTML标签标记和ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!