Should I use the ASP:Label tag?

a 夏天 提交于 2019-12-10 17:20:12

问题


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?


回答1:


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>



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6839007/should-i-use-the-asplabel-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!