Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use the ASP:Label tag?

I'm building a form in ASP.NET to send an email. So far things work great and I'm able to pass my ASP:TextBox contents to the email without any issue. Right now how I've done things is put in static text as the TB label and then follow it up with a TB control for the input.

Should I be using the ASP:Label control instead?

Code example:

<div>
Pub Contact Phone: <asp:TextBox ID="PublicationContactPhone" runat="server" TabIndex="9"></asp:TextBox>
</div>

Is there a form best practice that says to have all the non-input text as labels or is it preference?

like image 535
Valien Avatar asked Dec 27 '22 16:12

Valien


2 Answers

This sounds like maybe a mixup of the ASP.NET <asp:Label> control and the HTML <label> element. For building forms it's a good practice to use the HTML <label> for an input label so that clicking on the label will give the input element focus, you can implement a label two ways:

  1. place static text and input in the label together (ex. <label>A TextBox <input id="txtbox1" type="text" /></label>)
  2. place static text in the label and set a for attribute on the label to the id of the input (ex. <label for="txtbox1">A TextBox</label> <input id="txtbox1" type="text" />)

So you can markup your page like so and the text Pub Contact Phone: will be clickable to give focus to the input

<div>
    <label>
        Pub Contact Phone: 
        <asp:TextBox ID="PublicationContactPhone" runat="server" TabIndex="9" />
    </label>
</div>
like image 113
MikeM Avatar answered Dec 30 '22 11:12

MikeM


You don't have to use the label control as the text is static. The label control is best used if you want to change the value of the static text in your code behind before the page is returned to the browser.

If you don't want to do this, then there is no need to use a label control.

like image 22
lomaxx Avatar answered Dec 30 '22 11:12

lomaxx