开发者

ASP.NET typing text vs using label

What's the difference between typing some text on a page vs inserting a label and typing some text into that label ?

Any reason why somebody would want to use a label vs just type text on the page ?

The only advantage that I can think of is that a label can be updated easily ( e.g user clicks a button , in the event code for the click action one can write something like label1.Text = "some value" )

开发者_StackOverflow中文版

Thanks


Labels can be associated with controls using the AssociatedControlID property, allowing the user to click the label to focus the control.
If a label is associated with a checkbox, clicking the label will toggle the checkbox.


You've nailed it. Putting text inside a label control allows you easy programmatic control over that portion of the page, while putting it directly in the HTML requires you to then jump through extra hoops if you want to modify it later.


In addition, you can also programmatically show/hide a label, add css styles, and associate it with an input control (AssociatedControlId property).


You can't easily apply CSS styling to random text on the page.

Edit - Sorry I meant in server side code.


The difference is that typing into a Label causes it to render the HTML from the server side while typing text into the HTML does not.

This is very useful if you want to change the text dynamically or if you need to deal with changing the text for internationalization.


ASP.NET labels should be used to much like HTML labels: to indicate which control this text is related to. ASP.NET also has the LiteralControl, which is just text, and is better suited to your needs.


Typing text directly onto your page is often uncontrollable - It is difficult to control where it will appear, and in what manner. Labels have very predictable features that can be adjusted easily to work with formatting. Furthermore, as your page gets more complicated, having text in labels that is identifiable with IDs makes things significantly easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜