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

标签 html asp.net

我正在将使用 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/

相关文章:

asp.net - 使用 MVC 库在 ASP.NET Web 窗体中进行基于属性的验证?

c# - JsonIgnore 属性在 ASP.NET 中不起作用?

javascript - Html5 Canvas 动画无法正常工作

html - Angular 4 启用 HTML5 验证

javascript - 如何使用 JSON API?

python - 使用 Python 从 HTML 中提取数据

c# - .net framework 上的 startup 和 startup.auth 代码放在哪里(不是 .net core)

gridview asp.net 中的 javascript 计时器

html - 一个header的字体不会变,其他的会变,怎么办?

javascript - 在 jquery 中上传文件后将当前选项卡设置为事件状态