Html 如何在 ASP.NET 中使用 <label> 标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/493801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use the <label> tag in ASP.NET?
提问by Alex York
How can I use the <label>
tag within an ASP.NET application? I want it to be valid, accessible, and usable.
如何<label>
在 ASP.NET 应用程序中使用标记?我希望它是有效的、可访问的和可用的。
I understand the optimal HTML way is this:
我理解最佳的 HTML 方式是这样的:
<label for="Username">Username:</label>
<input type="text" id="Username" runat="server" />
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 its "for" attribute in the code (Username.ClientID
), but it seems like a lot of work for such a simple thing.
但是如果上面的代码在一个 ASP.NET 用户控件中,输入的 ID 就会改变,这意味着标签的“for”属性是没有用的。我可以将 label 标签设为服务器控件并在代码 ( Username.ClientID
) 中设置其“for”属性,但对于这样一个简单的事情来说,似乎需要做很多工作。
I've also seen this HTML used in the past:
我还看到了过去使用的这个 HTML:
<label>
<span>Username</span>
<input type="text" id="Username" runat="server" />
</label>
What is the proper approach?
什么是正确的方法?
回答by Sean Bright
I use <asp:Label ... AssociatedControlID="Username" ...>
controls for this. They get rendered as <label>
tags and set the for
attribute appropriately.
我<asp:Label ... AssociatedControlID="Username" ...>
为此使用控件。它们被呈现为<label>
标签并for
适当地设置属性。
Note that you can also nest other tags within the Label control if you wish:
请注意,如果您愿意,您还可以在 Label 控件中嵌套其他标签:
<asp:Label ID="UsernameLabel"
Text="Username:"
AssociatedControlID="UsernameTextBox"
runat="server">
<asp:TextBox ID="UsernameTextBox" runat="server" />
</asp:Label>
回答by Christian Hagelid
回答by Matt Brunell
use the <asp:Label>
server control. It has a property that you can use to set the associated control ID.
使用<asp:Label>
服务器控件。它具有可用于设置关联控件 ID 的属性。
<asp:Label ID="label1" runat="server" Text="Username" AssociatedControlID="Text1" />
<asp:TextBox ID="Text1" runat="server" />
回答by Brian Kim
I guess the easiest way to do it is this.
我想最简单的方法就是这样做。
<asp:Label AssociatedControlID="Username" runat="server" Text="Username:"></asp:Label>
<asp:TextBox ID="Username" runat="server"></asp:TextBox>
回答by Christian Hagelid
If you are using .NET 4 you can now use the ClientIDMode property to configure one or more controls to use static or predictable ID's. The ClientIDMode property can be set on the TextBox directly or you can set it on any parent control or the containing page.
如果您使用的是 .NET 4,您现在可以使用 ClientIDMode 属性来配置一个或多个控件以使用静态或可预测 ID。ClientIDMode 属性可以直接在 TextBox 上设置,也可以在任何父控件或包含页面上设置。
<label for="Username">Username:</label>
<asp:TextBox ID="Username" runat="server" ClientIDMode="Static" />
Read more about the ClientIDMode on MSDN
在MSDN上阅读有关 ClientIDMode 的更多信息
回答by RMalke
If you want a label, but don't have another control to use in AssociatedControlID
one can use the label itself
如果你想要一个标签,但没有另一个控件可以使用,AssociatedControlID
可以使用标签本身
<asp:Label ID="Not_Span" AssociatedControlID="Not_Span" Text="Will be rendered as label" />
回答by magn
You my also try and this:
你我也试试这个:
<asp:Label ID="Label1" runat="server" Text="label"></asp:Label>
This is what Visual Studio, or any other software gives you if you drag and drop a label.
如果您拖放标签,这就是 Visual Studio 或任何其他软件为您提供的。
回答by chugh97
<p><asp:Label ID="label1" Text="Username:" AssociatedControlID="txtUserName" runat="server"> <asp:TextBox ID="txtUserName" runat="server" /></asp:Label></p>