views:

50

answers:

1

I'm starting to learn ASP.NET and going through the tutorial videos on www.asp.net. I understand the basic concept of web application frame works like ASP/PHP/ASP.NET. HTML/XHTML is created with special tags that the server knows to read and replace with content. I did a little bit with ASP during a summer internship back in the 90's.

Yet with the few examples of ASP.NET I've seen so far it seems ASP.NET has two types of server interpreted tags.

Most of the time I see <asp:XXXX > tags such as

<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>

but I also see regular ASP tags like

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="TailspinSpyWorks.SiteMaster" %>

What is the difference between these two tag formats? Both are interpreted by the server correct? Why would I use one over the other?

+5  A: 

The <%@ %> tags are called directives, they're used by the compilers for compiling the page or user control.

The <asp:....> tags are just ASP server controls (other prefixes may be user controls, etc) they're components in the page itself.

From your title <%# %> is again something different, it's called a data binding expression, that's the binding syntax used for binding whatever's inside, for example: <%# Eval("property %>.

There's also <%= %> which is equivalent to Response.Write() for outputting content directly in the page, and there's a <%: %> version that also does html encoding for sanitized output in .Net 4.0.

All of these simply have different purposes, so you're not usually choosing between them (though some cases certainly overlap), they're just used in different situations.

Nick Craver
Ok that makes sense. So what's the fundamental difference between the tags that start with <% and vs the ASP server controls? From the syntax they seem like two separate "families" of tags, if that makes any sense.
Eric Anastas
@Eric - The `<%` is for server syntax, either binding or inline printing directly, the `<asp:` are actual controls, full fledged things that bind/render/take input, etc, you can find a list here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.aspx
Nick Craver
Ok that sort of makes sense. I guess what's confusing me is when I did regular ASP years back I only remember using <% tags.
Eric Anastas