tags:

views:

182

answers:

4

What is the difference between the following?

<asp:Label runat="server">Hello World</asp:Label>

<asp:Label runat="server" Text="Hello World"></asp:Label>


UPDATED:

If they are exactly the same then why does

<asp:Label ID="Label1" runat="server">
There were <%#transactionCount%> transactions today
</asp:Label>

bind correctly when using single-value databinding and displays the correct value for transactionCount, while...

<asp:Label ID="Label1" runat="server" Text="There were <%#transactionCount%> transactions today">
</asp:Label>

does not show a value for transactionCount in the page?


UPDATED AGAIN:

I understand the points being made about using the Literal controls. I shall slap my face accordingly - but it still doesn't solve the problem - though is perhaps getting closer to a solution.

If I use...

<asp:Literal ID="Label1" runat="server" Text="<%#transactionCount%>"></asp:Literal>

...I see transactionCount's value show up in the web page.

On the other hand, if I put any character or word in front of the single-value databinding field e.g.

<asp:Literal ID="Label1" runat="server" Text="No. <%#transactionCount%>"></asp:Literal>

...transactionCount does not appear.

+4  A: 

They will render the same in your final HTML. However the Text attribute is useful for programmatically setting the displayed text in your code behind.

Patrick McDonald
..and adds consistency with the rest of the server controls
Eduardo Molteni
Thanks for the answer - but I'm not finding they operate the same for single-value databinding. I've updated my question above
Joe
+2  A: 

Yes, outside of satisfying the ITextControl interface. Remember that 99% of developers use Labels wrong--one generally should be using Literals to hold output, if not just <%# Databind() %>. You don't need a server-side span for most things.

Where one should use a label is to create a label field in your form:

<asp:Label runat="server" id="LabelForFirstName" Text="First Name:" AssociatedControlID="firstname />
<asp:TextBox runat="server" id="FirstName" />

Gives one a label tied to the first name tag and semantically correct HTML.

Wyatt Barnett
A: 

If you set the text as Hello World you cannot change its value progamatically from your code-bhind file.

Whereas, woukd let you change the value as mytxt.Text = "New text"

But they will be rendered the same way.

Bhaskar
A: 

Another reason why they are different is to support implicit localization. In the resource file you specify ID.Property (i.e. Label1.Text).

Greg