A simple question, I think:
I want to put a tag into an ASP.NET app I’ve been asked to maintain, so I’m coming at this from a newbie point of view just tinkering around the edges without knowing a lot.
I wrote an old ASP application back in 1998, so I am just running on memory…
How do I write some output to the webpage?
I know I can use an
<asp:label id="blah">
but then I need to define a Label blah; in my code behind and then assign it.
I believe that I can put in-place:
<% Response.Write("sometext"); %>
and that will write sometext in the location within the page. (Am I correct?)
Lastly, I remember there was a syntax to the effect of
<%= "some string" %>
but I can’t find the documentation on it, to say either it is deprecated, unadvised, or the rationale for such a decision.
I have tried googling for “ASP.NET grammar” but I can’t even find a good description that “<%=” even exists, though it is mentioned in a few blogs.
For something simple, like inject the global version number, or the current date, then I can’t see anything particularly wrong with in-place composition – it would save me defining 15 labels and having to initialise them all – though perhaps the asp:label approach could reference one global instance of a label?
Just asking for opinions on good practices 🙂
<%= string %>is perfectly valid ASP.NET syntax. The reason you will often find references to problems with using that is people use<%=(equivalent toResponse.Write) when they should use<%#(for databinding) or vice-versa.For example, we use it very extensively in our content managed site, where we pull in values from a global settings repository:
MSDN:
<%=(this is under the JScript.NET section but still applies)<%#Some others suggest
<%=is not a “best practice” or a very good approach, but I strongly disagree with that sentiment. For an MVC-ish type site (especially a site that is template- or view-driven in some way), the most direct approach is frequently more effective than using server controls.