40

I understand what these signify in the markup of the aspx pages ... but I don't not know the full power they can be used for or even the name to denote these special directives.

Example:

can i put conditional statements like ifs or switches

I have seen and used them to bind data from a data set for example

Any input is greatly appreciated

2
  • 4
    If your doing Asp.net Webforms, I would refrain from using it too much. It's better presentation and easier to debug to put that code in the code behind instead of the html portion. Commented Nov 21, 2010 at 22:08
  • @ Lareau agreed. I was just interested in the flexibility of them. Commented Nov 21, 2010 at 22:14

5 Answers 5

69

Here (or here - in case it moves again) is a post I found and stashed away some time ago listing all the different inline server-side tags with examples. There are seven:

  1. <%...%> runs normal code
  2. <%=...%> is equivalent to Response.Write()
  3. <%#...%> is used for databinding expressions
  4. <%$...%> returns the value of an expression, and can be used in parameters (note: expressions are not code - see here)
  5. <%@...%> is for page directives, usually at the top of the ASPX file
  6. <%--...--%> is for comments
  7. <%:...%> is the same as <%= except it HTML-encodes the value
Sign up to request clarification or add additional context in comments.

2 Comments

That link appears dead but the MSDN article from this answer is pretty good.
what is unfortunate is that the MSDN article lists <%@ ... %> twice in the intro, but does not have any info on <%: ... %> server tag, which is what I was looking for... Here is another link to check out: weblogs.asp.net/ahmedmoosa/embedded-code-and-inline-server-tags
36

These are code Block tags.

And yes you can wrap serverside code in these tags (examples in C#)

<% if (x = y) {
  } else {
  }
%>

OR

<% if (x = y) {%>
   Write this HTML
<%  } else {%>
   Write this html
<%  }%>

There is also

This <%=SomeVar %> Which will out put SomeVar to HTML

Comments

10

The MSDN documentation calls them embedded code blocks. You can put pretty much any code you would place in code-behind files and the server will execute them before serving your pages to browsers.

Directive is the name given to one particular type of code block, the one most commonly seen at the top of ASP.NET pages to give the compiler information about your ASP.NET pages. They are delimited by <%@ and %>.

The language of the code blocks is the same one as specified in the directive block. A quick example:

<%@ Page Language="C#" %>
<!DOCTYPE html>
<html>
  <body>
    <p><% string hello = "Hello world!"; Response.Write(hello); %></p>
    <ol>
    <% for (int i = 1; i <= 5; ++i) { %>
        <li><% Response.Write("Item #" + i); %></li>
    <% } %>
    </ol>
  </body>
</html>

1 Comment

thank you for your responses. That is going to help with further research on different forms of embedded code blocks.
0

When the server receives a request for an ASPX page, it generates an in-memory class that inherits from Page (or whatever base class you specified). The inherited class translates "normal" markup into static Response.Write() calls, <%...%> into equivalent code, and <%= someExpression %> into Response.Write(someExpression). For the former code block, any valid C# (or VB) should be accepted; for the latter, the embedded code must be a single expression (something you could assign to a variable.

Comments

-2

Yes, those symbols indicate to the server parsing the page that the code within those tags should be interpreted as code and not HTML.

So, to answer your other question, you can use conditionals and most any other programming features supported by the server.

Check out a quick guide to ASP: http://www.w3schools.com/asp/default.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.