gpt4 book ai didi

html - ASP.NET 标签等效于 HTML 'for' 属性

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:59 25 4
gpt4 key购买 nike

我正在将使用 Bootstrap 3 的 HTML 代码转换为 ASP.NET Web 窗体。 Bootstrap 3 类“form-group”有一个带有“For”属性的标签。例如:

<div class="form-group">
<label for="txtField" class="control-label">Caption:</lable>
<input type="text" id="txtField" name="txtField" class="form-control" />
</div>

我正在将上述代码转换为使用控件:

<div class="form-group">
<asp:label class="control-label">Caption:></asp:lable>
<asp:TextBox id="txtField" class="form-control" /asp:TextBox>
</div>

但是<asp:TextBox>没有属性“名称”和 <asp:Label>没有属性“for”。我应该用什么代替?或者如果我跳过这些属性根本就没有关系?

最佳答案

使用AssociatedControlID:

<asp:Label runat="server" CssClass="control-label" AssociatedControlID="txtField">Caption</asp:Label>
<asp:TextBox runat="server" ID=txtField" CssClass="form-control" />

来自MSDN documentation page :

When the AssociatedControlID property is set, the Label control renders as an HTML label element, with the for attribute set to the ID property of the associated control. You can set other attributes of the label element using the Label properties. For example, you can use the Text and AccessKey properties to provide the caption and hot key for an associated control.

顺便说一句,似乎很多人似乎认为省略这些属性不会有任何影响,但实际上它会对某些用户产生深远的影响;特别是使用屏幕阅读器的用户。

屏幕阅读器使用标签上的 for 属性来告诉用户在遇到表单控件时他们应该输入什么。因此,您不仅应该努力将这些属性包含在您的 Web 应用程序中,还应该使标签的内容尽可能具有描述性。

关于html - ASP.NET 标签等效于 HTML 'for' 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22230650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com