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?
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:
- place static text and input in the label together (ex.
<label>A TextBox <input id="txtbox1" type="text" /></label>
) - place static text in the label and set a
for
attribute on the label to theid
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>
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.
I personally don't like things that makes the code complicated.
Do you need to change the label contents in code?
If yes, use the Label control, because it is easy to change it in the code-behind... otherwise, just write the text there.
精彩评论