Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Label equivalent of HTML 'for' attribute

Tags:

html

asp.net

I am converting an HTML code that uses Bootstrap 3 into ASP.NET Web Form. The Bootstrap 3 class "form-group" has a tag with "For" attribute. For example:

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

I am converting the above code into using controls as:

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

But <asp:TextBox> does not have an attribute "name" and <asp:Label> does not have attribute "for". What should I use instead? Or it does not matter at all if I skip these attributes?

like image 886
Hidalgo Avatar asked Mar 06 '14 16:03

Hidalgo


People also ask

How to create a label using HTML in MVC?

The Display attribute on the StudentName property will be used as a label. The Html.Label () method generates a <label> element for a specified property of model object. You can specify another label text instead of property name as shown below. Want to check how much you know ASP.NET MVC?

How does ASP NET label work?

How does ASP.NET Label work? 1 Open a visual studio and create a new empty web application. 2 Now add a new web form, which is there in the solution explorer. 3 From the toolbox, select the label control to drag it and drop it on the web form. More ...

What is the for attribute on a form label?

Screen readers use the for attribute on labels to tell the user what they are expected to enter when they encounter a form control. So not only should you endeavour to include these attributes in your web applications, you should also aim to make the content of the label as descriptive as possible. Show activity on this post.

What is label tag helper in HTML?

The label TagHelper is simplest of all the tag helpers. The purpose of the label tag helper is to generate the label caption & for attribute for the expression provided in the asp-for attribute. It is applied on the <label> HTML element. It has only one server-side attribute asp-for. Example of Label Tag Helper.


1 Answers

Use AssociatedControlID:

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

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

As an aside, it seems that many people seem to think that omitting these attributes will have no effect where the reality is that it can have a profound effect on some users; specifically users with screen readers.

Screen readers use the for attribute on labels to tell the user what they are expected to enter when they encounter a form control. So not only should you endeavour to include these attributes in your web applications, you should also aim to make the content of the label as descriptive as possible.

like image 170
Sean Airey Avatar answered Sep 20 '22 02:09

Sean Airey