Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<% %> in ASP.NET (embedded code blocks)

Tags:

asp.net

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

like image 819
Matthew Cox Avatar asked Nov 21 '10 21:11

Matthew Cox


People also ask

Is code that is embedded directly within the ASP.NET page?

Embedded code blocks are supported in ASP.NET Web pages primarily to preserve backward compatibility with older ASP technology. In general, using embedded code blocks for complex programming logic is not a best practice, because when the code is mixed on the page with markup, it can be difficult to debug and maintain.

What is master page in ASP.NET with example?

A master page is an ASP.NET file with the extension . master (for example, MySite. master) with a predefined layout that can include static text, HTML elements, and server controls. The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary . aspx pages.

How do you comment out in ASP.NET HTML?

aspx page, and then clicking the “comment” command button that is on HTML Source Editor Toolbar: This will automatically wrap the selected content with a <%-- --%> block. You can likewise move the cursor within it and click the uncomment command to remove the comment.

What is ASP.NET page structure?

A directive controls how an ASP.NET page is compiled. The beginning of a directive is marked with the characters <%@ and the end of a directive is marked with the characters %>. A directive can appear anywhere within a page. By convention, however, a directive typically appears at the top of an ASP.NET page.


2 Answers

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
like image 148
Dan Parsonson Avatar answered Sep 18 '22 13:09

Dan Parsonson


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

like image 37
John Hartsock Avatar answered Sep 18 '22 13:09

John Hartsock