如何使用<标签>ASP.NET 中的标签?

发布于 2024-07-13 02:54:40 字数 592 浏览 5 评论 0原文

如何在 ASP.NET 应用程序中使用 标记? 我希望它有效、可访问且可用。

我理解最佳的HTML方式是这样的:

<label for="Username">Username:</label>
<input type="text" id="Username" runat="server" />

但是如果上面的代码在ASP.NET用户控件中,输入ID将会改变,这意味着标签的“for”属性是无用的。 我可以将 label 标签设置为服务器控件,并在代码中设置其“for”属性(Username.ClientID),但对于如此简单的事情来说,似乎需要做很多工作。

我过去也见过这种 HTML:

<label>
    <span>Username</span>
    <input type="text" id="Username" runat="server" />
</label>

正确的方法是什么?

How can I use the <label> tag within an ASP.NET application? I want it to be valid, accessible, and usable.

I understand the optimal HTML way is this:

<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.

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 proper approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦里的微风 2024-07-20 02:54:40

我为此使用 控件。 它们被渲染为 标签并适当地设置 for 属性。

请注意,如果您愿意,还可以在 Label 控件中嵌套其他标签:

<asp:Label ID="UsernameLabel"
           Text="Username:"
           AssociatedControlID="UsernameTextBox"
           runat="server">
    <asp:TextBox ID="UsernameTextBox" runat="server" />
</asp:Label>

I use <asp:Label ... AssociatedControlID="Username" ...> controls for this. They get rendered as <label> tags and set the for attribute appropriately.

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>
笑红尘 2024-07-20 02:54:40

您也可以这样写:

<label for="<%= Username.ClientID %>">Username:</label>
<asp:TextBox ID="Username" runat="server" />

Phil Haack 有一篇 博客文章< /a> 关于这个主题

You can also write it like this:

<label for="<%= Username.ClientID %>">Username:</label>
<asp:TextBox ID="Username" runat="server" />

Phil Haack has a blog post on this topic

笑着哭最痛 2024-07-20 02:54:40

使用 服务器控件。 它有一个属性,可用于设置关联的控件 ID。

<asp:Label ID="label1" runat="server" Text="Username" AssociatedControlID="Text1" />
<asp:TextBox ID="Text1" runat="server" />

use the <asp:Label> server control. It has a property that you can use to set the associated control ID.

<asp:Label ID="label1" runat="server" Text="Username" AssociatedControlID="Text1" />
<asp:TextBox ID="Text1" runat="server" />
怪我入戏太深 2024-07-20 02:54:40

我想最简单的方法就是这样。

<asp:Label AssociatedControlID="Username" runat="server" Text="Username:"></asp:Label>
<asp:TextBox ID="Username" runat="server"></asp:TextBox>

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>
萌吟 2024-07-20 02:54:40

如果您想要一个标签,但在 AssociatedControlID 中没有其他控件可使用,则可以使用标签本身

<asp:Label ID="Not_Span" AssociatedControlID="Not_Span" Text="Will be rendered as label" />

If you want a label, but don't have another control to use in AssociatedControlID one can use the label itself

<asp:Label ID="Not_Span" AssociatedControlID="Not_Span" Text="Will be rendered as label" />
只是偏爱你 2024-07-20 02:54:40

如果您使用的是 .NET 4,您现在可以使用 ClientIDMode 属性来配置一个或多个控件以使用静态或可预测的 ID。 ClientIDMode 属性可以直接在 TextBox 上设置,也可以在任何父控件或包含的页面上设置。

<label for="Username">Username:</label>
<asp:TextBox ID="Username" runat="server" ClientIDMode="Static" />

MSDN 上了解有关 ClientIDMode 的更多信息

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.

<label for="Username">Username:</label>
<asp:TextBox ID="Username" runat="server" ClientIDMode="Static" />

Read more about the ClientIDMode on MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文