简体   繁体   English

ASP.NET中的<%%>(嵌入式代码块)

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

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. 我理解这些在aspx页面的标记中有什么意义......但我不知道它们可以用于它们的全部功能,甚至不知道这些特殊指令的名称。

Example: 例:

can i put conditional statements like ifs or switches 我可以把条件语句,如ifs或开关

I have seen and used them to bind data from a data set for example 我已经看到并使用它们来绑定数据集中的数据

Any input is greatly appreciated 任何输入都非常感谢

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() <%=...%>等同于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 <%@...%>用于页面指令,通常位于ASPX文件的顶部
  6. <%--...--%> is for comments <%--...--%>用于评论
  7. <%:...%> is the same as <%= except it HTML-encodes the value <%:...%><%=相同,只是它对值进行HTML编码

These are code Block tags. 这些是代码块标记。

And yes you can wrap serverside code in these tags (examples in C#) 是的,您可以在这些标签中包含服务器端代码(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 这个<%=SomeVar %>将SomeVar放到HTML中

The MSDN documentation calls them embedded code blocks . MSDN文档称它们为嵌入式代码块 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. Directive是给予一种特定类型代码块的名称,这是ASP.NET页面顶部最常见的代码块,用于为编译器提供有关ASP.NET页面的信息。 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>

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). 当服务器收到对ASPX页面的请求时,它会生成一个内存类,该类继承自Page (或您指定的任何基类)。 The inherited class translates "normal" markup into static Response.Write() calls, <%...%> into equivalent code, and <%= someExpression %> into Response.Write(someExpression) . 继承的类将“正常”标记转换为静态Response.Write()调用,将<%...%>转换为等效代码,将<%= someExpression %>转换为Response.Write(someExpression) For the former code block, any valid C# (or VB) should be accepted; 对于前一个代码块,应接受任何有效的C#(或VB); for the latter, the embedded code must be a single expression (something you could assign to a variable. 对于后者,嵌入的代码必须是单个表达式(您可以将其分配给变量。

Yes, those symbols indicate to the server parsing the page that the code within those tags should be interpreted as code and not HTML. 是的,这些符号表示服务器在解析页面时,这些标签中的代码应该被解释为代码而不是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 查看ASP快速指南: http//www.w3schools.com/asp/default.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM