MadSeb
MadSeb

Reputation: 8234

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" )

Thanks

Upvotes: 2

Views: 459

Answers (7)

M4N
M4N

Reputation: 96551

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

Upvotes: 1

SLaks
SLaks

Reputation: 887365

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.

Upvotes: 4

carlbenson
carlbenson

Reputation: 3207

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.

Upvotes: 0

Fammy
Fammy

Reputation: 563

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.

Upvotes: 0

bechbd
bechbd

Reputation: 6341

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.

Upvotes: 0

asawyer
asawyer

Reputation: 17808

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

Edit - Sorry I meant in server side code.

Upvotes: 0

The Evil Greebo
The Evil Greebo

Reputation: 7138

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.

Upvotes: 1

Related Questions